【问题标题】:Xpath first text before needed tagXpath 需要标记之前的第一个文本
【发布时间】:2013-11-12 22:52:29
【问题描述】:

我有一些 HTML 代码:

<dl>
    <div>
       <div>foo</div>
    <div>
    "I need getting only this text"
    <dd>
       <div>foo</div>
       <div>foo</div>
    </dd>
    <div>
        <div>foo</div>
    <div>
    "I need getting only this text"
    <dd>
        <div>foo</div>
        <div>foo</div>
    </dd>
</dl>

所以我需要只在一个必需的标签(不是“foo”)之前获取第一个文本。 我试过像

这样的语法

//text()[(preceding::dd)][某个整数]

//text()[(preceding::dd)][last()]

但这一切现在都对我有用,因为 div"foo" 的计数 - 未定义。 我需要一些 xpath,例如 //dl/text() 但返回 'dl' 的直接祖先,没有更深的层次。

【问题讨论】:

    标签: html xpath


    【解决方案1】:

    所以我只需要在所需标签之前获取第一个文本

    这个怎么样:

    //dd/preceding-sibling::text()[1]
    

    即找到dd 标签,然后为每个标签取最近的前面的兄弟文本节点。给定您的示例,这将返回一组两个文本节点,每个节点都包含文本

    '
        "I need getting only this text"
        '
    

    (没有单引号,即换行符,四个空格,双引号,我只需要获取此文本,双引号,换行符,四个空格)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-11
      • 2011-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多