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
May 17, 2013 at 7:42 PM
HashKeyCopier - An AngularJS Utility Class For Merging Cached And Live Data
Ben - thanks so much for posting these Angular articles and findings, they've been a huge help towards learning one of the more 'complex' JavaScript frameworks out there (IMO). I have been using Angu ... read »
May 16, 2013 at 5:01 PM
UPDATE: Parsing CSV Data Files In ColdFusion With csvToArray()
Your code was the closest thing I've found to obtaining some direction for converting ISO fields to values that CF can translate properly. Thank you for posting! ... read »
May 15, 2013 at 10:37 PM
Very Simple Pusher And ColdFusion Powered Chat
hi id making plz easy ... read »
May 15, 2013 at 6:07 PM
Making SOAP Web Service Requests With ColdFusion And CFHTTP
Ben, you once again saved my bacon at work. Thank you, thank you, thank you! ... read »
May 15, 2013 at 4:15 PM
What If All User Interface (UI) Data Came In Reports?
@Josh, Thanks! @Ben, I definitely recommend the David West book "Object Thinking" I've been quoting from. It goes deeply into the philosophy and history of OO programming. His breadth ... read »
May 15, 2013 at 11:36 AM
Ask Ben: Print Part Of A Web Page With jQuery
I found this helpfull when you need to keep (refresh) the original parent page after closing the iframe child print dialog (Hoping you're not using a form at this time so it won't submit again): On ... read »
May 14, 2013 at 7:13 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, If there's any books you'd recommend on the subject of domain modelling, I'd love to hear it. I just downloaded the free PDF of "Domain Driven Design Quickly". Figured I'd give it ... read »
May 14, 2013 at 6:57 PM
The UX Of Prototyping: Low-Fidelity Is The New High-Fidelity
@Phillip, I'm not sure I follow what you mean? Are you saying that you looked at the list of widgets provided by the jQuery UI and let that be your style guide? ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools