views:

287

answers:

2

I'm new to XSLT and I can't figure out how to get an xsl:if that matches when there are no child tags.

I want this to match:

<context>
    howdy
</context>

And this not:

<context>
    <child>
        howdy
    </child>
</context>
+2  A: 

the relevant xpath expression should look like:

//context[not(./*)]
Jonathan Fingland
Sweet thanks! I was trying things with text(). I would have thought * matched text nodes also...
Adam A
./* is the same as just *, so you can leave out the "./" part, as in: //context[not(*)]. Also, "*" is short for "child::*", and the "principal node type" for the child axis is elements. Thus it only selects elements. Similarly, "@*" (which is short for "attribute::*") only selects attributes.
Evan Lenz
A: 

You could also specify count(child::*) = 0 .

AmbroseChapel