xpath如何按位置访问节点

时间:2018-02-28 10:36:10

标签: xpath position msxml6

我有这样的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中的位置来访问?

1 个答案:

答案 0 :(得分:1)

  

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

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

(/ns:Documents/.....//ce:amc)[5]