XPath expression to test for empty node?

N

n_o_s_p_a__m

My xml doc has many <title></title> and <title> in it, meaning the
nodes have no content (although some do). How can I test for this?

I tried title[. is null] (doesn't work)
I tried //title[node() is null] (doesn't work)
I tried //title[text() is null] (doesn't work)
I tried //title[text() = ''] (doesn't work)

Any suggestions welcome.

Thank you,
-KJ
 
D

David Carlisle

My xml doc has many <title></title> and <title> in it, meaning the
nodes have no content (although some do). How can I test for this?

This is a faq you should csee the xsl-list faq at www.dpawson.co.uk
I tried title[. is null] (doesn't work)
the is operator is only in XPath2 which is still in draft form
that is legal but always selects an empty sequence as it selects all
I tried //title[node() is null] (doesn't work)
That is again XPath2 only and is the same as //[null] and selects all
title elements that have a child element called null.
I tried //title[text() is null] (doesn't work)

that again is xpath2 only and is the empty sequence, it selects all
title elements which have a text node which is an element node with name
null. text nodes are never element nodes so this selects nothing.
I tried //title[text() = ''] (doesn't work)
This one is valid XPath1 but selects nothing as it selects all title
elements that have a text node with value '' but text nodes are never
empty in Xpath, an empty node has no child at all, not a text node with
empty string value.

To test if the current element has no children you just need
test="not(node())"

to match on empty title elements you could do
<xsl:template match="title[not(node())]">
empty title was here
 
M

Martin Honnen

My xml doc has many <title></title> and <title> in it, meaning the
nodes have no content (although some do). How can I test for this?

I tried title[. is null] (doesn't work)
I tried //title[node() is null] (doesn't work)
I tried //title[text() is null] (doesn't work)
I tried //title[text() = ''] (doesn't work)

Use
//title[not(text())]
for <title> elements which have no text content.
Or use
//title[not(node())]
for <title> elements which have no child nodes at all (i.e. which are
empty).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,246
Members
46,843
Latest member
WizcraftEntertainmentAgen

Latest Threads

Top