I have read article after article and post after post about how to deal with namespaces. After trying to strip them, include them, and even write my own XML program from scratch in Visual Studio I am just going back to the basics. How in the world do you write XPath for namespaced XML Files?
I am using this site: https://codebeautify.org/Xpath-Tester
Here is the query that I want to make
/ExportedObjects/ExportedObject[ObjectValue/@superClasses="Document"]
Based on every post regarding documents which contain namespaces I must add extra syntax: ExportedObjects
becomes *[name()='ExportedObjects']
and so on for each element.
Well I get this far without issue
/*[name()='ExportedObjects']/*[name()='ExportedObject']
But when I want to add search criteria, I get no results. Why is it that the namespaced syntax does not work in the where clause?
/*[name()='ExportedObjects']/*[name()='ExportedObject']/[*[name()='ObjectValue']/@superClasses="Document"]
Example of XML
<?xml version="1.0" encoding="UTF-8"?><ExportedObjects xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:e="http://www.w3.org/2003/05/soap-envelope" xmlns:xop="http://www.w3.org/2004/08/xop/include" xmlns:wn0="http://www.filenet.com/ns/fnce/2006/11/ws/schema" xmlns:wn1="http://schemas.xmlsoap.org/ws/2002/04/reference/" xmlns:wn2="http://www.filenet.com/ns/fnce/2006/11/ws/MTOM/schema" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.filenet.com/ns/fnce/2006/11/ws/schema">
<ExportedObject><ObjectValue superClasses="Document">Hello World</ObjectValue></ExportedObject></ExportedObjects>
CodePudding user response:
Just an educated guess: your XPath is not valid because of a wrong pair of brackets. So it should probably look like
/*[name()='ExportedObjects']/*[name()='ExportedObject']/*[name()='ObjectValue' and @superClasses='Document']
Maybe this helps you.
CodePudding user response:
Use local-name()
not name()
. The result of name()
includes an unpredictable prefix.