[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: Function from-ancestors and parent element name.


Subject: Re: Function from-ancestors and parent element name.
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Wed, 08 Sep 1999 12:39:48 -0400

At 99/09/08 11:41 -0400, Stephane St-Denis wrote:
    '********* I would like to have a count of the
    ' ******** parent of the name element *****
    <xsl:value-of select="count(from-ancestors(parent(.)))"/>

Using the XSLT-19990813 Working Draft, you would use predicates in the fashion shown in the example below. Note that I am not using variables where I could, but I'm hoping the way I've coded it illustrates what you need.


I'm asking for a count of all ancestor elements whose name is equal to the name of the parent of the current element ... which is what I think you are asking for.

I hope this helps.

........ Ken

T:\ftemp>type test.xml
<?xml version="1.0"?>
<test>
 <test attr="a">
  <test attr="b">
   <other attr="c">
    <test attr="d">
     <test attr="e">
      <other attr="f">
       <test attr="g">
        <test attr="h"/>
       </test>
      </other>
     </test>
    </test>
   </other>
  </test>
 </test>
</test>

T:\ftemp>type test.xsl
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/XSL/Transform/1.0">

<xsl:output method="xml"/>

<xsl:template match="text()"/>

<xsl:template match="/*">   <!--don't report document element-->
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="*">
  <xsl:text>Element </xsl:text>
  <xsl:value-of select="@attr"/>
  <xsl:text> has parent </xsl:text>
  <xsl:value-of select="name(..)"/>
  <xsl:text>, being number </xsl:text>
  <xsl:value-of select="count(ancestor::*[name(.)=name(current()/..)])"/>
  <xsl:text>.&#xa;</xsl:text>
  <xsl:apply-templates/>
</xsl:template>

</xsl:stylesheet>

T:\ftemp>call xsl test.xml test.xsl test.txt
T:\ftemp>type test.txt
Element a has parent test, being number 1.
Element b has parent test, being number 2.
Element c has parent test, being number 3.
Element d has parent other, being number 1.
Element e has parent test, being number 4.
Element f has parent test, being number 5.
Element g has parent other, being number 2.
Element h has parent test, being number 6.

T:\ftemp>


-- G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ Box 266, Kars, Ontario CANADA K0A-2E0 +1(613)489-0999 (Fax:-0995) Website: XSL/XML/DSSSL/SGML services, training, libraries, products. Practical Transformation Using XSLT and XPath ISBN 1-894049-01-2 Next instructor-led training: 1999-09-24, 1999-11-08, 1999-12-05/06, 1999-12-07, 2000-02-27/28, 2000-05-11/12



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



Current Thread
Keywords