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

Subject: Re: ### Outputting the "full path". Is this possible ?? ###
From: "Khalid Asad" <asad@xxxxxxxxxxxxx>
Date: Fri, 26 May 2000 07:14:25 -0400
Interesting that this keeps coming up. I asked this questions some time ago
and I found out that there were multiple ways of getting the full path (none
which were complete). The technique described here works for simple cases,
but what if an element contains a sequence of elements, such as

 <name>Joe Shmo</name>
 <name>Bob Shmo</name>

Also, how about getting the full path for attributes?

I would like to see better support from processors for outputting XPATH
strings. Using SAX, when I get an element event, I'd like to see the path to
that element in the document - it makes it easier for me to update the

I'd like an XSLT processor to have a function to generate an XPATH string
for nodes in the tree, rather than me having to write one. In fact, if there
was a standard function to do this, then all XSLT processor providers would
end up supporting it and XSL stylesheet writers would not be burdened with
trying to figure out how to get it right.


----- Original Message -----
From: "Jeni Tennison" <Jeni.Tennison@xxxxxxxxxxxxxxxx>
To: "Jonathan Asbell" <jonathan.asbell@xxxxxxxxxxxxxx>
Cc: <xsl-list@xxxxxxxxxxxxxxxx>
Sent: Thursday, May 25, 2000 1:04 PM
Subject: Re: ### Outputting the "full path". Is this possible ?? ###


>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:

 XSL-List info and archive:

Current Thread