How to get fully qualified name of current node?

J

Jim Garrison

I know how to use the name() function to access the name of the
current node. How do I get the 'fully qualified' name, consisting
of the path from the root to the current node?

I.e.
<a>
<b>
<c>
</c>
</b>
</a>

When processing node c I need an xpath expression that returns
'a/b/c'. Is this available in XPath 1.0?

Jim Garrison
(e-mail address removed)
 
M

Martin Honnen

Jim said:
I know how to use the name() function to access the name of the
current node. How do I get the 'fully qualified' name, consisting
of the path from the root to the current node?

I.e.
<a>
<b>
<c>
</c>
</b>
</a>

When processing node c I need an xpath expression that returns
'a/b/c'. Is this available in XPath 1.0?

There is nothing like "the" fully qualified name in that way, of course
there are various XPath expressions selecting a single node but there is
nothing like "the XPath" to a node.
So you need to carefully define what kind of XPath you want to have and
create that programmatically, for instance with an XSLT stylesheet.
For that example for instance if anything of some precision is created
it could be
/a[1]/b[1]/c[1]
your expression
a/b/c
is much to generic unless you want some optimization when there is only
one child element.
Of course
/*[1]/*[1]/*[1]
is also a fitting XPath to get at that c element.
But as said, define carefully for yourself which results you want to
have and then create an XSLT stylesheet or other program walking a tree
model to create the XPath for you. Usually you do that by looking at the
number of preceding-sibling nodes of the same name and move up to the
parent node.
Or use a tool your favourite XML editor might provide.
 
P

Peter Flynn

Martin said:
There is nothing like "the" fully qualified name in that way, of course
there are various XPath expressions selecting a single node but there is
nothing like "the XPath" to a node.

True, but I think what he wanted was the parental location ladder back to
the root element:

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

<xsl:eek:utput method="text"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/">
<xsl:call-template name="parentage">
<xsl:with-param name="element" select="//c"/>
</xsl:call-template>
</xsl:template>

<xsl:template name="parentage">
<xsl:param name="trace"/>
<xsl:param name="element"/>
<xsl:choose>
<xsl:when test="count(/|$element)=1">
<xsl:value-of select="name($element)"/>
<xsl:text>/</xsl:text>
<xsl:value-of select="$trace"/>
</xsl:when>
<xsl:eek:therwise>
<xsl:call-template name="parentage">
<xsl:with-param name="element" select="$element/parent::*"/>
<xsl:with-param name="trace">
<xsl:value-of select="name($element)"/>
<xsl:text>/</xsl:text>
<xsl:value-of select="$trace"/>
</xsl:with-param>
</xsl:call-template>
</xsl:eek:therwise>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>

///Peter
 
J

Jim Garrison

Thanks for the response. I guess I'm surprised that XPath doesn't
provide a simple way to get the full path from the document root
without having to kludge it like this.

Or maybe I'm missing something?
 
P

Peter Flynn

Jim said:
Thanks for the response. I guess I'm surprised that XPath doesn't
provide a simple way to get the full path from the document root
without having to kludge it like this.

Or maybe I'm missing something?

No, it's just not very commonly asked for. But once written, you can
store the template in an external file and include it in any XSLT
program that needs it.

///Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top