代码之家  ›  专栏  ›  技术社区  ›  Catfish

为什么此xpath的值不为true?

  •  0
  • Catfish  · 技术社区  · 15 年前

    我试图使用xpath评估一个XML节点,我不知道为什么它的评估结果不是真的。

    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 回复  |  直到 12 年前
        1
  •  4
  •   hakre    12 年前

    而不是这个

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

    我只是需要这么做

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

    现在它检查是否至少有一个节点 <username> 与节点值一起存在 "tom" .