How can I check if a XML node contains text, or only empty nodes?
Example: Let’s say we have the following XML:
<text>
<p> </p>
<p> </p>
</text>
(Note the whitespace between the p tags)
In a different XML, we have the following XML:
<text>
<p>Hello World!</p>
</text>
I’d like the test to pass in the second example, but not in the first, as the second example contains text, but the first contains empty nodes.
Is there a way to easily achieve this?
(I use XSLT 2.0.)
If you put
at the top of your style sheet then the parser will ignore text nodes in your input document that consist entirely of whitespace. Among other things this means that your example
pelements with spaces will be treated the same as completely empty ones. For example a template matchingtext[p/text()]would match thetextelement in your second example but not ones whosepchildren are all empty (or contain only space).