Select node without namespace with XPath

不打扰是莪最后的温柔 提交于 2019-11-28 03:25:43

问题


I have a xml like

<root xmlns:ns1="http://foo">
    <ns1:child1>Text</ns1:child1>
    <ns1:child2>Number</ns1:child2>
</root>

Now I get this from different persons, so that for example person 2 sends me another message with the same structure like

<root xmlns:anotherNs="http://foo">
    <anotherNs:child1>Another Text</anotherNs:child1>
    <anotherNs:child2>Another Number</anotherNs:child2>
</root>

So the only difference is the name of the namespace. How can I select the content of child2 for both xml's with one XPath expression?

Something like "/root/child2" or "//child2" did not work.


回答1:


Use the local-name() function like so:

//*[local-name()='child2']



回答2:


You can bind any prefix you like (say banana) to the namespace "http://foo", and the expression /root/banana:child2 will find the child2 element, regardless what namespace prefix has been used in the source document. Only the namespace URI has to match.



来源:https://stackoverflow.com/questions/15139979/select-node-without-namespace-with-xpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!