XPath 匹配空格分隔的属性值?

ein*_*ein 3 xml 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>
Run Code Online (Sandbox Code Playgroud)

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

kjh*_*hes 5

您必须小心避免无意中匹配超字符串,例如“attribute-value-77”或“wrong-attribute-value-7”。

使用常用的习惯用法来匹配 HTML@class属性:

XPath 1.0

//entity[contains(concat(' ', normalize-space(@attribute), ' '),
                           ' attribute-value-7 ')]
Run Code Online (Sandbox Code Playgroud)

XPath 2.0

//entity[tokenize(@attribute,'\s+')='attribute-value-7']
Run Code Online (Sandbox Code Playgroud)