Re: [xsl] Position() vs counter variable

Subject: Re: [xsl] Position() vs counter variable
From: Brian Chrisman <incubus@xxxxxxxxxxxxxx>
Date: Mon, 4 Apr 2005 12:18:21 -0700
On Mon, Apr 04, 2005 at 03:07:53PM -0400, Maria Amuchastegui wrote:
> My data contains a series of tables that I need to number consecutively,
> i.e. 1, 2, 3. I want to do this using the position() property. However,
> because the position is relative to the parent node, the numbering is
> restarting whenever there is a new parent node, so I get: 1, 2, 1. Is there
> a way to do this using position(), or do I have to set up a counter
> variable?

position() isn't really 'relative to the parent node'.
It's relative to the list of nodes selected by a particular
select="" attribute.

Here you have a for-each selecting the Section elements
of a particular type.  The position() will tell you what
the order is among those selected Section elements.

What you are probably looking for is something like
count(preceding-sibling::Section) instead of position() to get 
the number of Section elements occuring prior to the current
node.. 


>  
> Here is the XSL:
>  
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform
> <http://www.w3.org/1999/XSL/Transform> "
> xmlns:fo="http://www.w3.org/1999/XSL/Format
> <http://www.w3.org/1999/XSL/Format> ">
>  <xsl:output method="html"/>
>  <xsl:template match="/">
>   <html>
>    <body>
>     <xsl:apply-templates select="myXMLData"/>
>    </body>
>   </html>
>  </xsl:template>
>  <xsl:template match="myXMLData">
>   <xsl:apply-templates select="//BTN"/>
>  </xsl:template>
>  <xsl:template match="//BTN">
>   <b><xsl:value-of select="@number"/></b>
>   <xsl:for-each select="Section[@type='Detail']">
>    <table border="1">
>     <th colspan="6" align="left">
>      This is table <xsl:value-of select="position()"/>. 
>     </th>
>     <tr>
>      <th>ID </th>
>      <th>Date </th>
>      <th>Location </th>
>      <th>Number </th>
>      <th>Duration</th>
>      <th>Charges </th>
>     </tr>
>     <xsl:apply-templates select="ServiceHeader"/>
>    </table>
>    <br/>
>   </xsl:for-each>
>  </xsl:template>
>  <xsl:template match="ServiceHeader">
>   <xsl:if test="@group != '' ">
>    <tr>
>     <td colspan="6"><xsl:value-of select="@group"/></td>
>    </tr>
>   </xsl:if>
>   <xsl:apply-templates select="message"/>
>  </xsl:template>
>  <xsl:template match="message">
>   <tr>
>    <td><xsl:value-of select="id"/></td>
>    <td><xsl:value-of select="date"/></td>
>    <td><xsl:value-of select="location"/></td>
>    <td><xsl:value-of select="number"/></td>
>    <td><xsl:value-of select="duration"/></td>
>    <td><xsl:value-of select="charges"/></td>
>   </tr>
>  </xsl:template>
> </xsl:stylesheet>
> 
>  
> And here is the XML:
>  
> <?xml version="1.0" encoding="UTF-8"?>
> <myXMLData xmlns="">
>  <BTN number="635 5669">
>   <Section number="400" type="Detail" subHead1="Long distance calls"
> subHead2="">
>    <ServiceHeader group="">
>     <message>
>      <id>1-01</id>
>      <date>2005-04-23</date>
>      <location>Quebec QC</location>
>      <number>418 683 1234</number>
>      <duration>1</duration>
>      <charges>0.43</charges>
>      <savings>.043</savings>
>      <amount/>
>     </message>
>     <message>
>      <id>1-02</id>
>      <date>2005-04-28</date>
>      <location>Montreal QC</location>
>      <number>514 485 6611</number>
>      <duration>2</duration>
>      <charges>3.44</charges>
>      <savings/>
>      <amount/>
>     </message>
>    </ServiceHeader>
>   </Section>
>   <Section number="420" type="Summary"/>
>   <Section number="430c" type="Detail" subHead1="Advantage Per Call"
> subHead2="">
>    <ServiceHeader group="Canada">
>     <message>
>      <id>1-06</id>
>      <date>2005-05-06</date>
>      <location>Collingwood ON</location>
>      <number>705 445 1030</number>
>      <duration>1.4</duration>
>      <charges>5.01</charges>
>      <savings/>
>      <amount/>
>     </message>
>     <message>
>      <id>1-07</id>
>      <date>2005-04-12</date>
>      <location>Erin ON</location>
>      <number>519 833 2380</number>
>      <duration>1.1</duration>
>      <charges>0.78</charges>
>      <savings/>
>      <amount/>
>     </message>
>    </ServiceHeader>
>   </Section>
>   <Section number="450c" type="Summary" subHead1="" subHead2=""/>
>  </BTN>
>  <BTN number="635 5670">
>   <Section number="400" type="Detail" subHead1="Long distance calls"
> subHead2="">
>    <ServiceHeader group="">
>     <message>
>      <id>1-12</id>
>      <date>2005-04-08</date>
>      <location>Edmonton AB</location>
>      <number>780 496 8200</number>
>      <duration>3</duration>
>      <charges>0.25</charges>
>      <savings/>
>      <amount/>
>     </message>
>     <message>
>      <id>1-13</id>
>      <date>2005-04-15</date>
>      <location>Halifax NS</location>
>      <number>902 490 6277</number>
>      <duration>5</duration>
>      <charges>1.01</charges>
>      <savings/>
>      <amount/>
>     </message>
>    </ServiceHeader>
>   </Section>
>   <Section number="420" type="Summary"/>
>  </BTN>
> </myXMLData>

Current Thread