我需要通过匹配“Ref Location=”C1”和“EventData ID=”W12”的索引来获取“ItemKey=”X””值。如果您注意到,所有四个都有“Ref Location=”C1”,但只有最后 3 个具有“EventData ID=”W12”'
<Object>
<Level1>
<Level2>
<Ref Location="C1"/>
<EventData ID="W6" EventKey="1">
<FormData ID="NL">
<ItemData ItemID="LINE" ItemKey="1">...</ItemData>
</FormData>
</EventData>
</Level2>
</Level1>
<Level1>
<Level2>
<Ref Location="C1"/>
<EventData ID="W12" EventKey="1">
<FormData ID="NL">
<ItemData ItemID="LINE" ItemKey="1">...</ItemData>
</FormData>
</EventData>
</Level2>
</Level1>
<Level1>
<Level2>
<Ref Location="C1"/>
<EventData ID="W12" EventKey="1">
<FormData ID="NL">
<ItemData ItemID="LINE" ItemKey="2">...</ItemData>
</FormData>
</EventData>
</Level2>
</Level1>
<Level1>
<Level2>
<Ref Location="C1"/>
<EventData ID="W12" EventKey="1">
<FormData ID="NL">
<ItemData ItemID="LINE" ItemKey="3">...</ItemData>
</FormData>
</EventData>
</Level2>
</Level1>
目前我有
(/Object/Level1/Level2/Ref[@Location='C1'])[2]/../EventData[@ID='W12']/FormData/ItemData/@ItemKey
其中 X 是索引,但它为所有“C1”节点获取 ItemKey="X"。我只需要为 ID="W12"节点获取 ItemKey="X"。
当路径具有“[X]”时的期望结果
[1] -> ItemKey="1" // ID = "W12"
[2] -> ItemKey="2" // ID = "W12"
[3] -> ItemKey="3" // ID = "W12"
最佳答案
这应该有效:
//ItemData[preceding::Ref[1][@Location="C1"][following-sibling::EventData[@ID="W12"]]]/@ItemKey
输出:3 个值:
1
2
3
关于java - XPath 解析特定节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61895220/