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">
- <name>Hayden Panettiere</name>
- Hayden played Claire, the Cheerleader, on the hit
- Fox television show, Heroes.
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(
- "//*[ not( text() ) ]"
- ) />
- <!--- Output names of nodes. --->
- #xmlNode.XmlName#<br />
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:
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.
Looking For A New Job?
- ColdFusion Web Developer at 1800Flowers.com/FineStationery.com
- Looking for a Technical Co-Founder at Live Storefronts
- ColdFusion Developer at Clayton Homes
- Sr. Coldfusion GURU at eXcelaweb
this is what most people want i believe (and would otherwise spend hours finding this! - like me)