1

鉴于此 XML 片段:

<table>
  <tbody>
    <tr>
      <td>
        <strong>Specifications</strong>
      </td>
    </tr>
    <tr>
      <td>Finish</td>
      <td>Black</td>
    </tr>
    <tr>
      <td>Action</td>
      <td>Semi-Automatic</td>
    </tr>
    <tr>
      <td>Caliber</td>
      <td>7.62mmX39mm</td>
    </tr>
  </tbody>
</table>

我正在尝试选择所有tr不包含其Specifications下方任何位置的文本的元素,但我对 XPath 感到恐惧并且无法正确处理。我试过了

//table/tbody/tr[not(contains(text(),'Specifications'))]

但这显然仍然给了我tr包含Specifications.

4

2 回答 2

2

请求的 XPath:

/table/tbody/tr[not(.//text()[contains(.,'Specifications')])]

示例测试程序:

<xsl:transform
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="2.0">
  <xsl:output method="xml" indent="yes" encoding="utf-8"/>

  <xsl:template match="/">
    <table>
      <tbody>
        <xsl:copy-of select="/table/tbody/tr[not(.//text()[contains(.,'Specifications')])]"/>
      </tbody>
    </table>
  </xsl:template>
</xsl:transform>

输出给定您的输入:

<?xml version="1.0" encoding="utf-8"?>
<table>
   <tbody>
      <tr>
         <td>Finish</td>
         <td>Black</td>
      </tr>
      <tr>
         <td>Action</td>
         <td>Semi-Automatic</td>
      </tr>
      <tr>
         <td>Caliber</td>
         <td>7.62mmX39mm</td>
      </tr>
   </tbody>
</table>
于 2013-09-15T02:33:35.060 回答
1

//table/tbody/tr[not(contains(td, 'Specifications'))] # 不好的一个

//table/tbody/tr[not(contains(., 'Specifications'))] # 更好

于 2013-09-15T02:12:48.960 回答