user2628363
user2628363

Reputation: 143

Counting nodes in xsl

My xml file is like this:

  <?xml version="1.0" encoding="UTF-8"?> 
  <?xml-stylesheet type="text/xsl" href="form.xsl"?>
  <Letter xmlns:xsi="http://www.irica.com/ECEP/1383-12/SendSchema-instance" xsi:noNamespaceSchemaLocation="file:ECEP_Letter.xsd">    
   ...
    <Keywords>
        <Keyword>A</Keyword>
        <Keyword>B</Keyword>
        <Keyword>C</Keyword>
 </Keywords>
 </Letter>

Now I want to count number of Keyword and put them in a table like this:

No. | keyword

1 | A

2 | B

3 | C

I've written this code so far:

  ...
  </table>
 <h2>keywords: <xsl:value-of select="count(/Letter/Keywords/Keyword)"/></h2>
 <table border="2">
    <tr >
    <th style="text-align:right">No.</th>
    <th style="text-align:right">keyword</th>

  </tr>
     <xsl:for-each select="Letter/Keywords/Keyword">

    <tr>
     <td><xsl:value-of select="Keyword" /></td>
      <td><xsl:value-of select="."/></td>

  </tr>
  </xsl:for-each>
</table>
...

It prints total number of keywords but in the table it only prints A,B,C but not the numbers.

Upvotes: 0

Views: 47

Answers (2)

Saurav
Saurav

Reputation: 640

Instead of this

<td><xsl:value-of select="."/></td>

Do this

<xsl:value-of select="position()"/>

Upvotes: 0

michael.hor257k
michael.hor257k

Reputation: 117140

I don't see that you actually want to count them - merely number them, which can be done easily by:

...
<xsl:for-each select="Letter/Keywords/Keyword">
    <tr>
        <td><xsl:value-of select="position()"/></td>
        <td><xsl:value-of select="."/></td>
    </tr>
</xsl:for-each>
...

Upvotes: 1

Related Questions