471,863 Members | 1,117 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,863 software developers and data experts.

XPath: Problem with namespaces

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 '08 #1
1 2971
re******@gmail.com 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/...34022/fid/1753
--

Martin Honnen
http://JavaScript.FAQTs.com/
Oct 20 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Piet | last post: by
3 posts views Thread by Colin Green | last post: by
1 post views Thread by Hollywood | last post: by
6 posts views Thread by AMDRIT | last post: by
14 posts views Thread by Mat| | last post: by
6 posts views Thread by J.Marsch | last post: by
14 posts views Thread by Mikhail Teterin | last post: by
NeoPa
reply views Thread by NeoPa | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.