XPATH query to filter values on certain attributes only
I have following XML:
<Library>
<Item>
<Field Name="Name">smooth</Field>
<Field Name="File Type">mid</Field>
<Field Name="File Size">60026</Field>
</Item>
<Item>
<Field Name="Name">mid</Field>
<Field Name="File Type">mp3</Field>
<Field Name="File Size">4584972</Field>
</Item>
</Library>
I'd like to get all item names of items of the file type "mid". My XPATH query looks as
/Library/Item/Field
[
@Name="Name" and
(../Field[@Name="File Type" and ../Field[.="mid"]])
]
But unfortunately both items are returned from that query.
smooth
mid
Seems that the last condition is not checked against fields with the attribute "File Type" only, but all fields. Which results in a return of the name "mid", even if this item is of file type "mp3".
How can I restrict the comparison with "mid" to the value of the field with the attribute Name="File Type"?
Can an开发者_JS百科ybody suggest me an XPATH syntax which works as expected?
XPath predicates can be applied anywhere, this would be more straight-forward:
/Library/Item[Field[@Name="File Type"] = "mid"]/Field[@Name="Name"]
Your own expression would be correct as
/Library/Item/Field[
@Name="Name"
and ../Field[@Name="File Type"] = "mid"
]
精彩评论