XPATH 查询仅过滤某些属性的值 [英] XPATH query to filter values on certain attributes only
问题描述
我有以下 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>
我想获取文件类型为mid"的项目的所有项目名称.我的 XPATH 查询看起来像
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
似乎不是仅针对具有文件类型"属性的字段检查最后一个条件,而是针对所有字段检查.这会导致返回名称mid",即使该项目的文件类型为mp3".
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".
如何将与mid"的比较限制为具有属性 Name="File Type" 的字段值?
How can I restrict the comparison with "mid" to the value of the field with the attribute Name="File Type"?
有人可以向我推荐一个按预期工作的 XPATH 语法吗?
Can anybody suggest me an XPATH syntax which works as expected?
推荐答案
XPath 谓词可以应用于任何地方,这会更直接:
XPath predicates can be applied anywhere, this would be more straight-forward:
/Library/Item[Field[@Name="File Type"] = "mid"]/Field[@Name="Name"]
你自己的表达是正确的
/Library/Item/Field[
@Name="Name"
and ../Field[@Name="File Type"] = "mid"
]
这篇关于XPATH 查询仅过滤某些属性的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!