This question already has answers here:
SimpleXML: Selecting Elements Which Have A Certain Attribute Value

(2个答案)


7年前关闭。




我有一个XML文件
<?xml version="1.0" encoding="UTF-8"?>
<xml>
  <events date="12/12/2010">
    <event>
      <title>JqueryEvent</title>
      <description>
        easily
      </description>
    </event>
  </events>
  <events date="14/12/2011">
    <event>
      <title>automatically onBlur</title>
      <description>
        when a date is selected. For an inline calendar, simply attach the datepicker to a div or span.
      </description>
    </event>
  </events>
</xml>

我正在使用此Xpath选择节点
$xml   = simplexml_load_file($file);
$nodes = $xml->xpath('//xml/events');

它将选择所有节点。我想根据日期选择节点。

最佳答案

在xpath表达式中指定日期,

IE。

$nodes = $xml->xpath('//xml/events[@date="14/12/2011"]');

将仅选择示例中的最后一个事件节点

10-07 19:05
查看更多