Copy link to clipboard
Copied
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
<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
...Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
<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
Copy link to clipboard
Copied
Well the xml doc is only about 15 tags so I guess the <cfif>s are fine.I like the positive ifs. That's cool .
Thanks for the helpful suggestions Adam. I appreciate it.
-Robert
Copy link to clipboard
Copied
<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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
list of better things to do. Eating and sleeping.
It doesn't get better than that. Ask kung-fu panda.
Copy link to clipboard
Copied
<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!
Huh? getValues isn't the XML doc, it's the reference data structure sourcing the data for the XML doc. I presume it's a query or a struct; probably a query in a query loop, given the way its being addressed.
You have settled that this question has been ansewered, but it isn't.
Hmmm. There is also the notion of using a sledge hammer to crack a walnut. You make a good point about a generic function to strip hierarchical empty tags, that's for sure.
But in this case, the real answer to the question - if you'd read the thread of the conversation - was that it was completely viable to not put the empty tags in the doc in the first place, so there was therefore no need to remove them.
We are not here to solve the problems of the world, we are here to assist people with their specific problems (and, in my case, try to help them to learn how to solve the problems for themselves... not so relevant in this particular case). I don't think your suggestion for solving this particular issue is the most suitable answer. And your attitude a bit hysterical, to be honest.
--
Adam
Copy link to clipboard
Copied
to be honest.
Never!
We are not here to solve the problems of the world, we are here to assist people with their specific problems (and, in my case, try to help them to learn how to solve the problems for themselves...
And apparently to fill up some missing gap in our own lives by being condescending and patronizing, for sure.
But in this case, the real answer to the question - if you'd read the thread of the conversation - was that it was completely viable to not put the empty tags in the doc in the first place, so there was therefore no need to remove them.
No, that is not the original question. That is how you bent it like Beckam. Yes, I read the question. Question is, did you? Here is a replay:
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.
You cannot say you've answered the question of removing empty tags from an XML if your solution doesn't remove <t1><t2></t2></t1>. That isn't as trivial as you wish it to be, and inevitably involves recursion.
You, of course, end with your traditional attitude when you fail the argument: an insult. Grow up, man, or read up.