Xpath select 节点文本但排除子节点文本

Xpath select node text but exclude child node text

我有以下 XML:

<topic class="Top">
    <title>
        Interesting Article
    </title>
    <subtitle>
        Science & Industry
        <insertedText action="start"/>
            Inside & Out
        <insertedText action="end"/>
        A Profile
    </subtitle>
</topic>

而且我想用xpath提取字幕的文本,除了两个<insertedText>节点之间的字符串,给出文本"Science & Industry A Profile".

这是我最近的尝试,但老实说我很困惑,我意识到这并不排除两个标签之间的文本!任何帮助将不胜感激:

/topic[@class='Top']/*[local-name()='subtitle'][not(descendant::insertedText)]/text()

<insertedText> 标签的数量也是可变的,因此 <insertedText> 标签上可能有 none 或多组应该被忽略。

下面是可能遇到的 XML 类型的进一步示例:

<topic class="Top">
    <title>
        Interesting Article
    </title>
    <subtitle>
        Science & Industry
        <insertedText action="start"/>
            Inside & Out
        <insertedText action="end"/>
        A Profile
        <insertedText action="start"/>
            An Insiders View
        <insertedText action="end"/>
        The Full Story
    </subtitle>
</topic>

回答

根据@lambo477 提供的完整答案如下:

./topic[@class='top']/*[local-name()='subtitle']/text()[1]|/topic[@class='top']/*[local-name()='subtitle']/*[local-name()='insertedText'][@action='end']/following-sibling::node()[1]

您可以尝试以下 XPath:

//subtitle/text()[1]|//insertedText[@action="end"]/following-sibling::node()[1]