我想遍历以下 xml 文件中的每个 book_list,并为每个 book_list 循环遍历该 book_list 的每本书。
<inventory>
<book_list>
<book>
<author>Rowling</author>
<title>Harry Potter</title>
</book>
<book>
<author>Blyton</author>
<title>Famous 5</title>
</book>
</book_list>
<book_list>
<book>
<author>Bloggs</author>
<title>Learning XML</title>
</book>
<book>
<author>Jones</author>
<title>Beginning PHP</title>
</book>
</book_list>
</inventory>
对于每个 book_list,我如何在 php simplexml 脚本中使用 xpath 循环浏览每本书?这是我的代码,
$booklistpath = $xml->xpath('//booklist');
foreach ($booklistpath as $booklist) {
$bookpath = $xml->xpath('//book');
foreach ($bookpath as $book) {
...
}
}
第一个循环很好,它遍历每个book_list
循环 - 但是嵌套循环,它意味着遍历每个book
特定的循环book_list
遍历整个文档中的每一本书。我也试过:-
'.//book' and
'descendant::book'