[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

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
xsl parent element
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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.