带有后代和后代text()谓词的XPath查询

我想构造一个XPath查询,它将返回一个“div”或“table”元素,只要它有一个包含文本“abc”的后代。一个警告是它不能有任何div或table后代。
<div>
  <table>
    <form>
      <div>
        <span>
          <p>abcdefg</p>
        </span>
      </div>
      <table>
        <span>
          <p>123456</p>
        </span>
      </table>
    </form>
  </table>
</div>
所以这个查询唯一正确的结果是:
/div/table/form/div 
我最好的尝试看起来像这样:
//div[contains(//text(), "abc") and not(descendant::div or descendant::table)] | //table[contains(//text(), "abc") and not(descendant::div or descendant::table)]
但不会返回正确的结果。 谢谢你的帮助。     
已邀请:
有些不同: :)
//text()[contains(.,'abc')]/ancestor::*[self::div or self::table][1]
似乎比其他解决方案短很多,不是吗? :) 翻译为简单英语:对于文档中包含字符串
"abc"
的任何文本节点,选择其第一个祖先是
div
table
。 这样更有效,因为只需要对文档树进行一次完整扫描(而不是任何其他),并且与
descendent::
(树)扫描相比,
ancestor::*
遍历非常便宜。 要验证此解决方案“确实有效”:
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/">
  <xsl:copy-of select=
  "//text()[contains(.,'abc')]/ancestor::*[self::div or self::table][1] "/>
 </xsl:template>
</xsl:stylesheet>
在提供的XML文档上执行此转换时:
<div>
  <table>
    <form>
      <div>
        <span>
          <p>abcdefg</p>
        </span>
      </div>
      <table>
        <span>
          <p>123456</p>
        </span>
      </table>
    </form>
  </table>
</div>
产生了想要的正确结果:
<div>
   <span>
      <p>abcdefg</p>
   </span>
</div>
注意:没有必要使用XSLT - 任何XPath 1.0主机(如DOM)都必须获得相同的结果。     
你可以尝试:
//div[
  descendant::text()[contains(., "abc")] 
  and not(descendant::div or descendant::table)
] | 
//table[
  descendant::text()[contains(., "abc")] 
  and not(descendant::div or descendant::table)
]
这有帮助吗?     
//*[self::div|self::table] 
   [descendant::text()[contains(.,"abc")]]  
   [not(descendant::div|descendant::table)]
contains(//text(), "abc")
的问题在于函数转换节点集占用第一个节点。     

要回复问题请先登录注册