开发者

Why does this xpath not evaluate to true?

I'm trying to evaluate an xml node using xpath and i'm not sure why it's not evaluating to true.

xml

<?xml version="1.0"?>
<users>
    <user>
        <username>tom</username>
        <password>d644cd4b1c72f563855e689d46d9198e</password>
    </user>
    <user>
        <username>jeff</username>
        <password>smith</password>
    </user>
</users>

When i submit a form this script is called

    <?php
        //needed for firePHP in firebug
        include('FirePHPCore/fb.php');
        ob_start();

        $error = false;
        if(isset($_POST['login'])) {
            $username = preg_replace('/[^A-Za-z0-9]/', '', $_POST['username']);
            $password = md5($_POST['password']);


       开发者_如何学运维     if(file_exists("../users.xml")) {

                $xmlobject = simplexml_load_file("../users.xml");
                fb("username is: ".$username); //returns tom
                fb($xmlobject->xpath("//*[username='tom']")); //returns the entire array of elements. How do i make it return just the node value?

                //why does this evaluate to false?
                if($username == $xmlobject->xpath("//*[username='tom']")) {
                    fb("got here");
                } else {
                    fb("got here instead");
                }   
            }
            $error = true;
 }
?>


Instead of this

if($username == $xmlobject->xpath("//*[username='tom']"))

I just needed to do this

if($xmlobject->xpath("//*[username='tom']"))

Now it checks if at least one node <username> exists with the node value "tom".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜