具有名称空间的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屋!
查看全文