473,387 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 3076
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Piet | last post by:
Hello, Via Xpath, I want to access nodes which have a namespace prefix. THe document at hand is an Xsl-FO document. I tried the following: from xml.dom import minidom from xml.xpath import...
3
by: Colin Green | last post by:
I have come across what seems like a failing in the .Net XML classes. Many people have posted requesting how to write an XPath query with namespace prefixes that works. The solution shown in all...
1
by: Hollywood | last post by:
I have the following XSD created in VS.NET 2003: <?xml version="1.0" encoding="utf-8" ?> <xs:schema id="ReferralSchama" targetNamespace="http://test.org/Referral"...
6
by: Chua Wen Ching | last post by:
Hi there, I had this xml file with me (not yet consider implementing xml namespaces yet). <?xml version='1.0'?> <Object> <Windows> <EID>1</EID> <EDesc>Error 1</EDesc> </Windows>
3
by: Goran Djuranovic | last post by:
Hi All, Does anyone know how to retreive deepest XPath value from XML document by using VB.NET? For example, if I had an XML file like this: <Root> <Customer> <Name>MyName</Name> </Customer>...
6
by: AMDRIT | last post by:
Hello Everyone, I am having an issue with xml and namespaces, at least I think it is namespaces. When I use namespaces, I cannot use SelectSingleNode / SelectNodes as they always return...
14
by: Mat| | last post by:
Hello :-) I am learning XPath, and I am trying to get child nodes of a node whose names do *not* match a given string, e.g : <dummy> <example> <title>Example 1</title> <body>this is an...
6
by: J.Marsch | last post by:
I must be completely losing my mind. I have some code that writes to config files. It works great with app.config files, but fails miserably with web.config files. For the life of me, I cannot...
14
by: Mikhail Teterin | last post by:
Hello! What's would be the syntax for a query, which would allow me to get only the elements with non-empty text-nodes? For example, from: <a><b></b></a> <c/> <d><e>meow</e></d>
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.