Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XPath expression to test for empty node?

Reply
Thread Tools

XPath expression to test for empty node?

 
 
n_o_s_p_a__m@mail.com
Guest
Posts: n/a
 
      05-06-2005
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

 
Reply With Quote
 
 
 
 
KJ
Guest
Posts: n/a
 
      05-06-2005
I meant (in the 2nd example) <title/>

 
Reply With Quote
 
 
 
 
David Carlisle
Guest
Posts: n/a
 
      05-06-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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



 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      05-07-2005


(E-Mail Removed) 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/
 
Reply With Quote
 
KJ
Guest
Posts: n/a
 
      05-07-2005
thank you very much!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Seek xpath expression where an attribute name is a regular expression GIMME XML 3 12-29-2008 03:11 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C Programming 45 11-04-2008 12:39 PM
"Memory leak" in javax.xml.xpath.XPath Marvin_123456 Java 4 07-29-2005 03:49 PM
test test test test test test test Computer Support 2 07-02-2003 06:02 PM



Advertisments