einstein
einstein

Reputation: 13850

XPath to match space-separated attribute values?

I have a 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>

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

Upvotes: 3

Views: 1185

Answers (1)

kjhughes
kjhughes

Reputation: 111621

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

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

XPath 1.0

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

XPath 2.0

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

Upvotes: 5

Related Questions