xpath如何按位置访问节点

问题描述 投票:1回答:1

我有这样的xml文件

<ce:MarkInfo>
<ce:boxpos>
<ce:boxnumber>box-00112</ce:boxnumber>
<ce:amclist>
<ce:amc>12</ce:amc>
<ce:amc>22</ce:amc>
</ce:amclist>
</ce:boxpos>
<ce:boxpos>
<ce:boxnumber>box-00113</ce:boxnumber>
<ce:amclist>
<ce:amc>32</ce:amc>
<ce:amc>42</ce:amc>
<ce:amc>52</ce:amc>
<ce:amc>62</ce:amc>
</ce:amclist>
</ce:boxpos>
</ce:MarkInfo>

和xpath表达式

xDoc.selectNodes("/ns:Documents/ns:Document/ns:WayBill_v3/wb:Content/wb:Position[1]/wb:InformF2/ce:MarkInfo//ce:amc").length = 6

/ns:Documents/ns:Document/ns:WayBill_v3/wb:Content/wb:Position[1]/wb:InformF2/ce:MarkInfo//ce:amc[1]

返回AMC值以12开头

/ns:Documents/ns:Document/ns:WayBill_v3/wb:Content/wb:Position[1]/wb:InformF2/ce:MarkInfo//ce:amc[3]

返回AMC值以52开头

/ns:Documents/ns:Document/ns:WayBill_v3/wb:Content/wb:Position[1]/wb:InformF2/ce:MarkInfo//ce:amc[5]

返回null

我如何通过它们的绝对位置来访问节点,而不是通过ce:amclist中的位置来访问节点?

xpath position msxml6
1个回答
1
投票

“我如何通过绝对位置访问节点,而不是通过ce:amclist中的位置?”

将整个XPath包装在括号中,并在外部添加位置谓词:

(/ns:Documents/.....//ce:amc)[5]
© www.soinside.com 2019 - 2024. All rights reserved.