0

我有一些以下格式的 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过滤器的第二部分,得到的价格大于15.

我试过了:

$test = $xml->xpath("item[attributes/[price>15]]");

但这给了我一个错误:

Warning: SimpleXMLElement::xpath(): Invalid expression

我可以将两个搜索合并到一个过滤器中吗?

谢谢

更新

$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
}
4

1 回答 1

1

获得所需输出的完整 XPATHItem 2是:

//item[attributes[stock/amount='1'][price >= 15]]
于 2013-05-22T08:37:54.247 回答