ecgbert
08-16-2007, 07:12 AM
I'm creating a little Php MUD, and I have the users create characters.
So, I'm trying to make sure that the character they make has a unique name.
I'm storing my data in XML files and Xpath seemed to be the easiest and best way to do this.
So I wrote the code found at the bottom (characterfile.xml is at the very bottom)
Since 'fdqworfwe' is not a character in the xmlfile, I'm expecting my XPath query to return a nodelist with a length of 0. (I tested it out even at An Xpath Expression Testbed (http://www.whitebeam.org/library/guide/TechNotes/xpathtestbed.rhtm))
It doesn't. Instead the length is 1.
However when I change the query to
$query = "/characterfile/characters/character[name='{$lowercasecharactername}']";
It gives me zero.
Why is it when I use lower-case() it gives me 1?
<?php
$characterfiledom = new DOMDocument();
$characterfiledom->load('characterfile.xml');
$characterfilexpath = new DOMXPath($characterfiledom);
$characterselement = $characterfiledom->getElementsByTagName('characters')->item(0);
$lowercasecharactername = "fdqworfwe";
$query = "/characterfile/characters/character[lower-case(name)='{$lowercasecharactername}']";
$characterswithname = $characterfilexpath->query($query);
echo $characterswithname->length;
?>
Another odd thing is that it was working fine before and doing exactly what I wanted, so I went and developed more things, but then when I was testing everything it stopped working. Perhaps my web host changed some setting or something to throw it off. Quite odd.
<?xml version="1.0" encoding="utf-8"?>
<characterfile>
<characters>
<character status="off">
<owner>testuser</owner>
<name>testcharacter</name>
</character>
<character status="off">
<owner>testusertwo</owner>
<name>testcharactertwo</name>
</character>
</characters>
</characterfile>
So, I'm trying to make sure that the character they make has a unique name.
I'm storing my data in XML files and Xpath seemed to be the easiest and best way to do this.
So I wrote the code found at the bottom (characterfile.xml is at the very bottom)
Since 'fdqworfwe' is not a character in the xmlfile, I'm expecting my XPath query to return a nodelist with a length of 0. (I tested it out even at An Xpath Expression Testbed (http://www.whitebeam.org/library/guide/TechNotes/xpathtestbed.rhtm))
It doesn't. Instead the length is 1.
However when I change the query to
$query = "/characterfile/characters/character[name='{$lowercasecharactername}']";
It gives me zero.
Why is it when I use lower-case() it gives me 1?
<?php
$characterfiledom = new DOMDocument();
$characterfiledom->load('characterfile.xml');
$characterfilexpath = new DOMXPath($characterfiledom);
$characterselement = $characterfiledom->getElementsByTagName('characters')->item(0);
$lowercasecharactername = "fdqworfwe";
$query = "/characterfile/characters/character[lower-case(name)='{$lowercasecharactername}']";
$characterswithname = $characterfilexpath->query($query);
echo $characterswithname->length;
?>
Another odd thing is that it was working fine before and doing exactly what I wanted, so I went and developed more things, but then when I was testing everything it stopped working. Perhaps my web host changed some setting or something to throw it off. Quite odd.
<?xml version="1.0" encoding="utf-8"?>
<characterfile>
<characters>
<character status="off">
<owner>testuser</owner>
<name>testcharacter</name>
</character>
<character status="off">
<owner>testusertwo</owner>
<name>testcharactertwo</name>
</character>
</characters>
</characterfile>