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

Re: XSL/XPath to generate a list of ancestors?

Subject: Re: XSL/XPath to generate a list of ancestors?
From: "Mukul Gandhi" <gandhi.mukul@xxxxxxxxx>
Date: Sun, 18 May 2008 00:21:46 +0530
Re:  XSL/XPath to generate a list of ancestors?
I think this could be simple to solve with XSLT 2.0. Below is the
working stylesheet:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                       version="2.0">

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

<xsl:template match="/">
  <result>
    <xsl:apply-templates select="//*[@name][not(@name = 'root')]" />
  </result>
</xsl:template>

<xsl:template match="*">
  <fullName>
    <xsl:value-of select="string-join(ancestor-or-self::*[not(@name =
'root')]/@name,'.')" />
  </fullName>
</xsl:template>

</xsl:stylesheet>

This when applied to the given XML, produces output:

<?xml version="1.0" encoding="UTF-8"?>
<result>
   <fullName>a1</fullName>
   <fullName>a1.a2</fullName>
   <fullName>a1.a2.a3</fullName>
   <fullName>b1</fullName>
   <fullName>b1.b2</fullName>
</result>

On 5/12/08, Nathan Potter <ndp@xxxxxxxxxxxxxxxxxxxx> wrote:
>
> I need to concatenate the "name" attributes of all of the parents for each
> element. All I could figure out was to use a recursive template. Is there a
> more straightforward way to accomplish this?
>
>
>
> XML:
>
> <Dataset name="root">
>    <A name="a1">
>        <A name="a2">
>            <A name="a3" />
>        </A>
>    </A>
>    <B name="b1">
>        <B name="b2"/>
>    </B>
> </Dataset>
>
> Desired output:
>
> <fullName>a1</fullName>
> <fullName>a1.a2</fullName>
> <fullName>a1.a2.a3</fullName>
>
> <fullName>b1</fullName>
> <fullName>b1.b2</fullName>


-- 
Regards,
Mukul Gandhi

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.