我有这个带有命名空间的 XML 文档,我想使用 XPath 提取一些节点。
这是文档:
<ArrayOfAnyType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
<anyType xsi:type="Document">
<Id>5</Id>
<Title>T1</Title>
</anyType>
<anyType xsi:type="Document">
<Id>15</Id>
<Title>T15</Title>
</anyType>
</ArrayOfAnyType>
如果我想用 xsi:type="Document"提取所有“anyType”元素,XPath 表达式应该是什么?
我已经试过了:
//anyType[@xsi:type="Document"]
它不起作用:
最佳答案
如果您使用的是 C#,则需要在 XPath 中为“anyType”元素指定命名空间:
var xml = new XmlDocument();
xml.LoadXml( "your xml" );
var names = new XmlNamespaceManager( xml.NameTable );
names.AddNamespace( "xsi", "http://www.w3.org/2001/XMLSchema-instance" );
names.AddNamespace( "a", "http://tempuri.org/" );
var nodes = xml.SelectNodes( "//a:anyType[@xsi:type='Document']", names );
关于xml - 带有命名空间的 XML 文档的 XPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/561822/