UDF: xmlDelete() for CFMX

Posted by Dan on May 28, 2004 @ 11:42 PM

I needed a way to filter out nodes from an XML document in CFMX. XPath is a great mechanism for finding nodes, so I decided to throw together a little UDF which would allow me to delete any nodes in an XPath expression. Here's the UDF:

UDF: xmlDelete(xml, xpath)
Author: Dan G. Switzer, II
Date: 5/27/2004

xml - the xml document to delete nodes from
xpath - the xpath statement which defines the nodes to delete

function xmlDelete(oXml, sXpath){
    var oNodes = xmlSearch(oXml, sXpath);
    var bNodesFound = iif(arrayLen(oNodes) gt 0, true, false);
    while( arrayLen(oNodes) GT 0 ){
        arrayDeleteAt(oNodes, 1);
    return bNodesFound;

If you need to delete all the nodes which have an attribute of "active" where the value is "false", you can use the follow:

xmlDelete(xmlObject, "//*[@active="false"]");

The xmlObject will now have all of the inactive nodes removed from it.

Categories: HTML/ColdFusion


  • Dan,

    Got a question for you actually. I was working on something like this, but gave because I couldn't get the XPath right.

    I'm trying to remove nodes where the parent element contains an attribute.

    Imagine this:

    [group id="1"]
     [item id="2" /]
     [item id="3" /]
     [item id="4" /]
     [item id="5" /]
    [group id="2"]
     [item id="6" /]
     [item id="7" /]

    I wanted to actually delete say item 6 if group 2 wasn't to be include. That make any sense?

  • Shouldn't this work:
    xmlDelete(oXml, "/groups/group[@id='2']/item[@id='6']");

    Obviously, nothing in your example shows *how* we know that group 2 wasn't to be included, but I'm assuming you just know you need to delete the node.

Comments for this entry have been disabled.