Testing For The Absence Of A Text Node Using XmlSearch() And XPath

Posted May 22, 2008 at 9:17 AM by Ben Nadel

Tags: ColdFusion

In my previous post, I explored the issues associated with non-existent text nodes in ColdFusion XML documents. Because non-existent text nodes, like NULL values in SQL, cannot be compared to values for equality or inequality, it sometimes becomes necessary to check for the very existence of a text node. Generally, when we use XPath predicates in XmlSearch(), we are checking too see that other values exist. For example, you might want to get all book nodes that have a nested author node:

//book[ author ]

... or all books nodes whose name attribute is a certain value:

//book[ @name = 'It Was On Fire When I Lay Down On It' ]

Testing for existence is pretty straight forward. Testing for non-existence, on the other hand is not quite as obvious. To explore this idea, let's create a ColdFusion XML Document:

  • <!--- Create a ColdFusion XML document. --->
  • <cfxml variable="xmlGirl">
  •  
  • <girl>
  • <name>Hayden Panettiere</name>
  • <age>18</age>
  • <height></height>
  • <weight></weight>
  • <description>
  • Hayden played Claire, the Cheerleader, on the hit
  • Fox television show, Heroes.
  • </description>
  • </girl>
  •  
  • </cfxml>

As in my previous post, the Height and Weight nodes in the above ColdFusion XML document have no nested text and therefore, the resultant ColdFusion XML Document Object Model (DOM) has no text node within those elements (having nothing to do with the XmlText value). As we saw before, if we wanted to get all element nodes that don't have nested text, we can't check for an empty string:

//*[ text() = '' ]

Non-existent text nodes are not equal to the empty string. They are also not NOT equal to the empty string. String comparison won't help us. Luckily, XPath has a not() function. The not() function takes a true condition and return false; likewise, it will take a false condition and return true. Therefore, we can get all nodes that don't have a nested text node element by "not" getting all nodes that have a nested text node.

That sounds confusing, so let's see it in action:

  • <!--- Get all nodes that do NOT have a nested text node. --->
  • <cfset arrNodes = XmlSearch(
  • xmlGirl,
  • "//*[ not( text() ) ]"
  • ) />
  •  
  • <!--- Output names of nodes. --->
  • <cfloop
  • index="xmlNode"
  • array="#arrNodes#">
  •  
  • #xmlNode.XmlName#<br />
  •  
  • </cfloop>

Here, you can see that we are negating, or NOT-ing, the list of all nodes that have a nested text node. This gives us the following nodes list:

  • height
  • weight

Not() is a pretty cool function. It can be wrapped around more than one condition and can also be used in conjunction with other conditions in a single predicate. For example:

//*[ not( text() ) or (text() != '18') ]

... would select all nodes that do NOT have a nested text node or whose text node value is NOT 18.

This function can also be applied to other types of nodes. For example:

//*[ not( @id ) ]

... would select all nodes that do NOT have an ID attribute.

This was tested in ColdFusion 8.0.1. I know that going from ColdFusion 7 to 8 added some more XPath function support, so I am not sure if this is new or not.




Reader Comments

Oct 23, 2009 at 3:07 PM // reply »
4 Comments

thanks ben!

great post!


dan
Dec 4, 2012 at 7:22 AM // reply »
1 Comments

this is what most people want i believe (and would otherwise spend hours finding this! - like me)

//*[not(text()[normalize-space()])]


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Jun 18, 2013 at 9:20 PM
Mapping AngularJS Routes Onto URL Parameters And Client-Side Events
I couldn't find examples of passing multiple arguments using the when() routing statement so figured out through trial and error that you can pass multiple arguments using the following format: .whe ... read »
Jun 18, 2013 at 3:39 PM
Experimenting With The Amazon Simple Storage Service (S3) API Using ColdFusion
Hi Ben, THANKS! While not bleeding edge, it is new to me & I like learning new things every day! ... read »
Jun 18, 2013 at 12:30 PM
Disabling Auto-Correct And Auto-Capitalize Features On iPhone Inputs
Also spellcheck="false" should be mentioned as part of html5 specs ... read »
Jun 18, 2013 at 8:40 AM
Using Named Functions Within Self-Executing Function Blocks In Javascript
Hi Ben, you forgot to mention the most important thing for named self-executing functions - they can be referenced by name ONLY inside their execution context (which is parens in this case), it mean ... read »
dee
Jun 18, 2013 at 7:01 AM
My Safari Browser SQLite Database Hello World Example
hai ben, this program is really good i could understand the concept but i dint know how to save it and how to open it as you have done in the video can u give that details pls ... read »
Jun 18, 2013 at 6:04 AM
Clearing Inline CSS Properties With jQuery
Thanks a lot for for post! It helped me a lot... after being stuck since 24 hrs.. found solution from your post. Thanks again! ... read »
Jun 18, 2013 at 2:31 AM
SOTR 2013 - The Best Conference I Never Went To
I keep watching it, should keep me happily distracted until SotR14 ;) ... read »
Jun 17, 2013 at 9:45 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, As I was reading what you wrote, it occurred to me that maybe I do something similar to that in some of my client-side code. In an application I'm working on, there are a bunch of unrelated ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools