我想构造一个 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
or a table
.
这样效率更高,因为只需要对文档树(而不是任何其他)进行一次完整扫描,并且ancestor::*
与a相比,遍历非常便宜descendent::
(树)扫描。
要验证此解决方案“确实有效”:
<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>
Note:没有必要使用 XSLT——任何 XPath 1.0 宿主——比如 DOM,都必须获得相同的结果。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)