XML Transformation

API 5.24 offers support for XML transformations for any of the resource of the data model.

Using XSLT 3.0 is possible inject XSL strings to any GET request to translate the default JSON response in a XML string based on the XSL in input.

To achieve this the URLs to use are

  • /ws/rest/{ResourceName}/xml

  • /ws/rest/{ResourceName}/xml/{key}

the XSL must be set as HTTP header parameter with the name xsl.

Example of Cargo transformation:

XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0" xpath-default-namespace="http://www.w3.org/2005/xpath-functions">
   <xsl:output indent="yes" />
   <xsl:strip-space elements="*" />
   <xsl:param name="json" />
   <xsl:mode on-no-match="deep-skip" />
   <xsl:template name="init">
      <xsl:apply-templates select="json-to-xml($json)" />
   </xsl:template>
   <xsl:template match="/array">
      <Cargos>
         <xsl:apply-templates />
      </Cargos>
   </xsl:template>
   <xsl:template match="/array/map | /map">
      <Cargo>
         <xsl:apply-templates />
      </Cargo>
   </xsl:template>
   <xsl:template match="*[@key='voyage']">
      <IsTc>
         <xsl:value-of select="*[@key='isTc']" />
      </IsTc>
      <VesselCode>
         <xsl:value-of select="*[@key='vessel']/*[@key='vesselCodes'][1]/*/*[@key='vesselCode']" />
      </VesselCode>
      <VesselName>
         <xsl:value-of select="*[@key='vessel']/*[@key='vesselName']" />
      </VesselName>
   </xsl:template>
   <xsl:template match="*[@key='commodity']">
      <CommodityName>
         <xsl:value-of select="*[@key='commodityName']" />
      </CommodityName>
   </xsl:template>
   <xsl:template match="*[@key='cargoPorts']">
      <xsl:apply-templates select="*/*" />
   </xsl:template>
   <xsl:template match="*[@key='portCall']">
      <xsl:variable name="reasonForCall" select="*[@key='reasonForCall']/*[@key='reasonForCall']" />
      <xsl:variable name="name" select="*[@key='port']/*[@key='portName']" />
      <xsl:if test="$reasonForCall = 'L' ">
         <LoadPort>
            <xsl:value-of select="$name" />
         </LoadPort>
         <LoadPortArrival>
            <xsl:apply-templates select="*/*" />
         </LoadPortArrival>
      </xsl:if>
      <xsl:if test="$reasonForCall = 'D' ">
         <DischargePort>
            <xsl:value-of select="$name" />
         </DischargePort>
         <DischargePortArrival>
            <xsl:apply-templates select="*/*" />
         </DischargePortArrival>
      </xsl:if>
   </xsl:template>
   <xsl:template match="*[@key='eventLogs']/*">
      <xsl:variable name="code" select="*[@key='event']/*[@key='eventCode'][text() = 'ARR']" />
      <xsl:if test="$code">
         <xsl:value-of select="*[@key='eventLogDate']" />
      </xsl:if>
   </xsl:template>
</xsl:stylesheet>

URL: http://platform-dev.dataloy.com/ws/rest/Cargo/xml/1225540

Output:

<?xml version="1.0" encoding="UTF-8"?>
<Cargo>
    <CommodityName>FERTILIZER</CommodityName>
    <LoadPort>AMSTERDAM</LoadPort>
    <LoadPortArrival>2006-06-14T12:17:00</LoadPortArrival>
    <DischargePort>LIDKOPING</DischargePort>
    <DischargePortArrival>2006-06-17T19:53:00</DischargePortArrival>
    <IsTc>false</IsTc>
    <VesselCode>FETR</VesselCode>
    <VesselName>FEHN TRADER</VesselName>
</Cargo>

Last updated