Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

'Anonymous' Namespace foils XPath query 1

Status
Not open for further replies.

UKCoder

Programmer
Apr 20, 2008
2
GB
I have an XML document that looks like:
Code:
<?xml version="1.0" encoding="utf-8"?>
<root element>
[tab]<next element>
[tab][tab]<another element xmlns:xsd="http:something" xmlns:xs="http:somthing" xmlns="http:something">
[tab][tab][tab]<other elements />
[tab][tab][tab]<other elements />
[tab][tab][tab]<other elements />
[tab][tab][tab]....
[tab][tab]</another element>
[tab]</next element>
</root element>
The xmlns="http:something" is correct; there is no prefix.

Using XPath wildcards, can get all nodes returned. Using XPath expressions relating to nodes below the <another element> node, eg "//other elements" returns nothing. Removing the xmlns clauses causes the XPath query to work. I cannot change the document structure.

I want to use xmlDoc.SelectSingleNode("xpath query") or xmlDoc.SelectNodes("xpath query"). How can I create an XPath query that will ignore (or cope with) this 'anonymous' namespace?

Thanks for any help.
 
If you really don't care about the namespaces then you can use something like this:
Code:
 "/root/next/*[local-name()='another']/*[local-name()='other']"
The local-name() function returns the element name without the namespace attached.

Tom Morrison
 
Thanks for that tip - it looks like it's the bees knees! Your help appreciated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top