Hoi,
for example, i have a xml file :
<bookstore> <book category="COOKING"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="WEB"> <title lang="en">XQuery Kick Start</title> <author>James McGovern</author> <author>Per Bothner</author> <author>Kurt Cagle</author> <author>James Linn</author> <author>Vaidyanathan Nagarajan</author> <year>2003</year> <price>49.99</price> </book> </bookstore>
And I want all the titles and prices
I could write this code
<php
$res = $xpath->query('/bookstore/book/title');
foreach ($res as $item) {
echo "{$item->nodeValue}";
}
$res = $xpath->query('/bookstore/book/price');
foreach ($res as $item) {
echo "{$item->nodeValue}";
}
?>
But this looks very ugly,
Is there another possibility so that I can combine those 2 blocks of code?
something like this?
<php
$res = $xpath->query('/bookstore/book');
foreach ($res as $item) {
echo "{$item->title} <br/> {$item->price}";
}
?>
Kind regards
You could adjust your XPATH with concatenate to read ‘/bookstore/book/title | /bookstore/book/price’
This would give you a node list as such:
Then just pull values 1 and 2 in each foreach loop cycle for what you like.
Hope this helps!