Search accented / Uppercase charaters with XPath

XPath 2.0 is a version of the XPath language defined by the World Wide Web Consortium
XPath is used primarily for selecting parts of an XML document. For this purpose the XML document is modelled as a tree of nodes. XPath allows nodes to be selected by means of a hierarchic navigation path through the document tree.

Function library

The function library in XPath 2.0 is greatly extended from the function library in XPath 1.0. (Bold items are available in XPath 1.0)

Some functions available include the following:
*Purpose : Example Functions

* General string handling lower-case, upper-case, substring, substring-before, substring-after, translate, starts-with, ends-with, contains, string-length, concat, normalize-space, normalize-unicode

* Regular expressions matches, replace, tokenize

However, chances are you’re stuck with XPath 1.0. In that case, you can abuse the translate() function:

For my search on unicode file xml under php library simpleXML I used (Nodes name are case sensitive). A good article about parsing xml files with simpleXML is here.

$objXML = simplexml_load_file('includes/datas/books.xml');
.....................

$nm = count($objXML->xpath("//Book[contains(translate(E010/text(), 'ABCDEFGHJIKLMNOPQRSTUVWXYZàâèéêëîïôùûü', 'abcdefghjiklmnopqrstuvwxyzaaeéeeiiouuu'), '$search')]"));
                 $xml = $objXML->xpath("//Eve[contains(translate(E010/text(), 'ABCDEFGHJIKLMNOPQRSTUVWXYZàâèéêëîïôùûü', 'abcdefghjiklmnopqrstuvwxyzaaeéeeiiouuu'), '$search')]");

For a terms search all uppercase and accents are translated under nodes (for example Algérie to algerie) so we must search in lowercase algerie to find our words …

 if (isset($_GET['search ']) && strlen($_GET['search '])>0) {
      $keywords = $_GET['search '];

extradrmtech

Since 30 years I work on Database Architecture and data migration protocols. I am also a consultant in Web content management solutions and medias protecting solutions. I am experienced web-developer with over 10 years developing PHP/MySQL, C#, VB.Net applications ranging from simple web sites to extensive web-based business applications. Besides my work, I like to work freelance only on some wordpress projects because it is relaxing and delightful CMS for me. When not working, I like to dance salsa and swing and to have fun with my little family.

You may also like...