case-insensitive matching in XPath?
Solution 1
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.
Solution 2
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')]
Solution 3
This does not work in Chrome Developer tools to locate a element, i am looking to locate the 'Submit' button in the screen
//input[matches(@value,'submit','i')]
However, using 'translate' to replace all caps to small works as below
//input[translate(@value,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz') = 'submit']
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
Solution 4
One possible PHP solution:
// load XML to SimpleXML
$x = simplexml_load_string($xmlstr);
// index it by title once
$index = array();
foreach ($x->CD as &$cd) {
$title = strtolower((string)$cd['title']);
if (!array_key_exists($title, $index)) $index[$title] = array();
$index[$title][] = &$cd;
}
// query the index
$result = $index[strtolower("EMPIRE BURLESQUE")];
Solution 5
for selenium xpath lower-case will not work ... Translate will help Case 1 :
- using Attribute //*[translate(@id,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')='login_field']
- Using any attribute //[translate(@,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')='login_field']
Case 2 : (with contains) //[contains(translate(@id,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz'),'login_field')]
case 3 : for Text property //*[contains(translate(text(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'),'username')]
Related videos on Youtube
Comments
-
Ethan almost 3 years
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.-
user2237201 over 11 yearsstackoverflow.com/questions/586231/… check out ben gripka's post for xpath 1.0
-
bharat about 5 yearsHere, I also have found 2 more solutions with description: (not my site/promotion) codingexplained.com/coding/php/…
-
-
Tomalak almost 13 yearsTo the anonymous down-voter: Read the OP's question. Especially the "Other solutions like PHP function are also accepted." part.
-
usr over 10 yearsThis is regex-based which is an important difference to text-based matching.
-
T.J. Crowder almost 9 yearsEven works in SQL Server's XPath queries on XML columns. Excellent.
-
neaumusic over 4 yearsIt seems the translate concept is called 'case insensitive collation' in regards to internationalization
-
Stephan over 3 yearsAlso bear in mind that the
translate
solution in this answer won't work for languages (german for instance) having special rules for uppercase letters. -
Arjan about 3 yearsBeware that this may also find partial matches; if that's not acceptable then use
^
and$
, likematches(@title, '^empire burlesque$', 'i')
-
Kamil over 2 yearsYou helped me a lot. Many thanks!