Search code examples
xpathpositionmsxml6

xpath how access nodes by position


I have such xml file

<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>

and xpath expression

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]

returns AMC value begins with 12

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

returns AMC value begins with 52

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

returns null

how can i access nodes by they absolute position, not by position in ce:amclist?


Solution

  • "how can i access nodes by they absolute position, not by position in ce:amclist?"

    Wrap the entire XPath in parentheses, and add the position predicate outside :

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