Re: ### Outputting the "full path". Is this possible ?? ###

Subject: Re: ### Outputting the "full path". Is this possible ?? ###
From: Jeni Tennison <Jeni.Tennison@xxxxxxxxxxxxxxxx>
Date: Thu, 25 May 2000 18:04:23 +0100

>Considering this xml:
>	<employees>
>		<name>Joe Shmo</name>
>	</employees>
>If the current node is "name", is there a simple XSL: way of outputting the
>"full path", ie:
>"departments/employees/name/Joe Shmo"

The 'full path' that you're describing are the names of the ancestor
elements of the current node, plus the current node, plus the current
node's value, separated by '/'s.

There are two ways of doing this: iteration and recursion.  I'm going to
show you  iteration because it keeps everything in one place.  You do
iteration using an xsl:for-each element.  We want to iterate over the list
of the ancestor elements of the current node, but we also want to include
the current node, and helpfully there is an XPath axis that enables us to
do just that: ancestor-of-self.  We don't care what the ancestor elements
are called.  So:

<xsl:for-each select="ancestor-of-self::*">

OK, so for each of those nodes we want to output the name of the node,
followed by a forward shash and no whitespace:

<xsl:for-each select="ancestor-of-self::*">
  <xsl:value-of select="name()" /><xsl:text>/</xsl:text>

Once we've outputted the information about the ancestor elements, we want
to round it off with information about the value of the current node:

<xsl:value-of select="." />

So, the complete template is:

<xsl:template match="name">
  <xsl:for-each select="ancestor-of-self::*">
    <xsl:value-of select="name()" /><xsl:text>/</xsl:text>
  <xsl:value-of select="." />

Easy, eh?

Hope that helps,


Dr Jeni Tennison
Epistemics Ltd, Strelley Hall, Nottingham, NG8 6PE
Telephone 0115 9061301 ? Fax 0115 9061304 ? Email

 XSL-List info and archive:

Current Thread