xslt transformation

XML to JSON Transformer

Version: 17.01

Supported Since: 17.01

What is an XSLT Transformer?

XSLT Transformer is a processing element which transforms an XML message payload to the expected format via a XSLT transformation. This is a standard XSLT transformation which transforms the XML payload using the configured XSLT stylesheet.

Sample Use Case

In the following use case, the requirement is to mediate the requests to implement a proxy which will mediate requests to a web service by doing a transformation to the received XML message payload. Message payload should be transformed to an XML message with pre-defined format to make it compatible with actual back end service.

Prerequisite

In order to implement above use case you must first select following dependencies when you are creating an empty Ultra project

  • HTTP NIO Connector from the connector list.

  • Message Transformation dependency from the processor list.

If you have already created a project, you can add above dependencies via Component Registry. From Tools menu, select Ultra Studio → Component Registry and from the Connectors list and Processors list, select above dependencies

Implementation

For the implementation we should first prepare the XSLT to transform the received XML payload to the expected format. For that let’s compare the input message format and expected output message format.

Input XML message format would be like this,

<?xml version="1.0" encoding="UTF-8"?>
<adrt:Currency xmlns:adrt="http://adrt.com/studio/testing">
    <adrt:CurrencyInfo>
        <adrt:EFFECTIVE_DATE>10/24/16</adrt:EFFECTIVE_DATE>
        <adrt:REFERENCE_NO>CR_9898123</adrt:REFERENCE_NO>
        <adrt:CRNCY_CODE>AUD</adrt:CRNCY_CODE>
        <adrt:EXCH_RATE>1.06</adrt:EXCH_RATE>
    </adrt:CurrencyInfo>
    <adrt:CurrencyInfo>
        <adrt:EFFECTIVE_DATE>10/24/16</adrt:EFFECTIVE_DATE>
        <adrt:REFERENCE_NO>CR_9898123</adrt:REFERENCE_NO>
        <adrt:CRNCY_CODE>CNY</adrt:CRNCY_CODE>
        <adrt:EXCH_RATE>6.07</adrt:EXCH_RATE>
    </adrt:CurrencyInfo>
    <adrt:CurrencyInfo>
        <adrt:EFFECTIVE_DATE>10/24/16</adrt:EFFECTIVE_DATE>
        <adrt:REFERENCE_NO>CR_9898123</adrt:REFERENCE_NO>
        <adrt:CRNCY_CODE>EUR</adrt:CRNCY_CODE>
        <adrt:EXCH_RATE>0.73</adrt:EXCH_RATE>
    </adrt:CurrencyInfo>
    <adrt:CurrencyInfo>
        <adrt:EFFECTIVE_DATE>10/24/16</adrt:EFFECTIVE_DATE>
        <adrt:REFERENCE_NO>CR_9898123</adrt:REFERENCE_NO>
        <adrt:CRNCY_CODE>GBP</adrt:CRNCY_CODE>
        <adrt:EXCH_RATE>0.59</adrt:EXCH_RATE>
    </adrt:CurrencyInfo>
    <adrt:CurrencyInfo>
        <adrt:EFFECTIVE_DATE>10/24/16</adrt:EFFECTIVE_DATE>
        <adrt:REFERENCE_NO>CR_9898123</adrt:REFERENCE_NO>
        <adrt:CRNCY_CODE>HKD</adrt:CRNCY_CODE>
        <adrt:EXCH_RATE>7.75</adrt:EXCH_RATE>
    </adrt:CurrencyInfo>
    <adrt:CurrencyInfo>
        <adrt:EFFECTIVE_DATE>10/24/16</adrt:EFFECTIVE_DATE>
        <adrt:REFERENCE_NO>CR_9898123</adrt:REFERENCE_NO>
        <adrt:CRNCY_CODE>JPY</adrt:CRNCY_CODE>
        <adrt:EXCH_RATE>103</adrt:EXCH_RATE>
    </adrt:CurrencyInfo>
    <adrt:CurrencyInfo>
        <adrt:EFFECTIVE_DATE>10/24/16</adrt:EFFECTIVE_DATE>
        <adrt:REFERENCE_NO>CR_9898123</adrt:REFERENCE_NO>
        <adrt:CRNCY_CODE>SGD</adrt:CRNCY_CODE>
        <adrt:EXCH_RATE>1.25</adrt:EXCH_RATE>
    </adrt:CurrencyInfo>
    <adrt:CurrencyInfo>
        <adrt:EFFECTIVE_DATE>10/24/16</adrt:EFFECTIVE_DATE>
        <adrt:REFERENCE_NO>CR_9898123</adrt:REFERENCE_NO>
        <adrt:CRNCY_CODE>USD</adrt:CRNCY_CODE>
        <adrt:EXCH_RATE>1</adrt:EXCH_RATE>
    </adrt:CurrencyInfo>
</adrt:Currency>

Expected output message format would be like this,

<?xml version="1.0" encoding="UTF-8"?>
<ExchangeRates xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://adrt.com/studio/testing">
   <OriginatedFrom>Global Currency Ltd</OriginatedFrom>
   <CreationTimestamp>2016-12-23T15:07:25.069Z</CreationTimestamp>
   <ReferenceNo>CR_9898123</ReferenceNo>
   <Rates>
      <Rate>
         <Currency>AUD</Currency>
         <Rate>1.06</Rate>
         <RelativeTo>USD</RelativeTo>
      </Rate>
      <Rate>
         <Currency>CNY</Currency>
         <Rate>6.07</Rate>
         <RelativeTo>USD</RelativeTo>
      </Rate>
      <Rate>
         <Currency>EUR</Currency>
         <Rate>0.73</Rate>
         <RelativeTo>USD</RelativeTo>
      </Rate>
      <Rate>
         <Currency>GBP</Currency>
         <Rate>0.59</Rate>
         <RelativeTo>USD</RelativeTo>
      </Rate>
      <Rate>
         <Currency>JPY</Currency>
         <Rate>103</Rate>
         <RelativeTo>USD</RelativeTo>
      </Rate>
      <Rate>
         <Currency>SGD</Currency>
         <Rate>1.25</Rate>
         <RelativeTo>USD</RelativeTo>
      </Rate>
      <Rate>
         <Currency>USD</Currency>
         <Rate>1</Rate>
         <RelativeTo>USD</RelativeTo>
      </Rate>
   </Rates>
</ExchangeRates>

For above transformation, relevant XSLT stylesheet would be like below,

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"   xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://adrt.com/studio/testing">

    <xsl:output indent="yes" omit-xml-declaration="no"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="Currency" xpath-default-namespace="http://adrt.com/studio/testing">
        <ExchangeRates>
            <OriginatedFrom>Global Currency Ltd</OriginatedFrom>
            <ReferenceNo><xsl:value-of select="CurrencyInfo[1]/REFERENCE_NO"/></ReferenceNo>
            <CreationTimestamp><xsl:value-of select="adjust-dateTime-to-timezone(current-dateTime(), xs:dayTimeDuration('PT0H'))"/></CreationTimestamp>
            <Rates>
                <xsl:apply-templates/>
            </Rates>
        </ExchangeRates>
    </xsl:template>

    <xsl:template match="CurrencyInfo" xpath-default-namespace="http://adrt.com/studio/testing">
        <Rate>
            <Currency><xsl:value-of select="CRNCY_CODE"/></Currency>
            <Rate><xsl:value-of select="EXCH_RATE"/></Rate>
            <RelativeTo>USD</RelativeTo>
        </Rate>
    </xsl:template>

</xsl:stylesheet>

To implement this use case, first let’s create our integration flow named “integrate-xslt-transformation". Then add an NIO HTTP Ingress Connector and configure the request receiving service path and port.

After that let’s add the XSLT Transformer processing element to do the above transformation. XSLT Transformer should be configured as below.

xslt transformer processor 1

Then NIO HTTP Egress Connector should be added to send out the received requests to the actual backend service. This element should be configured with the required parameters related to the actual back end service information - host, port and service path, etc.

With all above elements, complete integration flow would be like this,

xslt transformer processor 2

As the final step, add the Saxon-HE-9.7.x.jar to lib folder of your project to support the XSLT functionalities which we have used in our XSLT stylesheet.

Now run your integration flow and send a request message to the configured request URL to test your integration flow. Once you send a request, check with the flow tracing, you should be able to see that the received message is transformed to the expected format before sending it to the actual back end service.

Out Ports

Next

Message will be emitted from this port after setting the extract value from the XPath as the configured scope variable

On Exception

The message context will be sent to this outport if any exception occurred while extracting the provided XPath

Parameters

XSLT Path *

Basic

Path to the XSLT stylesheet file which should be applied for the transformation.

XPath for the transformation *

Basic

Xpath to select the relevant node for the XSLT transformation. If this is not configured, complete message payload will be applied to the XSLT transformation.

Namespaces

Basic

An optional map (defined as a resource) containing namespace prefixes and namespaces used within the above XPath expression. The map should have namespace prefixes as keys and namespaces as values.

In this topic
In this topic
Contact Us