Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use xsl number to count elements

Tags:

xslt

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!

like image 552
joe Avatar asked Apr 05 '11 13:04

joe


People also ask

What is the use of Count in XSLT?

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?

What is the use of XSL number in JavaScript?

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

How to use XPath in XSLT?

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.

How to count elements with specific attribute names in XML document?

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.


1 Answers

Use <xsl:number level="any"/>

like image 192
Michael Kay Avatar answered Sep 22 '22 00:09

Michael Kay