XPath: Problem with namespaces

R

reyesvsn

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
 
M

Martin Honnen

<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
 

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,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top