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

Subject: Re: [xsl] XSL/XPath to generate a list of ancestors?
From: "Mukul Gandhi" <gandhi.mukul@xxxxxxxxx>
Date: Sun, 18 May 2008 00:21:46 +0530
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