douren9077 2010-02-28 21:48
浏览 9
已采纳

为什么这个xpath没有评估为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;
 }
?>
  • 写回答

1条回答 默认 最新

  • donglu4633 2010-02-28 21:53
    关注

    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".

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题