代码之家  ›  专栏  ›  技术社区  ›  ebattulga

如何使用near元素选择XML节点

  •  0
  • ebattulga  · 技术社区  · 15 年前

    使用xpath和html敏捷性包,我需要选择 destination 文本使用 color:#ff00ff .

    我的HTML如下所示:

    <table>
       <tr style="color:#ff00ff">
          <td></td>
       </tr>
       <tr>
          <td>destination</td>
       </tr>
       <tr>
          <td></td>
       </tr>
       <tr>
          <td>not destination</td>
       </tr>
    </table>
    
    2 回复  |  直到 15 年前
        1
  •  2
  •   Tomalak    15 年前
    /table/tr[@style = "color:#ff00ff"]/following-sibling::tr[1]/td[1]/text()
    

    选择 <tr> 那有 style="color:#ff00ff" 从那里开始,第一个 <td> 第一个跟随者的孩子 <Tr> .

    为了额外的安全,您可以使用:

    tr[translate(@style, ' ', '') = "color:#ff00ff"]
    

    这会从属性值中删除任何空格,因此事情会变得更加独立于HTML源代码。

        2
  •  0
  •   tvanfosson    15 年前

    使用jquery,它可能看起来像这样:

    $('tr[style*="color:#ff00ff"]').next('tr').children().text();
    

    不过,这在很大程度上取决于您的确切文档结构和样式定义。它将找到任何具有包含字符串“color:ff00ff”(确切地说,没有空格等)的样式的tr。然后,它将从该行中选择下一个同级行,并从其所有直接子行中获取文本内容。在您的例子中,这将是单列元素。