Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I test if a text node is empty?

Tags:

xpath

I've got an XML file with this structure:

<entry id="1">
    <para>first paragraph</para>
    <para>second paragraph</para>
</entry>
<entry id="2">
    <para></para>
</entry>

My XSL needs to do something if it finds an entry has a first element whose text node is empty (entry id="2" in the example). I've tried this:

<xsl:when test="(entry/para[1]/text()='')">

but that doesn't seem to work.

A similar code snippet to read an attribute of the element does work:

<xsl:when test="entry[1]/para[@stylename = 'Table Heading']">

So I'm doing something wrong with the text()='' selection. A search suggests I should use [not(text())] but I can't figure out how to integrate this in my code.

like image 418
Hobbes Avatar asked Jan 08 '14 13:01

Hobbes


1 Answers

text()='' requires a text node to be there, but in the case of <para></para>, there is no text node to be matched.

So, I'd suggest something like

entry/para[1][.='']

or

entry/para[1][not(text())]
like image 73
Thomas W Avatar answered Oct 15 '22 19:10

Thomas W