php - 在 XPath 中实现条件

标签 php xml xpath simplexml

<分区>

我有一个 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表达式中指定日期,

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

只会选择示例中的最后一个事件节点

关于php - 在 XPath 中实现条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3448005/

相关文章:

python - 使用 lxml Python 解析非标准 XML 中的 XPath

php - 如果另一列包含所有请求值集,如何从表中选择特定列?

javascript - 如何将 "connect"PHP "network"转换为 .svg 图像

c# - 没有数组名称的XML到Json c#

java - 使用 getSupportActionBar 显示后退按钮

android - 更改 EditText 中的输入会更新 ScrollView 中的错误 EditText

python - 在 xpath 上的过滤器后使用索引

xpath - 尝试通过变量的xpath提取值,但未获得正确的结果

PHP + HTML 代码的 PHP 评估问题

php - 检测 url 是否从页面点击而不是在浏览器中复制粘贴的最佳选择