任何人都知道如何使用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