How to get fully qualified name of current node?

Discussion in 'XML' started by Jim Garrison, Oct 24, 2005.

  1. Jim Garrison

    Jim Garrison Guest

    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
     
    Jim Garrison, Oct 24, 2005
    #1
    1. Advertising

  2. Jim Garrison wrote:

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

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Oct 24, 2005
    #2
    1. Advertising

  3. Jim Garrison

    Peter Flynn Guest

    Martin Honnen wrote:

    > Jim Garrison wrote:
    >
    >> 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.


    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
     
    Peter Flynn, Oct 24, 2005
    #3
  4. Jim Garrison

    Jim Garrison Guest

    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?

    Peter Flynn wrote:
    > Martin Honnen wrote:
    >
    >
    > True, but I think what he wanted was the parental location ladder back to
    > the root element:
     
    Jim Garrison, Oct 27, 2005
    #4
  5. Jim Garrison

    Peter Flynn Guest

    Jim Garrison wrote:

    > 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
     
    Peter Flynn, Oct 27, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Frank Drebin

    Re: fully qualified domain name

    Frank Drebin, Aug 26, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    592
    Frank Drebin
    Aug 26, 2003
  2. Alexander Grigoriev
    Replies:
    0
    Views:
    393
    Alexander Grigoriev
    Sep 12, 2003
  3. Jim Wallace
    Replies:
    1
    Views:
    325
    Kamilche
    Feb 14, 2005
  4. Replies:
    2
    Views:
    628
  5. patrimith
    Replies:
    2
    Views:
    736
    Goldfish
    Oct 11, 2007
Loading...

Share This Page