Php XML XPath问题

Php XML XPath问题,php,xml,xpath,Php,Xml,Xpath,我是Xpath新手,实际上今天才第一次看到它,我对Xpath没有加载XML中定义的图像有一些问题 到目前为止,我已经: <page name = "home"><br /> <image> <imagehome1> <imge>resp.jpg</imge><br /> <class>right</class>

我是Xpath新手,实际上今天才第一次看到它,我对Xpath没有加载XML中定义的图像有一些问题

到目前为止,我已经:

<page name = "home"><br />
<image>
        <imagehome1>
            <imge>resp.jpg</imge><br />
            <class>right</class>
        </imagehome1>
        <imagehome2>
            <imge></imge><br />
            <class>right</class>
        </imagehome2>
        <imagehomebot>
            <imge>cfr.png</imge><br />
            <class>bottom</class>
        </imagehomebot>
    </image>
</page>

" />
看起来您希望以相同的方式处理包含imge节点的所有节点

试一试

“/content/page/image/*[imge]”

要获得带有一个xpath的imagehome1、imagehome2和imagehomebot,如果要将节点限制为具有实际字符串的节点,可能类似于

/content/page/image/*[imge/text()]


“[]”中的表达式是“谓词”,它指定了与前面的“上下文”节点(“*”)相关的条件,该节点表示获取图像的所有子节点(我这样做是因为它们的命名不同)

所有这些图像都存在吗?HTML输出是什么样子的?实际显示imagehomebot图像的代码在哪里?这可能与“imgs”文件夹后缺少“/”有关,但由于它现在神奇地出现了,您的问题可能会得到解决,对吗?:)不,如果您仔细查看编号方案,以上都没有变量不匹配lol。如果你看看我的操作编辑。我一直在复制和粘贴很多,这就是为什么会发生的原因。是的,我明白了。设置$img3并输出$img2。:)
<? 
                $nodes = $dom->xpath('/content/page/image/imagehome1');
                if (count($nodes) > 0) {
                    if(!(string)$nodes[0]->imge){
                        $class = "";}
                    else{
                    $class3 = (string)$nodes[0]->class;
                    $img3 = (string)$nodes[0]->imge;}
                }
            ?>
    <div class="<? echo $class ?>">
        <img src="imgs/<? echo $img1 ?>" />
    </div>

    <? 
                $nodes = $dom->xpath('/content/page/image/imagehome2');
                if (count($nodes) > 0) {
                    if(!(string)$nodes[0]->imge){
                        $class = "";}
                    else{
                    $class3 = (string)$nodes[0]->class;
                    $img3 = (string)$nodes[0]->imge;}
                }
            ?>

    <div class="<? echo $class ?>">
        <img src="imgs/<? echo $img2 ?>" />
    </div>

    </div>


    <div id="box2">

    <? 
                $nodes = $dom->xpath('/content/page/image/imagehomebot');
                if (count($nodes) > 0) {
                    if(!(string)$nodes[0]->imge){
                        $class = "";}
                    else{
                    $class3 = (string)$nodes[0]->class;
                    $img3 = (string)$nodes[0]->imge;}
                }
            ?>

<div class="<? echo $class3 ?>">
        <img src="imgs/<? echo $img3 ?>" />
    </div>