469,366 Members | 2,205 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,366 developers. It's quick & easy.

XPath expression to test for empty node?

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

Jul 20 '05 #1
4 58721
KJ
I meant (in the 2nd example) <title/>

Jul 20 '05 #2
n_**********@mail.com writes:
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
title elements that are the same node as their child <null> elements,
which can't happen.
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
</xsl:template>

Any suggestions welcome.

Thank you,
-KJ

Jul 20 '05 #3


n_**********@mail.com wrote:
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).

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 20 '05 #4
KJ
thank you very much!

Jul 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Tjerk Wolterink | last post: by
3 posts views Thread by gimme_this_gimme_that | last post: by
2 posts views Thread by gimme_this_gimme_that | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.