[xsl] Position() on template matches

Subject: [xsl] Position() on template matches
From: "Kevin L. Cobb" <kevin.cobb@xxxxxxxxxxxx>
Date: Tue, 25 Oct 2005 16:15:38 -0400
I have an element in an XML document whose node I need the position of
in the context of my template match (unsure if I'm forming the question
correctly, sorry). The Source XML and the StyleSheet are below. The
resulting output is:

<?xml version="1.0" encoding="UTF-8"?>
<out>
  <myOut>1</myOut>
  <myOut>1</myOut>
  <myOut>1</myOut>
  <myOut>1</myOut>
</out>

But I the output to be:
<?xml version="1.0" encoding="UTF-8"?>
<out>
  <myOut>1</myOut>
  <myOut>2</myOut>
  <myOut>3</myOut>
  <myOut>4</myOut>
</out>

I suppose this is by design because all of the "output" nodes exist
separately in the XML file, i.e. if all of the "tester" elements were
under a single "output" node, I'd be OK. Would this be considered poorly
designed XML? Not that it matters because I don't control the XML in
this case.

Thanks for any and all help.

-Kevin


<!-- ============== -->
<!-- The Source XML -->
<!-- ============== -->
<test>
   <output>
      <tester>One</tester>
   </output>
   <output>
      <tester>Two</tester>
   </output>
   <output>
      <tester>Three</tester>
   </output>
   <output>
      <tester>Four</tester>
   </output>
</test>

<!-- ============== -->
<!-- The StyleSheet -->
<!-- ============== -->
<?xml version="1.0"?>
<xsl:stylesheet
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
   version="1.0">

   <xsl:template match="/">
      <out>
         <xsl:apply-templates/>
      </out>
   </xsl:template>

   <xsl:template match="output/tester">
      <xsl:variable name="pos"><xsl:number/></xsl:variable>
      <xsl:element name="myOut"><xsl:value-of
select="$pos"/></xsl:element>
   </xsl:template>
</xsl:stylesheet>

Current Thread