选择不包含给定text()的任何子节点的节点?

时间:2013-09-15 01:32:08

标签: xml xpath

鉴于此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的{​​@ 1}}元素,但我对XPath感到很恐怖并且无法正确使用它。我试过了

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

但显然仍然是我的tr节点包含Specifications

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:1)

// table / tbody / tr [not(contains(td,'Specifications'))]#bad one

// table / tbody / tr [not(contains(。,'Specifications'))]#better