Monday, August 25, 2014

Strip your XML code for empty nodes




You all must be working with big big xml documents in our complex integration process. Specially if you are using AIA then you will come across huge payload structure. Sometimes i think AIA metadata are a totally waste but that is my views. We often come across situation where we are using huge meta data from AIA and then we just need to pass only few element out of it. In those cases it becomes totally redundant to send all those empty nodes which we are not using.

This will not only create a burden on the BPEL engine to process such a huge payload but also it will make the reading of data unclear to the administrator. So it is always a best practice to truncate/strip your xml data so that all the empty nodes can be removed.

In this exercise we will see how we can strip our xml code.

the magic will be done using the following piece of code

<xsl:output method="xml" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:if test=". != '' or ./@* != ''">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:if>
</xsl:template>



Further there are some other version of the same code which can be used to strip the XML code

<?xml version = '1.0'?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" >
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:if test=". != '' or ./@* != ''">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:if>
</xsl:template>
</xsl:stylesheet>


<?xml version = '1.0'?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@*[.='']"/>
<xsl:template match="*[not(node())]"/>
</xsl:stylesheet>


You may use any one of them if one of them doesn't give you the desired output.

Now lets see an example of how this can be used to truncate the empty tags from xml element.


I will come up with a very bad example but that will clear your doubt on how to use this code.

I have this weird schema input where in we have to pass 9 combination of user/pwd. In ideal case only one of the user/pwd will be required so rest of them are redundant so in this exercise we will how we can remove the redundant element..

Just to give you an idea this is how my schema for input will look like

<element name="process">
<complexType>
<sequence>
<element name="Username" type="string"/>
<element name="Password" type="string"/>
<element name="Username1" type="string"/>
<element name="Password1" type="string"/>
<element name="Username2" type="string"/>
<element name="Password2" type="string"/>
<element name="Username3" type="string"/>
<element name="Password3" type="string"/>
<element name="Username4" type="string"/>
<element name="Password4" type="string"/>
<element name="Username5" type="string"/>
<element name="Password5" type="string"/>
<element name="Username6" type="string"/>
<element name="Password6" type="string"/>
<element name="Username7" type="string"/>
<element name="Password7" type="string"/>
<element name="Username8" type="string"/>
<element name="Password8" type="string"/>
</sequence>
</complexType>
</element>



Now let me try to use this to truncate the empty element. the assumption here is that the actual usename/pwd will come in the first two elements.

Lets first create a BPEL process out of this schema as an input parameter.

Now create variable of the same type as that of the input element




Create a transformation between the input element and the new variable that you have created.



In the transformation thus create copy and paste the code that we have used earier




Now just test your instance and you will get following results

The input variable that you passed has all the empty tag but after transformation it has only two fields

1 comment:

Unknown said...

Thank you.
Very usefull. It helped me to solve my bugs.