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

Posted May 22, 2008 at 9:17 AM

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:

 Launch code in new window » Download code as text file »

  • <!--- 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:

 Launch code in new window » Download code as text file »

  • <!--- 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.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page




Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

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

thanks ben!

great post!


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 20, 2009 at 11:32 PM
Five Months Without Hungarian Notation And I'm Loving It
I've used headless camel case for years for not only ColdFusion variables, but also SQL tables and fields... pretty much everything involving code. I also subscribe to the "don't abbreviate and clea ... read »
Nov 20, 2009 at 11:00 PM
Five Months Without Hungarian Notation And I'm Loving It
@Marcel, Yeah, I always err on the side of longer but more readable variable names. As for the camel casing of CF methods and the headless camel casing of custom items, I get around this by always ... read »
Nov 20, 2009 at 10:56 PM
Five Months Without Hungarian Notation And I'm Loving It
I use the following and love it: my.namespace.MyComponents.functionMethodsOrUDF() CONSTANT_VALUES_OR_PROPERTIES One thing I always try is to CamelCaseBuiltInColdFusionFunctions() so others can tell ... read »
Nov 20, 2009 at 5:38 PM
Learning ColdFusion 8: CFImage Part I - Reading And Writing Images
Hi Ben, Great article. I've been looking around to see if ColdFusion image engine can programatically create the following "wrap around" effect: http://www.creativepro.com/article/photoshop-s-she ... read »
Nov 20, 2009 at 5:35 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Dave: I talked to Gert he suggested: <cfhttp method="get" url="http://{some cf website}" result="stuff" addtoken="yes" /> Note the addition of cfhttp attribute addtoken. That should persist y ... read »
Nov 20, 2009 at 5:23 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Todd, Ahh, gotcha, yeah that makes sense. ... read »
Nov 20, 2009 at 5:17 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
Ben, sorry if I didn't make this clear. You can make it work like that if you want, just put <cfset session.foo = 1> (and <cfset application.foo = 1>) in your OnRequestStart() and it reve ... read »