我正在使用XPATH 1.0從XML文檔中獲取某些節點,該文檔在根節點和具有屬性的節點上使用默認名稱空間。模式上設置了attributeFormDefault = "qualified"
。限定屬性的xpath
<Transaction xmlns="http://example.com/transaction">
<someElement/>
<collection>
<value p0:Name="SomeName" xmlns:p0="http://example.com/transaction">some value</value>
<value p1:Name="SomeOtherName" xmlns:p1="http://example.com/transaction">some other value</value>
</collection>
<differentCollection>
<value p2:Name="SomeName" xmlns:p2="http://example.com/transaction">not this one</value>
</differentCollection>
</Transaction>
我的目的是得到/Transaction/collection/value[@Name='SomeName']
節點。
到目前爲止我發現的唯一的解決辦法似乎有點寬泛:
//*[@*='SomeName']
我還可以通過使用得到<value>
節點的整個集合:
//*[local-name()='value']
與命名空間限制在記住,是否有更清晰,更精確的方法來獲得我之後的節點?第三方JavaScript庫不是一個選項。
編輯
感謝您的快速反應。我會在這裏指出,通過添加默認的命名空間前綴,解決了XPath查詢表達式:
<t:Transaction xmlns:t="http://example.com/transaction">
現在我可以這樣做:
/t:Transaction/t:collection/t:value[@t:Name='SomeName']