Remove XML Tags if empty

Hi Folks,

I have a query regarding removing empty tags values

After convertToString step, I need to remove below values in XML string if exist

For Instance:

Scenario_1:
%address%

Scenario_2:
<address/>

Scenario_3:
<address></address> <!— No values coming—>

I don’t need the above empty tags as they’re not required in final output

Attached is the current & expected OP

Could you please share your suggestions?

Current And Expected OP.png

Mohammed,

First, I’ll give you the answer you’re looking for but then I’ll propose something slightly different.

The answer:

First, when it comes to dropping empty elements, you’re better off dropping them before converting the document to a string.

Second, how to handle the %address% scenario: don’t! Variable substitution can be useful if the source variable has a value. If it doesn’t, then you end up with this garbage in your target variable. Avoid using variable substitution if you can. If you can’t, make sure you assign a default to the source variable (even if you assign an empty string) so that you’re guaranteed a value in the target variable. Setting a default is as easy as assigning a value to the variable and unchecking the “Overwrite pipeline value” checkbox. This ensures that the default is only assigned if the variable doesn’t already have a value.

Now, how to get rid of the empty elements. If you’re concerned with more than just a few elements, then you’re better off writing a Java service that traverses through your IData, and if it finds an element that is empty, it deletes that element from the document. You may have to use recursion if you have a nested data structure.

However, if you’re only concerned with a few elements (e.g. address1, address2, and address3), then you should be able to use simple BRANCH steps that look at the value of those variable, and if they are empty, it drops the variable. You can use a simple expression that evaluates whether the variable is an empty string or you can use a regular expression to also account for spaces (more on this below).

The better solution:

I’m assuming that this document that you’re converting to XML was mapped from a different source, is that correct? If so, then how about you get rid of the empty strings before they even get into your target document? In other words, as you’re mapping the data from the source structure to your target document type, you can use a Copy Condition in the MAP link to make sure that you only copy the data from the source to the target if it’s not empty. This would solve all of your problems above.

You can use the same regular expression I mentioned above to ensure you don’t copy empty strings (even if they contain spaces, tabs, etc). Check out this thread: http://tech.forums.softwareag.com/techjforum/posts/list/43519.page. Rupinder provides the ultimate regex at the end of page 1. It appears another user created a similar Java service to what I described above in page 2.

Hope this helps,
Percio

1 Like

Hi Percio Castro,

Thanks a lot for your feedback…