user837208
user837208

Reputation: 2577

why this XPATH query is not working?

My XML document looks like this

When I run XPATH query //collected_objects, I don't get any nodeset selected. What am I doing wrong? I want to select the whole collected_objects node.

Upvotes: 5

Views: 3115

Answers (1)

marc_s
marc_s

Reputation: 754220

Because your XML document has a XML namespace defined (<oval_system_characteristics xmlns="http://oval.mitre.org/XMLSchema/oval-system-characteristics-5") - you need to include that in your query!

How you can do this depends on what system/programming language you're using. In .NET / C#, you could do this something like this:

// create XmlDocument and load XML file
XmlDocument doc = new XmlDocument();
doc.Load(yourXmlFileNameHere);

// define XML namespace manager and a prefix for the XML namespace used
XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable);
mgr.AddNamespace("ns", "http://oval.mitre.org/XMLSchema/oval-system-characteristics-5");

// get list of nodes, based on XPath - using the XML namespace manager
XmlNodeList list = doc.SelectNodes("//ns:collected_objects", mgr);

Upvotes: 7

Related Questions