如何计算 following-sibling::*[1][self::disp-quote]

How to count following-sibling::*[1][self::disp-quote]

如何计算 following-sibling::*[1][self::disp-quote] 位置 EXAMPLE S/B EXAMPLE 1

输入文件

    <?xml version="1.0" encoding="UTF-8"?>
<sec>
    <title>Title</title>
    <p>vlvvnlfjkvv</p>
    <disp-quote><p content-type="example"><bold>EXAMPLE</bold> aaaaaaa</p></disp-quote>
    <disp-quote><p content-type="example"><bold>EXAMPLE</bold> aaaaaaa</p></disp-quote>
    <disp-quote><p content-type="example"><bold>EXAMPLE</bold> aaaaaaa</p></disp-quote>
    <p>aaaaaa</p>
    <p>aaaaaa</p>
    <disp-quote><p content-type="example"><bold>EXAMPLE</bold> aaaaaaa</p></disp-quote>
    <p>vvvkvuvhv</p>
</sec>

预期输出

    <?xml version="1.0" encoding="UTF-8"?>
<sec>
    <title>Title</title>
    <p>vlvvnlfjkvv</p>
    <disp-quote><p content-type="example"><bold>EXAMPLE 1</bold> aaaaaaa</p></disp-quote>
    <disp-quote><p content-type="example"><bold>EXAMPLE 2</bold> aaaaaaa</p></disp-quote>
    <disp-quote><p content-type="example"><bold>EXAMPLE 3</bold> aaaaaaa</p></disp-quote>
    <p>aaaaaa</p>
    <p>aaaaaa</p>
    <disp-quote><p content-type="example"><bold>EXAMPLE</bold> aaaaaaa</p></disp-quote>
    <p>vvvkvuvhv</p>
</sec>

XSLT

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">
    <xsl:template match="bold">
        <bold aid:cstyle="bold">
            <xsl:apply-templates />
            <xsl:if test=".='EXAMPLE' and ancestor::disp-quote[following-sibling::*[1][self::disp-quote]]">
                <xsl:for-each select="ancestor::disp-quote[following-sibling::*[1][self::disp-quote]]">
                    <xsl:value-of select="count(ancestor::disp-quote[following-sibling::*[1][self::disp-quote]])+1"/>
                </xsl:for-each>
            </xsl:if>
        </bold>
    </xsl:template>
</xsl:stylesheet>

根据评论编辑:

My Expected result only if it comes disp-quote with comes same following sibling disp-quote then add with position() Example 1, Example 2, Example 3. if its comes single disp-quote then No change only Example.

你把事情弄得很复杂了(或许真的很复杂?)

我愿意

<xsl:template match="disp-quote//bold[.='EXAMPLE']">
  <bold aid:cstyle="bold">
    <xsl:text>EXAMPLE </xsl:text>
    <xsl:number level="any" count="bold" from="sec"/>
  </bold>
</xsl:template>

我认为 Michael 建议使用 xsl:number 是你想要使用的方式,只要你想在任何 disp-quote 中对所有 <b>EXAMPLE</b> 进行编号,甚至是单个编号;如果您只想在相邻组中有多个 disp-quote 时对它们进行编号,那么评论中提出的建议可能会有所帮助:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="#all"
    version="3.0">

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:output indent="yes"/>

  <xsl:template match="sec[disp-quote]">
      <xsl:copy>
          <xsl:for-each-group select="*" group-adjacent="boolean(self::disp-quote[p/bold = 'EXAMPLE'])">
              <xsl:choose>
                  <xsl:when test="current-grouping-key() and tail(current-group())">
                      <xsl:apply-templates select="current-group()" mode="number-examples"/>
                  </xsl:when>
                  <xsl:otherwise>
                      <xsl:apply-templates select="current-group()"/>
                  </xsl:otherwise>
              </xsl:choose>
          </xsl:for-each-group>
      </xsl:copy>
  </xsl:template>

  <xsl:template match="disp-quote" mode="number-examples">
      <xsl:copy>
          <xsl:apply-templates>
              <xsl:with-param name="dq-pos" tunnel="yes" select="position()"/>
          </xsl:apply-templates>
      </xsl:copy>
  </xsl:template>

  <xsl:template match="disp-quote/p/bold[. = 'EXAMPLE']">
      <xsl:param name="dq-pos" tunnel="yes" select="()"/>
      <xsl:copy>
          <xsl:value-of select="., $dq-pos"/>
      </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/bwe3bW/