Using xslt and CAM
Overview
You can easily combine xslt transformations with CAM templates. The jCAM tool implementation supports this via the included saxon xslt processor. The particular advantage is that you can have a single processing step completing rules validation and then results transformation together.
A classic example use is generating HTML reports from XML transactions - this use case you can see in the EDXL samples page. When used with CAM - the template checks that the XML transaction content is correctly formatted - and then calls the xslt transformation to create the HTML report.
Conversely jCAM also supports exception handling - so a rule could be placed in the CAM template that detects a specific exception condition - and then calls another xslt to report that specific condition.
It should be noted that the output format produced from a CAM processor includes in-line error messaging and flagging of specific content - so this makes it easy to have an xslt script that reports these. An example is included in the jCAM package.
Further another example is detecting errors in a received XML transaction - and then reporting those back to the sender. This is the classic EDI 997 acknowledgement mechanism. Again - by combining xslt with CAM templates you can first detect the specific error(s), flag them in the resulting output structure - and then build a return XML transaction using xslt - to report those back.
Manipulating XML transactions
Another use case is post-processing XML to correct problems in the structure. While CAM does allow you to create rules that can accept a wide varience in the details of structures, including version differences, a specific partner may require only exactly one format of a transaction.
In that case - you may apply xslt to adjust the structure. CAM templates also allow you to produce a pruned, post-process output. By combining these techniques you have full control over such post-validation outputting.
Example xslt for manipulating XML transaction
This xslt is provided "as is" as a handy resource for experimenting with xslt and CAM transformations.
It is setup to work with a UBL Waybill transaction - such as the one here.
You can easily add in other re-factoring - such as correcting date formats, removing empty elements, and so on.
You can experiment by changing the selection XPath in the code below - see comments in-line in the code. By default is pulls out the Shipment details and strips the namespaces off that content.
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" exclude-result-prefixes="cac env" version="1.0">
<!-- NOTE: only declare the namespace needed for selection XPath - otherwise the others may be output -->
<xsl:template match="*"> <done><!-- this tag is just a place holder in case you need well formed result --> <xsl:for-each select="//cac:Shipment/*"><!-- put your selection target here; if you put "/" will get everything --> <xsl:call-template name="doIt"> <xsl:with-param name="this" select="."/> </xsl:call-template> </xsl:for-each> </done> </xsl:template>
<xsl:template name="doIt"> <xsl:param name="this"/> <xsl:element name="{local-name($this/.)}"> <xsl:if test="count($this/attribute::*) > 0"> <xsl:call-template name="putAttribs"> <xsl:with-param name="attribs" select="$this/attribute::*"/> </xsl:call-template> </xsl:if> <xsl:if test="count($this/descendant::*) = 0"> <xsl:value-of select="$this/node()"/> </xsl:if> <xsl:if test="count($this/descendant::*) > 0"> <xsl:for-each select="$this/child::*"> <xsl:call-template name="doIt"> <xsl:with-param name="this" select="."/> </xsl:call-template> </xsl:for-each> </xsl:if> </xsl:element> </xsl:template>
<xsl:template name="putAttribs"> <xsl:param name="attribs"/> <xsl:for-each select="$attribs"> <xsl:attribute name="{local-name()}"><xsl:value-of select="."/></xsl:attribute> </xsl:for-each> </xsl:template> </xsl:stylesheet>
<!-- Provided "as is" License: Creative Commons - http://creativecommons.org/licenses/by-sa/3.0/ Author: David RR Webber, February, 2008 -->
CAM Wiki