473,397 Members | 1,969 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,397 software developers and data experts.

XPath and default (overridden) namespaces

Hi,

I need some help on XPath ... what's the expressions to get:

1. All "Employee" nodes?
2. Only "Employee" nodes from "urn:test1" namespace?

The document is shown below.

<?xml version="1.0" standalone="yes"?>
<NorthwindEmployees>
<Employee xmlns="urn:test1">
<employeeid>1</employeeid>
<lastname>Davolio</lastname>
<firstname>Nancy</firstname>
<title>Sales Representative</title>
</Employee>
<Employee xmlns="urn:test2">
<employeeid>2</employeeid>
<lastname>Fuller</lastname>
<firstname>Andrew</firstname>
<title>Vice President, Sales</title>
</Employee>
<Employee>
<employeeid>3</employeeid>
<lastname>Leverling</lastname>
<firstname>Janet</firstname>
<title>Sales Representative</title>
</Employee>
</NorthwindEmployees>

I'm only able to get the "Employee" nodes from default namespace by using:

//NorthwindEmployees/Employee

It returns Employee 3.
Rgd,
Peter Theill
Nov 12 '05 #1
3 1198
Peter Theill wrote:
I need some help on XPath ... what's the expressions to get:

1. All "Employee" nodes?
You should realize that namespace is integral part of an element name,
so "All "Employee" nodes" actually makes little sense. You mean all
elements with local name "Employee"? Then use
/NorthwindEmployees/*[local-name='Employee']
2. Only "Employee" nodes from "urn:test1" namespace?


/NorthwindEmployees/foo:Employee
where foo prefix is bound to "urn:test1" namespace.

Or (usually machine-generated form)

/NorthwindEmployees/*[local-name='Employee' and namespace-uri()='urn:test1']

PS. Make sure you read "XML Namespaces and How They Affect XPath and
XSLT" at
http://msdn.microsoft.com/library/de...ml05202002.asp

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #2
> You should realize that namespace is integral part of an element name,
so "All "Employee" nodes" actually makes little sense. You mean all
elements with local name "Employee"?


Thank you very much, this was exactly my problem. I didn't realize :-)

Thanks for the article you pointed me too - it was helpful.

Rgd,
Peter
Nov 12 '05 #3
Oleg Tkachenko [MVP] wrote:
You should realize that namespace is integral part of an element name,
so "All "Employee" nodes" actually makes little sense. You mean all
elements with local name "Employee"? Then use
/NorthwindEmployees/*[local-name='Employee']
I meant

/NorthwindEmployees/*[local-name()='Employee']
2. Only "Employee" nodes from "urn:test1" namespace?

/NorthwindEmployees/foo:Employee
where foo prefix is bound to "urn:test1" namespace.

Or (usually machine-generated form)

/NorthwindEmployees/*[local-name='Employee' and
namespace-uri()='urn:test1']


/NorthwindEmployees/*[local-name()='Employee' and
namespace-uri()='urn:test1']

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: gimme_this_gimme_that | last post by:
I have the following xml file and need some tips on xpath queries to fetch the data. I think I'm not handling namespaces correctly. Here is xml file : <?xml version="1.0"...
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...
3
by: Patrick | last post by:
Dear Ng, after I have loaded am XmlDataDocument file, I try to extract a single node via XmlElement myElem = (XmlElement)this.XmlDataDocument.SelectSingleNode("/Document/Headerdata/myElem");...
5
by: David Thielen | last post by:
Hi; I set up my xml as follows: XmlDocument xml = new XmlDocument(); xml.Load(File.Open("data.xml", FileMode.Open, FileAccess.Read)); XmlNamespaceManager context = new...
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>...
3
by: Jason Mobarak | last post by:
Hello -- I'm attempting to get a handle on how to do xpath queries with System.Xml -- so far the biggest hurdle has been how to deal with a default namespace. If I use the test xml: <?xml...
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.