Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XPath to match space-separated attribute values?

Tags:

xml

xpath

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"?

like image 962
einstein Avatar asked Dec 02 '25 17:12

einstein


1 Answers

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']
like image 79
kjhughes Avatar answered Dec 05 '25 07:12

kjhughes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!