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

Re: Printing ancestor elements of all element nodes

Subject: Re: Printing ancestor elements of all element nodes
From: Mukul Gandhi <gandhi.mukul@xxxxxxxxx>
Date: Thu, 4 Aug 2005 13:11:53 +0530
xsl print element name
This is also working (I corrected my original approach). I am happy
with the responses so far..

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="html" indent="yes"/>

  <xsl:template match="/">
    <xsl:for-each select="//*">
       Element <xsl:value-of select="name()" /><xsl:text> -
&#xa;&#xa;</xsl:text>
       Ancestors of the element are <xsl:text>&#xa;</xsl:text>
       <xsl:call-template name="printAncestors">
          <xsl:with-param name="node" select="." />
       </xsl:call-template>
    </xsl:for-each>
  </xsl:template>

  <xsl:template name="printAncestors">
    <xsl:param name="node" />

    <xsl:choose>
      <xsl:when test="($node/parent::*[1] or
$node/parent::*[1]/parent::*[1])">
        <xsl:value-of select="name($node/parent::*[1])"
/><xsl:text>,</xsl:text>
        <xsl:call-template name="printAncestors">
	  <xsl:with-param name="node" select="$node/parent::*[1]" />
        </xsl:call-template>
      </xsl:when>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>

Regards,
Mukul

On 8/3/05, Mukul Gandhi <gandhi.mukul@xxxxxxxxx> wrote:
> Thank you David! This is marvellous..  My attempt was -
>
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
>
>    <xsl:output method="text" />
>
>    <xsl:template match="/">
>       <xsl:for-each select="//*">
>          Element name <xsl:value-of select="name()"
> /><xsl:text>&#xa;</xsl:text>
>          Ancestor elements of this element
>          <xsl:text>&#xa;</xsl:text>
>          <xsl:text>&#xa;</xsl:text>
>          <xsl:call-template name="printAncestors">
>              <xsl:with-param name="node" select="." />
>          </xsl:call-template>
>       </xsl:for-each>
>    </xsl:template>
>
>    <xsl:template name="printAncestors">
>       <xsl:param name="node" />
>
>       <xsl:choose>
>                        <xsl:when test="$node/parent::*">
>                <xsl:value-of select="name($node)" />,
>                <xsl:call-template name="printAncestors">
>                   <xsl:with-param name="node"
> select="$node/ancestor::*"/>
>                </xsl:call-template>
>                        </xsl:when>
>                        <xsl:otherwise>
>               <xsl:value-of select="name($node)" />,
>                        </xsl:otherwise>
>           </xsl:choose>
>    </xsl:template>
>
> </xsl:stylesheet>
>
> But this did'nt work.. ;)
>
> THANK YOU.
>
> Regards,
> Mukul
>
> On 8/3/05, David Carlisle <davidc@xxxxxxxxx> wrote:
> >
> > <xsl:for-each select="//*">
> >  <xsl:text>&#10;</xsl:text>
> >  <xsl:value-of select="name()"/>
> >  <xsl:text> - </xsl:text>
> >  <xsl:for-each select="ancestor::*">
> >  <xsl:sort select="-count(ancestor::*)"/>
> >  <xsl:value-of select="name()"/>
> >  <xsl:if tets="position()!=last()">,</xsl:if>
> >  </xsl:for-each>
> >  </xsl:for-each>
> >
> > ________________________________________________________________________
> > This e-mail has been scanned for all viruses by Star. The
> > service is powered by MessageLabs. For more information on a proactive
> > anti-virus service working around the clock, around the globe, visit:
> > http://www.star.net.uk
> > ________________________________________________________________________

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.