XPath 匹配空格分隔的属性值? [英] XPath to match space-separated attribute values?

查看:34
本文介绍了XPath 匹配空格分隔的属性值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 XML:

<entities>
  <entity attribute="attribute-value-1 attribute-value-2">value1</entity>
  <entity attribute="attribute-value-5 attribute-value-7 attribute-value-8">value2</entity>
</entities>

如何使用 XPath 选择属性值为 "attribute-value-7" 的实体?

How can I a select using XPath an entity with attribute value of "attribute-value-7"?

推荐答案

你必须小心避免无意中匹配了超字符串,例如attribute-value-77"或wrong-attribute-value-7".

You have to be careful to avoid inadvertently matching superstrings such as "attribute-value-77" or "wrong-attribute-value-7".

使用常用的成语来匹配 HTML @class 属性:

Use the idiom commonly used to match HTML @class attributes:

//entity[contains(concat(' ', normalize-space(@attribute), ' '),
                           ' attribute-value-7 ')]

XPath 2.0

//entity[tokenize(@attribute,'\s+')='attribute-value-7']

这篇关于XPath 匹配空格分隔的属性值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆