为什么这个xpath没有评估为true?

时间:2010-02-28 21:48:56

标签: php xml xpath

我正在尝试使用xpath评估xml节点,我不确定为什么它没有评估为true。

XML

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

当我提交表单时,这个脚本被称为

    <?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;
 }
?>

1 个答案:

答案 0 :(得分:4)

而不是这个

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

我只需要这样做

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

现在它检查是否存在至少一个节点<username>的节点"tom"