任何人都知道如何使用xpath獲取節點的位置?使用xpath查找節點的位置
說我有下面的XML:
<a>
<b>zyx</b>
<b>wvu</b>
<b>tsr</b>
<b>qpo</b>
</a>
我可以使用下面的XPath查詢以選擇第三<b>節點(<b> TSR </B >):
a/b[.='tsr']
這是一切都很好,但我想返回該節點的序號位置,如:
a/b[.='tsr']/position()
(但多一點的工作!)
它甚至有可能?
編輯:忘了提及我使用.net 2,所以它的xpath 1.0!
更新:結束了使用James Sulak的excellent answer。對於那些有興趣,這裏是我的C#實現:
int position = doc.SelectNodes("a/b[.='tsr']/preceding-sibling::b").Count + 1;
// Check the node actually exists
if (position > 1 || doc.SelectSingleNode("a/b[.='tsr']") != null)
{
Console.WriteLine("Found at position = {0}", position);
}
'因爲我正在使用.net及其或者我無法處理我去的權力:int position = doc.SelectNodes(「a/b [。='tsr']/preceding-Sibling :: b 「).Count + 1; if(position> 1 || doc.SelectSingleNode(「a/b [。='tsr']」)!= null)//檢查實際存在的節點{//在這裏做魔術} – 2008-10-24 09:52:59
以零索引語言, t需要+1 – 2017-11-23 14:50:30