Problem:
If an XML document contains empty elements such as the RemoveThis element in:-
<?xml version="1.0"?>
<customer>
<first_name>Joe</first_name>
<last_name>Smith</last_name>
<RemoveThis></RemoveThis>
</customer>
then how can you remove then from the XML Document whne running COBOL under the .Net Framework.
Resolution:
One approach to doing this would be to apply a XSLT Transformation to the XML Document. Attached to this document is a demo showing how you can do this in managed .Net Visual COBOL The .Net Framework provides a number of classes for XML and allows a developer to apply XSLT transformations to XML.
The XSLT Transform being applied is:-
<xsl:sylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xs:template match="@*|node()">
<sl:if test=". != '' or ./@* != ''">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
<xsl:if>
</xl:template>
</xslstylesheet>
The Demo shows how to apply the technique to both an in-memory XML doc and also on a file containing XML. Supplied with the code is a batch file runit.bat that will build and run the demo.
In the code the definitons of class types has been declared using code as:-
01 xmlxslt type System.Xml.Xsl.XslCompiledTransform.
This has been done to show the .Net namespace where the class exists. The code can be simplified with the ILUSING directive to specify the namespace:-
$set ilusing(system.Xml.Xsl)
....
01 xmlxslt type XslCompiledTransform.
With the ilusing directive the namespace can be ommitted from the code.
Attachments
#.netCOBOLXMLmanaged
#COBOL