IBM Support

How to output human readable/formatted XML from IIB.

Question & Answer


Question

How to output human readable/formatted XML from IIB.

Answer

Outputting formatted XML from IIB.


Although XML is meant to be machine readable - not human readable - we occasionally come across a requirement for IIB to output XML that is made human readable by starting each element? on a new line. For example rather than outputting a XML file as a single line like like this:

<callMessage><emp_num>000010</emp_num><last_name>Smith</last_name><first_name>Bill</first_name></callMessage>

The requirement is for the XML to be output like this:

<callMessage>
<emp_num>000010</emp_num>
<last_name>Smith</last_name>
<first_name>Bill</first_name>
</callMessage>

IIB does not provide a switch or property to enable this formatting. This means that we need to write a message flow or add additional function to an existing message flow to accomplish this. Writing ESQL to do this would get very messy quickly, the simplest approach is to use an IIB XSL Transform node to execute a simple XSL style sheet. Details of the IIB XSL Transform node can be found here:

http://www.ibm.com/support/knowledgecenter/en/SSMKHH_10.0.0/com.ibm.etools.mft.doc/bz90210_.htm

A simple message flow to carry out the transformation looks like this:


In this example the output needed to go to a file but it could go to an MQ Output node instead. In this message flow the MQ Input node uses the XMLNSC parser domain.


The XSL Transform node needs to have an XSL Style Sheet associated with it in much the same way as an ESQL compute node needs an *.esql file associated with it. Double clicking on the XSL Tranform node in the message flow editor opens the associated *.xsl file for editing. If an *.xsl file has not been associated with the node then you are prompted to select an *.xsl from the application or associated libraries. The node?s stylesheet *.xsl file can also be selected via the ?Stylesheet? tab in the XSL Transform?s properties:



In this case the samle.xsl Stylesheet file looks like this:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform&quot;>
?<xsl:output omit-xml-declaration="yes" method="xml" indent="yes" encoding="UTF-8"/>
?<xsl:strip-space elements="*"/>
?<xsl:template match="/">
? <xsl:copy-of select="."/>
?</xsl:template>
</xsl:stylesheet>

This simple stylesheet copies in the input xml to the output unchanged except that each element is on a new line ? this is achieved by setting indent="yes".
If we have the following sample input xml message:

<callMessage><emp_num>000010</emp_num><ratingResult>2</ratingResult></callMessage>

If this xml message is processed by the message flow show previously ?using the sample.xsl listed above the output file will look be as follows:
<callMessage>
<emp_num>000010</emp_num>
<ratingResult>2</ratingResult>
</callMessage>

Which is fine and what the basic requirement was but what if we have the following xml message as input:

<callMessage><emp_num>000010</emp_num><last_name>Smith</last_name><first_name>Bill</first_name><home_address><house_number>23</house_number><street>High Street</street><city>London</city><postcode>X1A 1AS</postcode></home_address></callMessage>

Which has nested elements if we run this through the samplexsl via our message flow the out xml file is:

<callMessage>
<emp_num>000010</emp_num>
<last_name>Smith</last_name>
<first_name>Bill</first_name>
<home_address>
<house_number>23</house_number>
<street>High Street</street>
<city>London</city>
<postcode>X1A 1AS</postcode>
</home_address>
</callMessage>

Which meets the requirement but is not easy to read. This can be addressed by amending the stylesheet thus:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform&quot; xmlns:xalan="http://xml.apache.org/xslt&quot;>
?<xsl:output omit-xml-declaration="yes" method="xml" encoding="UTF-8" indent="yes" xalan:indent-amount="3"/>
?<xsl:strip-space elements="*"/>
?<xsl:template match="/">
? <xsl:copy-of select="."/>
?</xsl:template>
</xsl:stylesheet>

Notice the: xalan:indent-amount="3" and its associated xalan namespace declaration. If we deploy the changes and rerun the same message through the message flow we will see the following in the output file:

<callMessage>
?? <emp_num>000010</emp_num>
?? <last_name>Smith</last_name>
?? <first_name>Bill</first_name>
?? <home_address>
????? <house_number>23</house_number>
????? <street>High Street</street>
????? <city>London</city>
????? <postcode>X1A 1AS</postcode>
?? </home_address>
</callMessage>

Which is much easier to read by humans!

?"

[{"Business Unit":{"code":"BU053","label":"Cloud & Data Platform"},"Product":{"code":"SSQTW3","label":"IBM On Demand Consulting for Hybrid Cloud"},"Component":"","Platform":[{"code":"PF025","label":"Platform Independent"}],"Version":"All Versions","Edition":"","Line of Business":{"code":"","label":""}}]

Document Information

Modified date:
08 December 2018

UID

ibm10776183