问题描述
什么是命名空间XPath的背后的故事和支持?做的XPath作为规范precede命名空间?如果我有各种元素都被赋予了默认命名空间的文档:
What is the story behind XPath and support for namespaces? Did XPath as a specification precede namespaces? If I have a document where elements have been given a default namespace:
<foo xmlns="uri" />
看来好像有些XPath处理库将无法识别 //富
命名空间,而其他的,因为会。我们小组已将想过的选择是使用常规的前pressions的XPath来增加一个命名空间的preFIX(你可以添加通过XmlNameTable一个名称空间preFIX),但由于XPath是这样一种灵活的语言,这似乎脆当涉及到节点测试。
It appears as though some of the XPath processor libraries won't recognize //foo
because of the namespace whereas others will. The option my team has thought about is to add a namespace prefix using regular expressions to the XPath (you can add a namespace prefix via XmlNameTable) but this seems brittle since XPath is such a flexible language when it comes to node tests.
是否有适用于本标准?
我的做法是一个有点hackish但似乎很好地工作;我删除的xmlns
声明用查找/替换,然后应用的XPath。
My approach is a bit hackish but it seems to work fine; I remove the xmlns
declaration with a search/replace and then apply XPath.
string readyForXpath = Regex.Replace(xmldocument, "xmlns=\".+\"", String.Empty );
这是一个公平的做法或有没有人解决了这个不同?
Is that a fair approach or has anyone solved this differently?
推荐答案
我试过类似palehorse提出什么样的东西,不能让它的工作。因为我是从一个发布的服务中获取数据我不能改变的XML。我结束了使用的XmlDocument和XmlNamespaceManager的,像这样:
I tried something similar to what palehorse proposed and could not get it to work. Since I was getting data from a published service I couldn't change the xml. I ended up using XmlDocument and XmlNamespaceManager like so:
XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlWithBogusNamespace);
XmlNamespaceManager nSpace = new XmlNamespaceManager(doc.NameTable);
nSpace.AddNamespace("myNs", "http://theirUri");
XmlNodeList nodes = doc.SelectNodes("//myNs:NodesIWant",nSpace);
//etc
这篇关于的XPath和默认命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!