Below I am counting elements using xsl:number
. I would like to count the chapters in order, see below:
XML:
<map>
<part>
<chapter/>
</part>
<chapter/>
<part>
<chapter/>
<chapter/>
</part>
</map>
XSLT:
<xsl:template match="chapter">
<xsl:variable name="chapNum">
<xsl:number count="chapter" format="1"/>
</xsl:variable>
<xsl:value-of select="$chapNum"/>
</xsl:template>
OUTPUT:
1
1
1
2
Desired OUTPUT:
1
2
3
4
I believe I need to use the from attribute but I am not sure how to implement it.
Thanks for any help in advance!
The syntax says the count function tells the XSLT processor to count each time in which an XML tag found at the XPATH expression. It returns a number value that specifies the number of sequence elements contained in a sequence of items that passes by. The empty set of the count is returned to ‘zero’. How does the count function work in XSLT?
The <xsl:number> element is used to determine the integer position of the current node in the source. It is also used to format a number. Optional. An XPath expression that specifies what nodes are to be counted Optional. Controls how the sequence number is assigned
Here the XPATH starts by making a function call that is a count () which counts the number of lists in the sequence. Here is the sequence of author elements. Next XPath asks the XSLT engine to read the condition assigned greater than two (gt). The comparison is made with the sequence and the respective result is displayed.
XSLT count function is defined to count elements with specific attribute names defined in the XML document. It neglects the arithmetic on a set of nodes to validate the number of counts.
Use <xsl:number level="any"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With