我有以下xml,我想篩選以僅顯示指定發佈者的數據。xml to php with filter
<?xml version="1.0"?>
<books>
<book isbn="978-1594489501">
<title>A Thousand Splendid Suns</title>
<author>Khaled Hosseini</author>
<publisher>Riverhead Hardcover</publisher>
<amazon_price>14.27</amazon_price>
</book>
<book isbn="978-1594489587">
<title>The Brief Wondrous Life of Oscar Wao</title>
<author>Junot Diaz</author>
<publisher>AB</publisher>
<amazon_price>14.97</amazon_price>
</book>
<book isbn="978-0545010221">
<title>Harry Potter and the Deathly Hallows</title>
<author>J. K. Rowling</author>
<publisher>AB</publisher>
<amazon_price>19.24</amazon_price>
</book>
</books>
我希望把HTML表從示例發佈AB的數據。
<?php
// load SimpleXML
$books = new SimpleXMLElement("books.xml);
$myVar="AB";
$books = $s->xpath("//book");
echo <<<EOF
<table style='width: 99%;'>
<thead>
<tr>
<th>Title</th>
<th>Author</th>
</tr>
</thead>
EOF;
foreach($books as $book) // loop through our books
{
//if the type of $product books user input (ie $myVar)
if ($book->type == $myVar)
{
echo <<<EOF
<tbody>
<tr>
<td>{$book->title} </td>
<td>{$book->author}</td>
</tr>
EOF;
}}
echo '</tbody>';
echo '</table>';
?>
我是一個非對象在得到調用一個成員函數的XPath(),任何幫助,請。
變量$ s是字符串'$書= $ S->的XPath( 「//書」)中未定義;' – Minras