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.
One possible PHP solution:
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:
If you are using XPath 1, there is a hack using translate.
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:
This does not work in Chrome Developer tools to locate a element, i am looking to locate the 'Submit' button in the screen
However, using 'translate' to replace all caps to small works as below
Update: I just found the reason why 'matches' doesnt work. I am using Chrome with xpath 1.0 which wont understand the syntax 'matches'. It should be xpath 2.0
You mentioned that PHP solutions were acceptable, and PHP does offer a way to accomplish this even though it only supports XPath v1.0. You can extend the XPath support to allow PHP function calls.
See the PHP registerPhpFunctions documentation for more examples. It basically demonstrates that "php:function" is for boolean evaluation and "php:functionString" is for string evaluation.