简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM