Help with simple XPATH

Discussion in 'XML' started by Monty, Dec 30, 2006.

  1. Monty

    Monty Guest

    Despite reading posts in Google, I don't understand XPATH. Can someone
    help me write an XPATH. From Google I think my problem is that the
    default namespace does not have a prefix. I can't change this as I have
    received this XML and I didn't create it. All I want to is retrieve the
    PROJECTNAME from the following XML. I am typing this XML and XPATH into
    this site

    http://www.activsoftware.com/xml/xpath/

    The XPATH that does not work is /PROJECTS/PROJECTNAME.

    Thank you
    Monty

    <PROJECTS xmlns="http://www.companyname.com/project"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <PROJECTNAME>
    Project A
    </PROJECTNAME>
    <PROJECTBUDGET>
    27000
    </PROJECTBUDGET>
    </PROJECTS>
     
    Monty, Dec 30, 2006
    #1
    1. Advertising

  2. Monty wrote:

    > The XPATH that does not work is /PROJECTS/PROJECTNAME.


    > <PROJECTS xmlns="http://www.companyname.com/project"
    > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    > <PROJECTNAME>
    > Project A
    > </PROJECTNAME>



    Those elements are in the namespace http://www.companyname.com/project
    so for XPath to qualify element names in your expression you need to
    bind a prefix to the namespace URI. Check your XPath API on how to do
    that exactly, you can choose any prefix you like and you don't have to
    change the XML source document, you just need to make sure your XPath
    API knows that e.g. 'pf' is bound to http://www.companyname.com/project
    and then you can use expressions alike
    /pf:pROJECTS/pf:pROJECTNAME

    Or as an alternative you could use e.g.
    /*[namespace-uri() = 'http://www.companyname.com/project' and
    local-name() = 'PROJECTS']/*[namespace-uri() =
    'http://www.companyname.com/project' and local-name() = 'PROJECTNAME']


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Dec 30, 2006
    #2
    1. Advertising

  3. Monty

    Myron Turner Guest

    Monty wrote:
    > Despite reading posts in Google, I don't understand XPATH. Can someone
    > help me write an XPATH. From Google I think my problem is that the
    > default namespace does not have a prefix. I can't change this as I have
    > received this XML and I didn't create it. All I want to is retrieve the
    > PROJECTNAME from the following XML. I am typing this XML and XPATH into
    > this site
    >
    > http://www.activsoftware.com/xml/xpath/
    >
    > The XPATH that does not work is /PROJECTS/PROJECTNAME.
    >
    > Thank you
    > Monty
    >
    > <PROJECTS xmlns="http://www.companyname.com/project"
    > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    > <PROJECTNAME>
    > Project A
    > </PROJECTNAME>
    > <PROJECTBUDGET>
    > 27000
    > </PROJECTBUDGET>
    > </PROJECTS>
    >


    You have to supply namespace prefixes in xsl/xpath for default
    namespaces in xml. It took a bit of fussing, but here's a solution:

    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://www.w3.org/1999/xhtml" >


    <xsl:template xmlns:pn="http://www.companyname.com/project" match="/">
    <xsl:apply-templates select="pn:pROJECTS/pn:pROJECTNAME" />
    <xsl:apply-templates select="pn:pROJECTS/pn:pROJECTBUDGET" />
    </xsl:template>

    <xsl:template xmlns:pn="http://www.companyname.com/project"
    match="pn:pROJECTNAME">
    Project Name: <xsl:value-of select = "." /><br />
    </xsl:template>

    <xsl:template xmlns:pn="http://www.companyname.com/project"
    match="pn:pROJECTBUDGET">
    Budget: <xsl:value-of select = "." />
    </xsl:template>

    </xsl:stylesheet>

    --

    _____________________
    Myron Turner
    http://www.room535.org
    http://www.bstatzero.org
    http://www.mturner.org/XML_PullParser/
     
    Myron Turner, Jan 5, 2007
    #3
    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. Marvin_123456

    "Memory leak" in javax.xml.xpath.XPath

    Marvin_123456, Jul 29, 2005, in forum: Java
    Replies:
    4
    Views:
    2,010
    jan V
    Jul 29, 2005
  2. Alastair Cameron
    Replies:
    1
    Views:
    7,482
    SQL Server Development Team [MSFT]
    Jul 8, 2003
  3. Anna
    Replies:
    0
    Views:
    555
  4. goog
    Replies:
    0
    Views:
    527
  5. Tjerk Wolterink

    XPath: efficiency in xpath expressions

    Tjerk Wolterink, Nov 13, 2004, in forum: XML
    Replies:
    1
    Views:
    1,681
    Richard Tobin
    Nov 13, 2004
Loading...

Share This Page