Xpath路径不起作用

时间:2017-09-09 11:08:30

标签: php xml xpath

我有以下XML:

   void SomeMethod()
   {
     ....
      var referrer = "some URL";
      //web.Referrer = referrer;  Ideal way, but not possible
      web.PreRequest += OnPreRequest;
      ....
   }

    bool OnPreRequest(HttpWebRequest req)
    {
        req.Referer = ??; //how to know the referrer address here?
        return false;
    }

我尝试用php xpath读取信息。我需要显示类型为“images_800x600”的产品网址。

我试过了:

<root>
    <product>
        <code>ZX-0015</code>
        <attached_files>
            <file>
                <type>images_800x600</type>
                <url>http://www.example.com/img/800_600/ZX-0015.jpg</url>
            </file>
            <file>
                <type>images_300x200</type>
                <url>http://www.example.com/img/300_200/ZX-0015.jpg</url>
            </file>
        </attached_files>
    </product>
    <product>
        <code>02PX-5836</code>
        <attached_files>
            <file>
                <type>images_800x600</type>
                <url>http://www.example.com/img/800_600/02PX-5836.jpg</url>
            </file>
            <file>
                <type>images_300x200</type>
                <url>http://www.example.com/img/300_200/02PX-5836.jpg</url>
            </file>
        </attached_files>
    </product>
</root>

但我只得到以下输出:

array(0){} array(0){}

如何获取800x600类型的请求网址?

2 个答案:

答案 0 :(得分:1)

尝试xpath("//product[code='$sku']//file[type='images_800x600']/url");

答案 1 :(得分:0)

另一个选择是,由于您正在迭代每个产品项,您可以执行相对 xpath 查询:

foreach ($xml as $item) {
    $sku = $item->code;
    $x_img = $item->xpath("attached_files/file[type='images_800x600']/url");
    var_dump($x_img);
}

在每个项目上,您都可以获得所需大小的图像。

相关问题