2

I don't want a search for the string value to be case insensitive. I want to do a search for the node without regard for case sensitivity. If the XML looks like this:

<items>
   <ITEM>My text!</ITEM>
</items>

I need something like this to work:

$someXmlDoc->xpath("/items/item/text()");

Currently, only this works:

$someXmlDoc->xpath("/items/ITEM/text()");
  • 1
    Duplicate: http://stackoverflow.com/questions/625986/how-can-i-use-xpath-to-perform-a-case-insensitive-search-and-support-non-english The answer is already there :) – Björn Jun 05 '09 at 09:38
  • 1
    Hi Bjorn -- I did see that, but it looked as if that provided an answer for a case insensitive on a string value, not a node, which is what I couldn't figure out. –  Jun 05 '09 at 11:44

4 Answers4

2

There is no case conversion in xpath 1.0 as supported by php (see http://jp.php.net/manual/en/class.domxpath.php)

you could use the translate function, shown below in a very limited sense. note: not recommended as it won't work for non-english characters

/items/*[translate(node(),'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz') = 'item']/text()

Upd:If node() will not work, try name()

you could also do a union as below

/items/ITEM/text() | /items/item/text()
Jonathan Fingland
  • 56,385
  • 11
  • 85
  • 79
  • The union works, but the translate does not. I am accepting for that answer. Thanks :) –  Jun 05 '09 at 11:49
0

Jonathan Fingland first variant did not work for me either, but I found solution finally.

It should be

/items/*[translate(name(),'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz') = 'item']/text()
0

This is weird. XML tags are case-sensitive (http://www.w3schools.com/xml/xml_syntax.asp), so <item> and <ITEM> are two different tags. Thus. "/items/item/text()" and /items/ITEM/text() are two different paths.

<items>
   <ITEM>My text!</ITEM>
   <item>Your text!</item>
</items>

"My text" and "Your text" have different xpaths

Max Kosyakov
  • 292
  • 2
  • 5
0

Taking your previous questions on the same subject into account another question submerges: "Does it have to be xml?" How about json?
To encode the data as json on the client you can use jquery-json, which takes only 2kb in the minimized version

var jsondata = {
  sender: "CATS",
  subject: "All Your Base",
  items: []
};
for(var i=0; i<8; i++) jsondata.items.push(i);

$.ajax({ url: "http://localhost/test.php", type: "post", dataType: "json", data: $.toJSON(jsondata), contentType: "application/json; charset=utf-8",

Then your server-side php script can decode the data with json_decode(), and you will get a native php array/hash according to the json-representation of the javascript object/hash.
For sending data back to the client you can use json_encode(). You get a json representation of your php array and since dataType: "json" was set, jquery will return a native javascript object holding these values/properties.
Community
  • 1
  • 1
VolkerK
  • 95,432
  • 20
  • 163
  • 226