Skip to main content
September 23, 2009
Answered

cfxml and empty tags

  • September 23, 2009
  • 2 replies
  • 1459 views

Is there a way to eliminate tags from the xml document if they are empty?

So <myTag></myTag> would not exist in the xml document.  But if there is a value, <myTag>xxx</MyTag> would display.

Thanks!

-Robert

    This topic has been closed for replies.
    Correct answer Adam Cameron.

    I haven't investigated your regex suggestion yet though I'm sure that would be the way to go.

    I decided to do just what you were saying however.  Don't generate the tag at all rather than strip empty ones.

    Is there a better way to do this than what I was thinking...

    <cfif getValues.value1 is not "">

         <firstTag>#value1#</firstTag>

    </cfif>

    That seems cumbersome and inefficient if the xml doc is more than a few tags.


    <cfif getValues.value1 is not "">
         <firstTag>#value1#</firstTag>
    </cfif>
    

    That seems cumbersome and inefficient if the xml doc is more than a few tags.

    Well it's a strange requirement, so probably calls for some cumbersome code :-)

    However I always try to make my conditionals sound "positive":

    <cfif len(getValues.value1)>
         <firstTag>#value1#</firstTag>
    </cfif>

    I guess if you have heaps and heaps of these, then perhaps sticking the blank ones in and then using a regex to get rid of all of them in one hit might be an option.

    Without knowing the data and the XML schema, it's hard to give a well-informed answer though, really.

    --

    Adam

    2 replies

    BKBK
    Community Expert
    Community Expert
    September 24, 2009
    <cfif len(getValues.value1)>
         <firstTag>#value1#</firstTag>
    </cfif>

    GetValues.value1? Say what?! Shouldn't the lingo be rootTag...["firstTag"].XmlText? Suppose someone comes here looking for answers. What the hell will this code say about the removal of empty elements from an XML? Absolutely nothing!

    You have  settled that this question has been ansewered, but it isn't. There is a good -- and important -- business case about removing empty elements from XML.

    Web services typically serve huge XML documents that cater for a large, diverse clientele. When you make a request to a web service, chances are, the response will contain a large number of elements that are empty, because they don't apply to you.

    You might happen to be a specialist messenger who has to deliver the information to someone else. Then you will have to filter out the empty and the irrelevant elements.

    Deleting empty elements from an arbitrary XML isn't trivial. It is similar to the problem of deleting the empty subdirectories from an arbitrary directory, and would almost always involve recursion. For example, a good answer to the problem should be able to reduce this document

    <root>
    <t1>
    <myTag1></myTag1>
    <myTag2>xxx</myTag2>

    <myTag3 attr="1"></myTag3>
    </t1>
    <t2>
    <myTag4 />
    </t2>
    </root>

    to this one

    <root>
    <t1>

    <myTag2>xxx</myTag2>
    </t1>

    </root>

    In my opinion, the best way to do this in Coldfusion is to write a function to remove the empty elements from an arbitrary XML document. Treat the XML as array, then use functions like arrayDeleteAt(). Alternatively, treat it as a structure, and use structDelete().

    There is another consideration in our interconnected world. Search the web. Someone somewhere must have done this before. It'd be a shame spending time to re-invent the wheel, when you could have been spending it with your child, lover, friend or with a good book.

    September 24, 2009

    Thanks BKBK, that was very helpful.

    While it may seem obvious, it never occured to me to approach it from "rootTag...["firstTag"].XmlText".  And it certainly never occured to me to manipulate the xml as a structure of array.  Excellent suggestion.

    I would add to your list of better things to do.  Eating and sleeping.

    Thanks again.

    BKBK
    Community Expert
    Community Expert
    September 24, 2009
    list of better things to do.  Eating and sleeping.

    It doesn't get better than that. Ask kung-fu panda.

    Inspiring
    September 23, 2009

    Why would you want to do that?  I mean, there's plenty of reasons why, but having a better idea of why you want to do it will help answering the question.

    If you have the XML as a string, it's a simple regex find and replace to find empty tags.  Also bear in mind that <mytag /> is a valid way of representing an empty tag as well as <mytag></mytag>.

    --

    Adam

    September 24, 2009

    It's a requirement of the outside people I am creating the xml for.  I am pulling the values for the tags from a sql table and if the value is empty they don't want the tags in the document.  Believe me, I know it's odd but I have no say in the matter.

    Inspiring
    September 24, 2009

    Fair enough.

    But if you're the one generating the XML in the first place, would it not be a better approach to not generate the empty tags in the first place, rather than generate them and then strip 'em out afterwards?

    Did you investigate my regex replace suggestion?

    --

    Adam