Groups | Blog | Home
all groups > dotnet xml > august 2004 >

dotnet xml : XPath and default (overridden) namespaces


Peter Theill
8/26/2004 10:11:28 AM
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

Peter Theill
8/26/2004 1:25:37 PM
[quoted text, click to view]

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

Oleg Tkachenko [MVP]
8/26/2004 1:33:29 PM
[quoted text, click to view]

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']

[quoted text, click to view]

/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/default.asp?url=/library/en-us/dnexxml/html/xml05202002.asp

--
Oleg Tkachenko [XML MVP]
Oleg Tkachenko [MVP]
8/26/2004 3:19:52 PM
[quoted text, click to view]

I meant

/NorthwindEmployees/*[local-name()='Employee']

[quoted text, click to view]

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

--
Oleg Tkachenko [XML MVP]
AddThis Social Bookmark Button