次の形式の XML があります。
<root>
<item>
<created>2013-05-21</created>
<name>Item 1</name>
<attributes>
<stock>
<amount>1</amount>
</stock>
<price>10</price>
</attributes>
</item>
<item>
<created>2013-05-21</created>
<name>Item 2</name>
<attributes>
<stock>
<amount>1</amount>
</stock>
<price>20</price>
</attributes>
</item>
<item>
<created>2013-05-21</created>
<name>Item 3</name>
<attributes>
<stock>
<amount>2</amount>
</stock>
<price>10</price>
</attributes>
</item>
</root>
私は次のものを取得しようとしています:
1 - Get all items that have a stock amount that is 1
2 - Get all of the filtered items that have a price greater than or equal to 15
つまり、上記の結果は次のようになります。Item 2
私はこれまでに次のことをしています:
$xml = new SimpleXMLElement($xmlString);
$xmlReturn = $xml->xpath("item[attributes/stock[contains(amount,'1')]]");
その後、次のようにループして XML を取得できます。
foreach($xmlReturn as $node){
echo $node->asXml();
}
問題は、xpath
フィルターの 2 番目の部分で、価格が より大きくなることです15
。
私はもう試した:
$test = $xml->xpath("item[attributes/[price>15]]");
しかし、それは私にエラーを与えます:
Warning: SimpleXMLElement::xpath(): Invalid expression
2 つの検索を 1 つのフィルターに結合できますか?
ありがとう
更新しました
$data = 'XML DATA HERE';
$xml = new SimpleXMLElement($data);
$xpath = $xml->xpath("//item[attributes[stock/amount='1'][price >= 15]]");
foreach($xpath as $node)
{
echo $node->xpath('name');
// this throws a Notice: Array to string conversion error
}