How to count following-sibling::*[1][self::disp-quote]
position EXAMPLE S/B EXAMPLE 1
Input File
<?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>
Expected Output
<?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>
Edit from comments:
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.
I think Michael's suggestion to use xsl:number
is the way you want to use as long as you want to number all <b>EXAMPLE</b>
in any disp-quote
, even a single one; if you only want to number them if there is more than one disp-quote
in an adjacent group then the suggestion made in a comment might help:
<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>
Thanks for reply @Martin Honnen!
@Sandy, if the the reply solved your problem then you can indicate by accepting the answer, then it is clear the question is solved.