OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index]
xslt example for cloning / manipulating XML structure parts / content / namespaces

In the past I've seen requests for how to handle complex XML transactions in plain old XML v1.0 systems that cannot handle namespaces and other nuances.

Other folks may need to extract out just part of a XML transaction, or refactor an incoming message before relaying it on.

All of this is possible using xslt of course - but a handy example of excluding parts / namespaces and manipulating the structure tree - is hard to come by.

I'm sharing here a utility that can perform various manipulations via xslt.  You can easily add in other re-factoring - such as correcting date formats, removing empty elements, and so on.

It's setup to work with a sample UBL Waybill - such as the one here:
 http://docs.oasis-open.org/ubl/os-UBL-2.0/xml/UBL-Waybill-2.0-Example-International.xml

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.

Yes - you can easily combine this with using CAM templates too - via jCAM utility - since CAM supports use of xslt as a content transformation agent.

Enjoy, DW

=========================================
<?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:attribute-set name="unitCodes">
      <xsl:attribute name="unitCode"><xsl:value-of select="./@unitCode"/></xsl:attribute>
  </xsl:attribute-set>
  
 <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::*) &gt; 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::*) &gt; 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
-->


[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index]