XPath: Problem with namespaces

Discussion in 'XML' started by reyesvsn@gmail.com, Oct 20, 2008.

  1. Guest

    Hello everybody,

    I have a question concerning XPath expressions and namespaces.
    Consider this XML:

    <?xml version="1.0" encoding="UTF-8"?>
    <newsMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:rtr="http://www.reuters.com/ns/2003/08/content"
    xmlns="http://iptc.org/std/nar/2006-10-01/">
    <header>
    <transmitId>LWN_2008-07-26_892</transmitId>
    <priority>4</priority>
    <channel>VID</channel>
    </header>
    <itemSet>
    <!-- ========================================================= -->
    <!-- PRIMARY ITEM : NEP -->
    <packageItem standard="NewsML-G2" standardversion="2.1"
    conformance="power" guid="tag:reuters.com,2008:newsml_WNEI6QNH"
    version="2" xml:lang="en">
    <!-- ========================================================= -->
    <itemMeta>
    <itemClass qcode="icls:composite"/>
    <versionCreated>2008-07-26T15:55:37Z</versionCreated>
    <fileName>20080726954SP-FRANCE-EVIAN_GOLF.xml</fileName>
    <link rel="irel:prevVersion" residref="tag:reuters.com,
    2008:newsml_WNEI6QNH" version="1" contenttype="application/
    vnd.iptc.g2.packageitem+xml" rtr:id="WNEI6QNH" rtr:type="idType:USN"/>
    <rtr:versionedId guid="tag:reuters.com,2008:newsml_WNEI6QNH:2"/>
    </itemMeta>
    </packageItem>
    <!-- ========================================================= -->
    <!-- PRIMARY ITEM : SNI-Text -->
    <newsItem standard="NewsML-G2" standardversion="2.1"
    conformance="power" guid="tag:reuters.com,2008:newsml_WNEI6QNI"
    version="2" xml:lang="en">
    <itemMeta>
    <itemClass qcode="icls:text"/>
    <versionCreated>2008-07-26T15:55:37Z</versionCreated>
    <fileName>20080726954SP-FRANCE-EVIAN_GOLF.xml</fileName>
    <link rel="irel:prevVersion" residref="tag:reuters.com,
    2008:newsml_WNEI6QNI" version="1" contenttype="application/
    vnd.iptc.g2.newsitem+xml" rtr:id="WNEI6QNI" rtr:type="idType:USN"/>
    <rtr:versionedId guid="tag:reuters.com,2008:newsml_WNEI6QNI:2"/>
    </itemMeta>
    </newsItem>
    <!-- ========================================================= -->
    <!-- PRIMARY ITEM : SNI -Video -->
    <newsItem standard="NewsML-G2" standardversion="2.1"
    conformance="power" guid="tag:reuters.com,2008:newsml_LWNEI6Q1CDHNJ"
    version="1" xml:lang="en">
    <itemMeta>
    <itemClass qcode="icls:video"/>
    <versionCreated>2008-07-26T15:55:37Z</versionCreated>
    <rtr:versionedId guid="tag:reuters.com,2008:newsml_LWNEI6Q1CDHNJ:
    1"/>
    </itemMeta>
    </newsItem>
    </itemSet>
    </newsMessage>



    If the "newsMessage" node has the three namespaces (xmlns:xsi,
    xmlns:rtr & xmlns), my XPath expressions like '/newsMessage/header/
    priority/text()' returns nothing. But if I delete the third namespace
    (xmlns), all my XPath expressions find the nodes and return the text
    content except the expression '/newsMessage/itemSet/packageItem/
    itemMeta/rtr:versionedId/@guid' that returns the error: 'Reference to
    undeclared namespace prefix: 'rtr'.'

    Any ideas about the problem?

    Best regards
    Reyes
    , Oct 20, 2008
    #1
    1. Advertising

  2. wrote:

    > <newsMessage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    > xmlns:rtr="http://www.reuters.com/ns/2003/08/content"
    > xmlns="http://iptc.org/std/nar/2006-10-01/">



    > If the "newsMessage" node has the three namespaces (xmlns:xsi,
    > xmlns:rtr & xmlns), my XPath expressions like '/newsMessage/header/
    > priority/text()' returns nothing. But if I delete the third namespace
    > (xmlns), all my XPath expressions find the nodes and return the text
    > content except the expression '/newsMessage/itemSet/packageItem/
    > itemMeta/rtr:versionedId/@guid' that returns the error: 'Reference to
    > undeclared namespace prefix: 'rtr'.'


    For XPath 1.0 to select elements in a namespace you need to bind a
    prefix to the namespace URI and use that prefix in your XPath
    expressions. How you do that depends on the API of the XPath engine you
    use (respectively on the host language you use XPath with like XSLT).
    See http://www.faqts.com/knowledge_base/view.phtml/aid/34022/fid/1753


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Oct 20, 2008
    #2
    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. Alastair Cameron
    Replies:
    1
    Views:
    7,400
    SQL Server Development Team [MSFT]
    Jul 8, 2003
  2. Jim
    Replies:
    2
    Views:
    449
    Richard Tobin
    Jan 29, 2004
  3. kj
    Replies:
    3
    Views:
    22,581
    sandhya
    Mar 27, 2010
  4. Stefan Franke

    XPath and namespaces...

    Stefan Franke, Jan 2, 2005, in forum: XML
    Replies:
    6
    Views:
    1,076
    Martin Honnen
    Jan 3, 2005
  5. Replies:
    1
    Views:
    560
Loading...

Share This Page