case insensitive xpath searching in php

Gordon’s recommendation to use a PHP function from within XPath will prove more flexible should you choose to use that. However, contrary to his answer, the translate string function is available in XPath 1.0 so that means you can use it; your problem is how.

First, there is the obvious typo that Charles pointed out in his comment to the question. Then there is the logic of how you’re trying to match the text values.


In word form, you are currently asking, “does the text contain the lowercase form of the keyword?” This is not really what you want to be asking. Instead, ask, “does the lowercase text contain the lowercase keyword?” Translating (pardon the pun) that back into XPath-land would be:

(Note: truncated alphabets for readability)

//line[contains(translate(text(),'ABC...Z','abc...z'),'chicago')]

The above lowercases the text contained within the line node then checks that it (the lowercased text) contains the keyword chicago.


And now for the obligatory code snippet (but really, the above idea is what you really need to take home):

$xml    = simplexml_load_file($data);
$search = strtolower($keyword);
$nodes  = $xml->xpath("//line[contains(translate(text(), 'ABCDEFGHJIKLMNOPQRSTUVWXYZ', 'abcdefghjiklmnopqrstuvwxyz'), '$search')]");

echo 'Got ' . count($nodes) . ' matches!' . PHP_EOL;
foreach ($nodes as $node){
   echo $node . PHP_EOL;
}

Edit after dijon’s comment

Inside the foreach, you could access the line number, chapter number and book name like below.

Line number — this is just an attribute on the <line> element which makes accessing it super-easy. There are two ways, with SimpleXML, of accessing it: $node['number'] or $node->attributes()->number (I prefer the former).

Chapter number — to get at this, as you rightly said, we need to traverse up the tree. If we were using the DOM classes, we would have a handy $node->parentNode property leading us directly to the <chapter> (since it is the immediate ancestor to our <line>). SimpleXML does not have such a handy property, but we can use a relative XPath query to get it. The parent axis allows us to traverse up the tree.

Since xpath() returns an array we can cheat and use current() to access the first (and only) item in the array returned from it. Then it is just a matter of accessing the number attribute as above.

// In the near future we can use: current(...)['number'] but not yet
$chapter = current($node->xpath('./parent::chapter'))->attributes()->number;

Book name — the process for this is the same as that of accessing the chapter number. A relative XPath query from the <line> could make use of the ancestor axis like ./ancestor::book (or ./parent:chapter/parent::book). Hopefully you can figure out how to access its name attribute.

Leave a Comment