Look at the following two examples:
<foo>some text <bar/> and maybe some more</foo>
and
<foo>some text <bar/> and a last <bar/></foo>
Mixed text nodes and bar elements within the foo element. Now I am in foo, and want to find out if the last child is a bar. The first example should prove false, as there are text after the bar, but the second example should be true.
How can I accomplish this with XSLT?
Just select the last node of the
<foo>element and then useselfaxis to resolve the node type.This XPath expression returns an empty set (which equates to boolean false) if the last node is not an element. If you want to specifically get value
trueorfalse, wrap this expression in the XPath functionboolean(). Useself::*instead ofself::barto match any element as the last node.Input XML document:
XSLT document example:
Output of the stylesheet: