2012-03-28 90 views
5

我的XML文檔看起來像this爲什麼這個XPATH查詢不起作用?

當我運行XPath查詢//collected_objects,我沒有得到任何選擇節點集。我究竟做錯了什麼?我想選擇整個collect_objects節點。

+0

@marc_s,它似乎應該進入一個答案,所以這個問題可以被標記爲回答... – 2012-03-29 17:29:30

回答

7

因爲你的XML文檔有一個定義的XML名稱空間<oval_system_characteristics xmlns="http://oval.mitre.org/XMLSchema/oval-system-characteristics-5") - 你需要在你的查詢中包含它!

你如何做到這一點取決於你使用的是什麼系統/編程語言。在.NET/C#中,你可以這樣做:

// 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);