对于“匹配”使用:
//*[text()[normalize-space()] and not(../text()[normalize-space()])]
对于“可能匹配”,请使用:
//*[../text()[normalize-space()]]
基于 XSLT 的验证:
<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()[normalize-space()] and not(../text()[normalize-space()])]"/>
==========
<xsl:copy-of select="//*[../text()[normalize-space()]]"/>
</xsl:template>
</xsl:stylesheet>
当对提供的 XML 应用此转换时(包装到单个顶部元素中以成为格式良好的 XML 文档):
<t>
<table class="dont-match">
<tr class="dont-match">
<td class="match">Mixed <strong class="maybe-match">content</strong> in here.</td>
<td class="match">Plain text in here.</td>
<td class="dont-match"><img src="..." /></td>
</tr>
</table>
<div class="dont-match">
<div class="dont-match"><img src="..." /></div>
<div class="match">Mixed <em class="maybe-match">content</em> in here.</div>
<p class="match">Plain text in here.</p>
</div>
</t>
计算两个 XPath 表达式中的每一个,并将选定的节点复制到输出:
<td class="match">Mixed <strong class="maybe-match">content</strong> in here.</td>
<td class="match">Plain text in here.</td>
<div class="match">Mixed <em class="maybe-match">content</em> in here.</div>
<p class="match">Plain text in here.</p>
==========
<strong class="maybe-match">content</strong>
<em class="maybe-match">content</em>
正如我们所看到的,这两个表达式都准确地选择了想要的元素。