【问题标题】:Joining 2 XPath Queries加入 2 个 XPath 查询
【发布时间】:2009-08-17 12:51:51
【问题描述】:

我在使用 XPath 时遇到了困难。鉴于以下 XPath 查询:

$xpath->query('//input[@name="' . $field . '"]');
$xpath->query('//select[@name="' . $field . '"]');

是否可以将它们组合成一个查询?我想获取该字段的值,但是我不知道该字段是否是输入、选择、文本区域...

我现在的做法是这样的:

$input = $xpath->query('//input[@name="' . $field . '"]');

if (empty($input) === true)
{
    $select = $xpath->query('//select[@name="' . $field . '"]');

    if (empty($select) === true)
    {
        // ...
    }
}

尽管看起来很麻烦,但我相信一定有一种方法可以将所有查询合并为一个。

【问题讨论】:

    标签: php html dom xpath input


    【解决方案1】:

    使用“|”加入查询。

    $v = '[@name="' . $field . '"]';
    $input = $xpath->query('//input' . $v. ' | //select' . $v);
    
    if (empty($input) === true)
    {
         // ...    
    }
    

    编辑: 以为我会添加这个以供更多参考。 http://www.w3schools.com/XPath/xpath_operators.asp

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-16
      • 2017-11-22
      • 1970-01-01
      • 2021-03-02
      • 2023-01-13
      • 1970-01-01
      相关资源
      最近更新 更多