XPATH 查询查找包含关键字的属性
我正在尝试在多个不同节点的多个属性中搜索某些内容
这就是我在一个属性中找到它的方式
//*[contains(@name,'KEYA')]
示例 XML:
<cars>
<car model="2000" name="Awesome KEYA Car" name2="somethine else">Brand1</car>
<car model="2005" name="Awesome Car" name2="KEYA something else">Brand 2</car>
<car name="Awesome Car" name2="somethine else">Brand1</car>
<car dontmatch="KEYA" name2="somethine else">Brand3333</car>
</cars>
我真正想要的是超过 10 个属性的类似内容(它只需要匹配白名单属性) ,
//*[contains((@name or @name2 or @name3),'KEYA')]
使用XPATH 1.0。关于如何做到这一点有什么想法吗?除了重复包含之外尝试了多种方法,但没有成功。
I'm trying to search something in multiple attributes in multiple different nodes
This is how I find it in one attribute
//*[contains(@name,'KEYA')]
Sample XML:
<cars>
<car model="2000" name="Awesome KEYA Car" name2="somethine else">Brand1</car>
<car model="2005" name="Awesome Car" name2="KEYA something else">Brand 2</car>
<car name="Awesome Car" name2="somethine else">Brand1</car>
<car dontmatch="KEYA" name2="somethine else">Brand3333</car>
</cars>
What I really want is something like this for more than 10 attributes (it needs to match only white-listed attributes),
//*[contains((@name or @name2 or @name3),'KEYA')]
Using XPATH 1.0. Any ideas about how to do this? Tried several ways other than repeating contains but didn't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这对我有用
好的,编辑后,我建议
如果您想排除多个属性,则需要指定其他谓词(方括号内的内容)。 Xpath 不提供排除属性名称集的方法,除非您可以使用
startswith()
或contains()
或类似的东西来约束该集合。有一些方便的方法可以在其他库中查询 XML,例如,如果您使用 .NET,则可以使用 LINQ-to-XML。这将允许您更简洁地指定这些类型的查询。但它是一个完全不同的 API。
This worked for me
ok, after the edit, then I suggest
If you want to exclude more than just one attribute, then you need to specify additional predicates (the things inside the square brackets). Xpath doesn't provide a way to exclude sets of attr names, unless you can constrain the set with a
startswith()
orcontains()
or something similar.There are handy ways to query XML in other libraries, like, for example LINQ-to-XML if you are using .NET. That would allow you to specify those kinds of queries more succintly. But it's a different API altogether.
使用:
这是由 XPath 可视化工具:
说明 :
您可以在字符串中指定要搜索的所有属性的名称。使用的“管道分隔符 (
|
) 保证即使一个名称是另一个名称的开头,只有当该名称存在单独的管道分隔子字符串时,该名称才会包含在搜索中。Use:
Here is the selection, as produced by the XPath Visualizer:
Explanation:
You can specify in a string the names of all attributes you want searched. The "pipe delimiter (
|
) used guarantees that even if a name is the start of another, it one will only be included in the search if there is a separate, pipe-delimited substring for this name.怎么样:
您可以采用 Cheeso 的答案并执行以下操作:
您必须以某种方式构建 XPath 语句,以从属性白名单中进行选择(我的示例),或者排除黑名单中的属性(修改后的 Cheeso 示例)。
再次构建 Cheeso 的工作,这可能会简化构建黑名单:
或作为白名单:
How about:
You could take Cheeso's answer and do something like this:
One way or another you're going to have to construct the XPath statement to either select from a whitelist of attributes (my example), or exclude attributes in a blacklist (the modified Cheeso example).
Building again off of Cheeso's work this might simplify building the blacklist:
or as a whitelist: