具有命名空间的 XML 文档上的 XPath [英] XPath on an XML document with namespace
本文介绍了具有命名空间的 XML 文档上的 XPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个带有命名空间的 XML 文档,我想使用 XPath 提取一些节点.
I'm having this XML document with namespaces and I want to extract some nodes using 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 表达式会是什么?
What's the XPath expression going to be if I want to extract all "anyType" elements with xsi:type="Document"?
我试过这个:
//anyType[@xsi:type="Document"]
它不起作用:
推荐答案
如果您使用的是 C#,那么您需要在 XPath 中为anyType"元素指定命名空间:
If you are using C# then you need to specify the namespace for the "anyType" element in your XPath:
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 文档上的 XPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文