为什么这个XPATH查询不起作用?

时间:2012-03-28 05:06:34

标签: xpath

我的XML文档看起来像this

当我运行XPATH查询//collected_objects时,我没有选择任何节点集。我究竟做错了什么?我想选择整个gather_objects节点。

1 个答案:

答案 0 :(得分: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);
相关问题