RE: [xsl] my node position within a certain context, efficiently?

Subject: RE: [xsl] my node position within a certain context, efficiently?
From: Syd Bauman <Syd_Bauman@xxxxxxxxx>
Date: Sat, 25 Oct 2008 15:07:31 -0400
MG> It seems, a simple position() function would work here,

Thank you. While the actual code you posted doesn't work, a minor
modification thereof comes extremely close. Using position() as you
demonstrate in your reply
a) doesn't quite meet use-case, as this needs to be issued from the
   //box//thing template, and
b) results in 2 <box> containers in the output

The obvious slight modification to this suggestion comes a lot
closer: 
  <xsl:template match="/">
    <result>
      <xsl:apply-templates select="//box//thing"/>
    </result>
  </xsl:template>
  <xsl:template match="box//thing">
    <thing myCount="{position()}">
      <xsl:value-of select="."/>
    </thing>
  </xsl:template>

This results in all of the <thing> elements being counted
irrespective of their position within a <box>, though, whereas I had
asked to count their positions with respect to their ancestor <box>.
(Turns out for my real use-case, this isn't a very important
constraint, so in a pinch this solution would do, thank you.)


MK> I would use ... <xsl:number from="box"/>

Thank you! "I should have known to look at <xsl:number>, that bag of
holding of XSLT counting tricks" I thought to myself. But then I
tired it, and it didn't work, producing exactly what I didn't want
(count in relation to parent, not box). But, recalling there was a
lot more nuance to this element, I went and re-read the material on
<xsl:number> in your book. After adding
  level="any"
to my <xsl:number>, I get exactly the desired output. Yay!


Thanks again to both of you.

Current Thread