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!


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
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 10, 2012 at 7:21 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
Update! Instead of $(eval(options.insertAfter)).after(data['insertData']); I now use: var ajaxNode = document.createElement('span'); var parent = $(eval(options.insertAfter))[0].parentNode; ... read »
Feb 10, 2012 at 6:18 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
encountered this same, what I consider, jQuery bug last week. I'm building a site in which I load some content via AJAX. This content contains Linkedin share button placeholders which Linkedin API ne ... read »
Feb 10, 2012 at 11:30 AM
Cross-Origin Resource Sharing (CORS) AJAX Requests Between jQuery And Node.js
After you understand the concepts here, this is an awesome cheatsheet for enabling CORS in just about anything http://enable-cors.org/ ... read »
JM
Feb 10, 2012 at 9:10 AM
My Safari Browser SQLite Database Hello World Example
@Amy, Here is a very good tutorial on how to use JOIN: http://www.sqltutorial.org/sqljoin-innerjoin.aspx ... read »
Feb 10, 2012 at 4:42 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
This is great, very useful Ben. I spotted a small typo in the api.cgm listing: <cfthrow type="Unauthroized" /> Cheers Stefan ... read »
Feb 9, 2012 at 10:35 PM
CFDirectory Filtering Uses Pipe Character For Multiple Filters (Thanks Steve Withington)
I was wondering if there would be a filter you could apply so that you got everything but what you included in the filter. As in show me all docs that are not a .pdf. ... read »
Feb 9, 2012 at 10:29 PM
Learning ColdFusion 9: Application-Specific Data Sources
@Ben, No offence, but if people were really wanting advanced features they would be using a platform like ASP.NET MVC. CFML is so structurally compromised as a tag-based scripting language that ... read »
Feb 9, 2012 at 10:03 PM
Subversion - Cleanup Failed To Process The Following Paths
@Leviaguirre, do you still have problems with this? ... read »