查找具有特定子元素但没有文本节点的元素

Jen*_*ann 5 xslt xpath

对于XSLT模板,我需要一个XPath表达式,目标元素具有特定的子元素,但没有文本节点.

<!-- 1: matches -->
<a><b/></a>

<!-- 2: does not match -->
<a>foo bar quux<b/></a>

<!-- 3: does not match -->
<a>whatever <b/> further text</a>

<!-- 4: does not match -->
<a>whatever <b/> further text <b/></a>

 <!-- 5: does not match -->
<a><x/><b/></a>

<!-- 6: does not match -->
<a>foo bar quux</a>
Run Code Online (Sandbox Code Playgroud)

我提出了a[*[1 = last() and name() = 'b']],但是当他们不应该匹配案例2或3.当然,原因是*选择元素而不关心文本节点.那我该怎么做呢?

Dim*_*hev 2

a[b and not(text() or *[2])]
Run Code Online (Sandbox Code Playgroud)

这将选择a当前节点(上下文节点)的每个具有子节点b且没有任何文本节点子节点或第二个元素子节点的子节点。

如果您也不希望有任何 PI 或评论子项,您可以使用较短的版本:

a[b and not(node()[2])]
Run Code Online (Sandbox Code Playgroud)