How to ignore namespaces with XPath

My goal is to extract specific nodes from multiple XML files with multiple namespaces using XPath. Everything works fine as long as I know the namespace URI. The namespace name itself remains constant, but the schemes (XSD) are sometimes generated by the client, that is, they are unknown to me. Then I remain basically three options:

  • use only one schema for the namespace, hoping that nothing will work (can I be sure?)

  • get the child nodes of the document and look for the first node with the namespace URI, hoping that it is there, and just use the URI, hoping that it is correct. may go wrong for several reasons

  • somehow say xpath: "Look, I don't care about namespaces, just find ALL nodes with that name, I can even name the namespace, not the URI." And here is the question ...

This is not a repetition of the numerous “my xpath expression does not work because I am not aware of namespace awareness”, as found here or. I know how to use namespace understanding. Just not how to get rid of it.

+95
xml namespaces xpath xml-namespaces
Dec 14 2018-10-14
source share
4 answers

You can use the local-name() XPath function. Instead of choosing node as

 /path/to/x:somenode 

you can select all nodes and filter for one with the correct local name:

 /path/to/*[local-name() = 'somenode'] 
+143
Dec 14 '10 at 15:05
source share
— -

You can do the same in XPath2.0 in a less detailed syntax:

 /path/to/*:somenode 
+16
Feb 21 '17 at 8:41
source share

You can use Namespace = false in XmlTextReader

 [TestMethod] public void MyTestMethod() { string _withXmlns = @"<?xml version=""1.0"" encoding=""utf-8""?> <ParentTag xmlns=""http://anyNamespace.com""> <Identification value=""ID123456"" /> </ParentTag> "; var xmlReader = new XmlTextReader(new MemoryStream(Encoding.Default.GetBytes(_withXmlns))); xmlReader.Namespaces = false; var content = XElement.Load(xmlReader); XElement elem = content.XPathSelectElement("/Identification"); elem.Should().NotBeNull(); elem.Attribute("value").Value.Should().Be("ID123456"); } 

via:

 using System; using System.IO; using System.Linq; using System.Text; using System.Xml; using System.Xml.Linq; using System.Xml.XPath; using FluentAssertions; using Microsoft.VisualStudio.TestTools.UnitTesting; 
+1
Mar 19 '18 at 10:54
source share

I am not allowed to comment (!!), so I can just let them shut me up or in any case add my two cents using the "reply to comment" ...

I found that this hack has another drawback: it does not work if the document declares any XML namespace prefixes (the colon is not valid in XML names, and when using namespaces == false the reader interprets the colon as part of the name) for example

 <ParentTag xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://anyNamespace.com"> 
0
Aug 27 '19 at 6:48
source share



All Articles