---
title: xml - case-insensitive matching in xpath? - Stack Overflow
updated: 2019-02-26 14:12:35Z
created: 2019-02-26 14:09:09Z
tags:
  - IT
  - Development/xml
  - Development/xpath
---


case-insensitive matching in xpath?

For example, for the xml below

```xml
<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.

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:

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

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

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

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

```xml
//CD[matches(@title,'empire burlesque','i')]
```

One possible PHP solution:

```php
// 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")];
```

