Re: [xsl] position of a child node

Subject: Re: [xsl] position of a child node
From: David Carlisle <davidc@xxxxxxxxx>
Date: Thu, 15 Nov 2001 22:19:01 GMT
>     Actually I thought it was rather clear, but maybe it was not.
well still isn't really:-)


> As I am traversing this tree  with a statement like
> 
> <xsl:template match = "testcase">
   .....

a match pattern in a template doesn't traverse the tree it just executes
a template if you happen to arrive at that node during your traversal
(which you specify with select attributes on apply-templates.)
This difference is important as position() in the template does not
relate to anything in the souce file but to the position in the current
node list formed by ordering the node set of the currently active
apply-templates into document order. So for example if you go
<xsl:apply-templates select=".."/> when you are at a child2 element then 
the template for testcase will run and position() will be 1, for every
test case.

But what I think  number you want is, given


<mainElement>
 <testcase>
  <child1>first text</child1>
  <child2>
    <child21>
        this is the text node for I want which I want to find its position
and it
       contains three dots (...)
    </child21>
  </child2>
</testcase>
<testcase>
  <child1>first text</child1>
  <child2>
    <child21>
       some other text that I don't want
      <child211>
          This is the text node for I which want to find its position and it
          contains three dots (...)
      </child211>
    </child21>
  </child2>
 </testcase>
</mainElement>

to get 3 on the first test case and 4 on the second, is that right?

if so you want the number of descendents of tetscase with teh property
that the marked element follows or is a descendent of the elments being
counted. (incidentally why do you want this number, it seems a very
strange thing to want?)

probably the easiest way is to go
<xsl:for-each select="descendent::*">
 <xsl:if test="contains(.,'...')">
  <xsl:value-of select="position()"/>
 </xsl:if>
</xsl:for-each>

there are certainly more efficient ways (you can look at some general
set difference functions at Jeni's site I think, but teh above is simper
to understand and maintain, but inefficient if there are lots of nodes
after the marked one.

David


_____________________________________________________________________
This message has been checked for all known viruses by Star Internet
delivered through the MessageLabs Virus Scanning Service. For further
information visit http://www.star.net.uk/stats.asp or alternatively call
Star Internet for details on the Virus Scanning Service.

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread