Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

case-insensitive matching in XPath?

Tags:

xml

xpath

For example, for the XML below

<CATALOG>     <CD title="Empire Burlesque"/>     <CD title="empire burlesque"/>     <CD title="EMPIRE BURLESQUE"/>     <CD title="EmPiRe BuRLeSQuE"/>     <CD title="Others"/> <CATALOG> 

How to match the first 4 records with xpath like //CD[@title='empire burlesque']. Is there xpath function to do this? Other solutions like PHP function are also accepted.

like image 465
Ethan Avatar asked May 23 '10 21:05

Ethan


2 Answers

XPath 2 has a lower-case (and upper-case) string function. That's not quite the same as case-insensitive, but hopefully it will be close enough:

//CD[lower-case(@title)='empire burlesque'] 

If you are using XPath 1, there is a hack using translate.

like image 164
Matthew Flaschen Avatar answered Sep 17 '22 15:09

Matthew Flaschen


matches() is an XPATH 2.0 function that allows for case-insensitive regex matching.

One of the flags is i for case-insensitive matching.

The following XPATH using the matches() function with the case-insensitive flag:

//CD[matches(@title,'empire burlesque','i')] 
like image 42
Mads Hansen Avatar answered Sep 16 '22 15:09

Mads Hansen