Home » OpenESB » Working With XSLT Service Engine: Part 1

Working With XSLT Service Engine: Part 1

About the XSLT Service Engine

The XSLT Service Engine is a Java-based transformation engine that is used to convert XML documents from one data format to another. The XSLT Service Engine makes it easier for users to configure and expose XSL style sheets as web services. Using the XSLT Service Engine requires no special knowledge of XSL, but rather allows any XSL style sheet to be deployed as a JBI service unit.

The XSLT Service Engine is not solely responsible for performing transformations. XSL style sheets implement a web service operation (as normally defined in a WSDL). When deployed as JBI service units, these service units correspond to a service endpoint. Each endpoint is activated when the XSLT service unit is deployed. In a sense, the XSLT Service Engine is a container of XSL style sheets, each of which represents a service endpoint in the JBI environment.

The following steps highlight the life cycle of a typical message using the XSLT Service Engine:

The XSLT service unit is configured with service endpoint information.

  1. The service unit is deplopyed, along with the XSL style sheet, to the JBI environment.
  2. The XSLT Service Engine compiles the style sheet.
  3. A message arrives and the XSLT Service Engine searches for the service endpoint responsible for handling the message.
  4. The message is transformed using the service endpoint’s XSL style sheet.
  5. A response is sent back via the Normalized Message Router (NMR).

XSLT Service Engine Features

The XSLT Service Engine supports the following use cases:

Request-Reply Service

Request-Reply is a standard request-reply scenario. An XML message request is transformed and the result is sent back to the original consumer of the XSLT endpoint. The sequence of events includes:

  1. XML message in
  2. XSL transformation of message
  3. Transformed result out

Service Bridge

it applies two separate XSL transformations. This scenario is useful when there are two existing web services which must be integrated even though they have incompatible input and output.

The first existing service acts as a consumer to the XSLT endpoint, sending a request. This message is transformed to match the input of the second service.

The second service is then invoked in an asynchronous manner. When the response from the second service arrives via the NMR, it is transformed to match the expected reply to the first (consuming) service. The sequence of events includes:

  1. XML message in
  2. XSL transformation of message using first XSL style sheet
  3. Invoke service, sending transformed message as input
  4. Receive response from invoked service endpoint
  5. XSL transformation of response using second XSL style sheet
  6. Reply to original sender with transformed third party response

Creating Sample Project Using Request Reply Service (xml to xml transformation)

In Part 1 I am going to create sample project using Request Reply Service. In the Sample project I am going to do xml to xml transformation using XSLT Service Engine.

Service Bridge sample project will be available in part 2 of this tutorial.

Project Summary:

In this sample project I am going to take different input from customer like  Name ( first name , last name) , Address ( Home address, office Address) , personal data ( gender , date of birth)  and then perform xsl operation to merge information under same node .For example

Input XML:

        <cus:CustomerRequest>
            <!--1 or more repetitions:-->
            <cus:Customer>
                <cus:CustomerName>
                    <cus:firstName>jay</cus:firstName>
                    <cus:lastName>Gupta</cus:lastName>
                </cus:CustomerName>
                <cus:CustomerAddress>
                    <cus:HomeAddress>Kolkata</cus:HomeAddress>
                    <cus:OfficeAddress>Bangalore</cus:OfficeAddress>
                </cus:CustomerAddress>
                <cus:CustomerPersonalData>
                    <cus:gender>M</cus:gender>
                    <cus:DateOfBirth>27/12/1987</cus:DateOfBirth>
                </cus:CustomerPersonalData>
            </cus:Customer>
        </cus:CustomerRequest>

Output XML  will be something like below ,

<ns0:CustomerResponse xmlns:msgns="http://j2ee.netbeans.org/wsdl/XsltModule1/src/CustomerWS" xmlns:ns0="http://xml.netbeans.org/schema/CustomerDetails">
      <ns0:CustomerResponse>
        <ns0:Name>jay Gupta</ns0:Name>
        <ns0:Address>Kolkata Bangalore</ns0:Address>
        <ns0:PersonalData>M 27/12/1987</ns0:PersonalData>
      </ns0:CustomerResponse>
    </ns0:CustomerResponse>

Note that: Input xml can have multiple Customer information. Let’s begin with creating projects

Creating XSLT Module.

img1Click Next and then Finish .

You will see one Transformmap.xml will get created itself within the project. Right now its empty as soon you create xsl service for this project .transform xml will get populate itself.

Create XSD for request and reply operation:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://xml.netbeans.org/schema/CustomerDetails"
    xmlns:tns="http://xml.netbeans.org/schema/CustomerDetails"
    elementFormDefault="qualified">
    <xsd:complexType name="CustomerName">
        <xsd:sequence>
            <xsd:element name="firstName" type="xsd:string"/>
            <xsd:element name="lastName" type="xsd:string"/>
        </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="CustomerAddress">
        <xsd:sequence>
            <xsd:element name="HomeAddress"/>
            <xsd:element name="OfficeAddress"/>
        </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="PersonalData">
        <xsd:sequence>
            <xsd:element name="gender" type="xsd:string"/>
            <xsd:element name="DateOfBirth" type="xsd:string"/>
        </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="Customer">
        <xsd:sequence>
            <xsd:element name="CustomerName" type="tns:CustomerName"/>
            <xsd:element name="CustomerAddress" type="tns:CustomerAddress"/>
            <xsd:element name="CustomerPersonalData" type="tns:PersonalData"/>
        </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="Customers">
        <xsd:sequence>
            <xsd:element name="Customer" type="tns:Customer" maxOccurs="unbounded"/>
        </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="CustomersResponse">
        <xsd:sequence>
            <xsd:element name="CustomerResponse" type="tns:CustomerResponse" maxOccurs="unbounded"/>
        </xsd:sequence>
    </xsd:complexType>
    <xsd:complexType name="CustomerResponse">
        <xsd:sequence>
            <xsd:element name="Name" type="xsd:string"/>
            <xsd:element name="Address" type="xsd:string"/>
            <xsd:element name="PersonalData" type="xsd:string"/>
        </xsd:sequence>
    </xsd:complexType>
    <xsd:element name="CustomerRequest" type="tns:Customers"/>
    <xsd:element name="CustomerResponse" type="tns:CustomersResponse"/>
</xsd:schema>

img2Customer Request and Customer Reponse are request and reponse type of the input and output.

Create WSDL to Expose xsl as webservice

Create Wsdl  for request and response with request as CustomerRequest Type and response as Customer ResponseType from xsd created above .

img3Click Next

img4

Same way for output select Customer Response and click next and Finish.

Create xsl file for transformation from input xml to output xml.

Right click on project and select XSLT Service if you do not see the option then click on others  then click on SOA , you will find XSLT Service in the right panel .

Select Request Reply Type give Service Name .

img5Here since in my project only wsdl operation is there so it has automatically taken that  if you have multiple then you can change the operation by clicking on choose and select required operation .

img6

img7Use XSLT syntax for transformation of input xml to output xml. Below content is used for this project. Do add xsd target name space in the xsl file as highlighted below .

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
                xmlns:ns0="http://xml.netbeans.org/schema/CustomerDetails"  version="1.0">
    <xsl:template match="/">        
        <ns0:CustomerResponse>
            <xsl:for-each select="/ns0:CustomerRequest/ns0:Customer">
                <ns0:CustomerResponse>
                    <ns0:Name>  
                        <xsl:value-of select=" concat(ns0:CustomerName/ns0:firstName, ' ' )"/>
                        <xsl:value-of select="ns0:CustomerName/ns0:lastName" />
                    </ns0:Name>
                    <ns0:Address>
                        <xsl:value-of select=" concat(ns0:CustomerAddress/ns0:HomeAddress,' ')"/>
                        <xsl:value-of select="ns0:CustomerAddress/ns0:OfficeAddress" />
                    </ns0:Address>
                    <ns0:PersonalData>
                        <xsl:value-of select=" concat(ns0:CustomerPersonalData/ns0:gender,' ')"/>
                        <xsl:value-of select="ns0:CustomerPersonalData/ns0:DateOfBirth" />
                    </ns0:PersonalData>                          
                </ns0:CustomerResponse>
            </xsl:for-each>
        </ns0:CustomerResponse>      
    </xsl:template>
</xsl:stylesheet>

Transformmap.XML

Now check the content of Transformmap.xml . It will be something like below .

<transformmap
   ----------------
   ----------------
    <import namespace="http://j2ee.netbeans.org/wsdl/XsltModule1/src/CustomerWS" location="CustomerWS.wsdl"/>
    <service name="Service1" portType="ns1:CustomerWSPortType">
        <operation opName="CustomerWSOperation" inputVariable="inOpVar1" outputVariable="outOpVar1">
            <transform file="InXslFile1.xsl" source="$inOpVar1.request" result="$outOpVar1.response" name="InTransform1"/>
        </operation>
    </service>
</transformmap>

Verify source and result it will same as message name in WSDL for request and reply.

img8Clean and Build XSLT Module.

 Create Composite Application

Create Composite Application and add XSLT module to CASA.

img9Build and Deploy composite Application:

 Result

Input XML:

        <cus:CustomerRequest>
            <!--1 or more repetitions:-->
            <cus:Customer>
                <cus:CustomerName>
                    <cus:firstName>jay</cus:firstName>
                    <cus:lastName>Gupta</cus:lastName>
                </cus:CustomerName>
                <cus:CustomerAddress>
                    <cus:HomeAddress>Kolkata</cus:HomeAddress>
                    <cus:OfficeAddress>Bangalore</cus:OfficeAddress>
                </cus:CustomerAddress>
                <cus:CustomerPersonalData>
                    <cus:gender>M</cus:gender>
                    <cus:DateOfBirth>27/12/1987</cus:DateOfBirth>
                </cus:CustomerPersonalData>
            </cus:Customer>
            <cus:Customer>
                <cus:CustomerName>
                    <cus:firstName>Param</cus:firstName>
                    <cus:lastName>Singh</cus:lastName>
                </cus:CustomerName>
                <cus:CustomerAddress>
                    <cus:HomeAddress>Satna</cus:HomeAddress>
                    <cus:OfficeAddress>Bangalore</cus:OfficeAddress>
                </cus:CustomerAddress>
                <cus:CustomerPersonalData>
                    <cus:gender>M</cus:gender>
                    <cus:DateOfBirth>19/10/1986</cus:DateOfBirth>
                </cus:CustomerPersonalData>
            </cus:Customer>
        </cus:CustomerRequest>

Output XML:

    <ns0:CustomerResponse xmlns:msgns="http://j2ee.netbeans.org/wsdl/XsltModule1/src/CustomerWS" xmlns:ns0="http://xml.netbeans.org/schema/CustomerDetails">
      <ns0:CustomerResponse>
        <ns0:Name>jay Gupta</ns0:Name>
        <ns0:Address>Kolkata Bangalore</ns0:Address>
        <ns0:PersonalData>M 27/12/1987</ns0:PersonalData>
      </ns0:CustomerResponse>
      <ns0:CustomerResponse>
        <ns0:Name>Param Singh</ns0:Name>
        <ns0:Address>Satna Bangalore</ns0:Address>
        <ns0:PersonalData>M 19/10/1986</ns0:PersonalData>
      </ns0:CustomerResponse>
    </ns0:CustomerResponse>

References:

http://docs.oracle.com/cd/E19509-01/821-0235/ghzfi/index.html

http://wiki.open-esb.java.net/Wiki.jsp?page=UnderstandingXSLTDesigner

Comments

  1. James says:

    Short, sweet, and extremely useful … thanks!
    Regards
    James
    http://www.tilogeo.com

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*