简体   繁体   中英

Loading xml document and filter it with xpath

Im using PHP to load a XML document:

$xmlDocument = file_get_contents($url, false, $context);
$xmlString = simplexml_load_string($xmlDocument);

XmlString and $url : http://testing.zwiink.us/getPurchased.php

Now im trying to filter out each element, which contains:

[u] => /zwinky/fds/creations/

I tried to use xpath, but always failed, i guess i used a wrong search value. Does anyone hava a idea how I would get this working?

You need to make an xpath query first with using contains() :

$xmlDocument = file_get_contents($url, false, $context);
$xmlString = simplexml_load_string($xmlDocument);

// target elements with that attribute
$u = $xmlString->xpath('//*[contains(@u, "/zwinky/fds/creations/")]');

echo '<pre>';
print_r($u);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM