Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why this XPATH query is not working?

Tags:

xpath

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.

like image 372
user837208 Avatar asked Mar 28 '12 05:03

user837208


People also ask

What is the meaning of '/' in XPath?

0 votes. Single Slash “/” – Single slash is used to create Xpath with absolute path i.e. the xpath would be created to start selection from the document node/start node.

What is the XPath query?

XPath (XML Path Language) is a query language that can be used to query data from XML documents. In RUEI, XPath queries can be used for content scanning of XML documents. A complete specification of XPath is available at http://www.w3.org/TR/xpath .

What does * indicate in XPath?

xpath=//tag[@attribute='value'] // : Select current node. tag: Tagname of the particular node. Also, "*" is for searching any tag in the xml structure.


1 Answers

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);
like image 162
marc_s Avatar answered Jan 01 '23 13:01

marc_s