473,226 Members | 1,433 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,226 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 3063
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>
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.