在 XPath 表达式中使用 OR 运算符

Using OR operator in XPath expression

我想在我的 XPath 表达式中使用 OR 条件(不止一次)在遇到特定字符串之前提取内容中我需要的内容,例如 'Reference,' 'For more information,' 等。当我在 PHP 中使用它时,下面的表达式似乎不起作用,但它在 XPath 测试器上有效。我将不胜感激任何帮助。另外,表达式可以压缩吗?

"//p[starts-with(normalize-space(),'Reference')]/preceding-sibling::p | 
//p[starts-with(normalize-space(), 'For more')]/preceding-sibling::p | 
//p[starts-with(normalize-space(),'Something')]/preceding-sibling::p"

这是一个例子:

<root>
    <main>
        <article>
            <p>
               The stunning increase in homelessness announced in Los Angeles 
               this week — up 16% over last year citywide — was an almost  an 
               incomprehensible conundrum given the nation's booming economy 
               and the hundreds of millions of dollars that city, county and 
               state officials have directed toward the problem.
            </p>
            <p>
                "We cannot let a set of difficult numbers discourage us 
                or weaken our resolve" Garcetti said.
            </p>
            <p>
                For more information: Maeve Reston, CNN
            </p>
        </article>
    </main>
</root>

我正在寻找的结果如下。

<p>
    The stunning increase in homelessness announced in Los Angeles
    this week — up 16% over last year citywide — was an almost  an
    incomprehensible conundrum given the nation's booming economy
    and the hundreds of millions of dollars that city, county and
    state officials have directed toward the problem.
</p>
<p>
    "We cannot let a set of difficult numbers discourage us
    or weaken our resolve" Garcetti said.
</p>

Normalize-space() 在这里不起作用,因为您正在尝试查找 sub-string。最好在 xpath 之后使用 contains.Try。

//p[contains(., 'For more')]/preceding-sibling::p

基于字符串的 or 子句示例。

//p[contains(., 'For more')]/preceding-sibling::p|//p[contains(., 'information')]/preceding-sibling::p|//p[contains(., 'CNN')]/preceding-sibling::p

管道不完全是 "OR" - 它允许您使用多个替代表达式。您可以将其与 SQL 中的 UNION 进行比较。但它适用于 PHP.

$document = new DOMDocument();
$document->loadXML($xml);
$xpath = new DOMXpath($document);

$expression = 
    "//p[starts-with(normalize-space(), 'Reference')]/preceding-sibling::p | 
     //p[starts-with(normalize-space(), 'For more')]/preceding-sibling::p | 
     //p[starts-with(normalize-space(), 'Something')]/preceding-sibling::p";

foreach ($xpath->evaluate($expression) as $node) {
    echo $document->saveXML($node);
}

然而Xpath条件下允许"or",实际上:

$expression = 
    "//p[
      starts-with(normalize-space(), 'Reference') or 
      starts-with(normalize-space(), 'For more') or 
      starts-with(normalize-space(), 'Something')
    ]/preceding-sibling::p";

foreach ($xpath->evaluate($expression) as $node) {
    echo $document->saveXML($node);
}

演示:https://3v4l.org/9SMJq