I maybe be trying to do something invalid here, but maybe someone smarter than me knows the correct syntax to solve my problem. Given:
<group code="开发者_运维技巧vehicle">
<line code="car">
<cell>
<box code="price">1000.00</box>
</cell>
</line>
<line code="car">
<cell code="sports">
<box code="price">500.00</box>
</cell>
</line>
</group>
If I use //*[@code="vehicle"]//*[@code="car"]//*[@code="price"]
, I will get both boxes returned (1000.00 and 500.00)--as expected but not what I want.
Is there an xpath syntax that will evaluate against all nodes that have an attribute of @code rather than skipping it if it doesn't match with the end result being that I only get back the first box (price of 1000.00)? Like asking, choose the first node with @code and that @code must equal "vehicle", then choose the next node with @code and that @code must equal "car", then choose the next node with @code and @code must equal "price".
Use:
"//box
[(@code='car' or @code='price' or @code='vehicle')
and
not(
ancestor-or-self::*
[
@code
and
not(@code='car'
or
@code='price'
or
@code='vehicle'
)
]
)
]
Not an expert on xpath but XmlSpy seems to indicate that this would work?
(//*[@code="vehicle"]//*[@code="car"]//*[@code="price"])[1]
精彩评论