Using OR operator in XPath

会有一股神秘感。 提交于 2019-12-24 19:17:51

问题


I'm using the OR operator (more than once) in my XPath expression to extract what I need in the content before a specific string is encountered such as 'Reference,' 'For more information,' etc. Any of these terms should return the same result, yet they may not be in that order. For example, 'Reference' might not be first and may not be in the content at all, and one of the matches uses a table, 'About the data.' I want all content before any one of these strings appears.

Any help would be appreciated.

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

That would also need to take into account the table:

$expression =
    "//article/table/tbody/tr/td[
        starts-with(normalize-space(), 'About the data used')
]/preceding-sibling::p";

Here's an example:

<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.
            </p>
            <p>
                "We cannot let a set of difficult numbers discourage us
                or weaken our resolve" Garcetti said.
            </p>
            <p>
                References
                By Jeremy Herb, Caroline Kelly and Manu Raju, CNN
            </p>
            <p>
                For more information: Maeve Reston, CNN
            </p>
            <p>Maeve Reston, CNN</p>
            <table>
                <tbody>
                    <tr>
                        <td>
                            <strong>About the data used</strong>
                        </td>
                    </tr>
                    <tr>
                        <td>From
                        </td>
                        <td>Washington, CNN</td>
                    </tr>
                </tbody>
            </table>
        </article>
    </main>
</root>

The result I'm looking for would be the following.

<p>
    The stunning increase in homelessness announced in Los Angeles
    this week — up 16% over last year citywide — was an almost  an
    incomprehensible conundrum.
</p>
<p>
    "We cannot let a set of difficult numbers discourage us
    or weaken our resolve" Garcetti said.
</p>

回答1:


I want all content before any one of these strings appears.

That is, you want the content before the first paragraph to contain one of these strings.

The paragraphs that contain one of these strings are:

p[starts-with(normalize-space(), 'References') or starts-with(....)]

The first such paragraph is

p[starts-with(normalize-space(), 'References') or starts-with(....)][1]

The paragraphs before that are:

p[starts-with(normalize-space(), 'References') or starts-with(....)][1]
/preceding-sibling::p

In 2.0 I would probably use a regular expression:

p[matches(., '^\s*(References|For more information)')]

to avoid the repeated calls on normalize-space().



来源:https://stackoverflow.com/questions/56550008/using-or-operator-in-xpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!