Working with XSLT Service Engine: Part 2

Project summary:

In This project, I am going to do xml to xml transformation using Service Bridge. Please Check working with XSLT Service Engine Part 1 for more details about XSLT Service engine and sample project for Request-Reply service of XSLT service Engine.

In this sample project I am going to take customer information like Name , Address , Personal Data in separate node and then merge all above info in single node.

Creating Sample Project Using   Service Bridge (xml to xml transformation)

In Service Bridge we can apply multiple xml transformation on request xml and then send output back to source. We need to have same number of wsdl operation as number of transformation we want to do. In this project I am going to apply two xml transformations. So I need to create two wsdl as webservice operation to perform transformation. Let’s  say

Request1 and response 1 – first operation

Request2 and response 2 – second operation

Client will send request via request 1 and response 1 will get back to client but over all process will something like this .

(Client input xml)Request1–>apply xsl on input to generate request2–>  request 2àappy xsl on request 2 generate response 2–>  response 2 –> apply xsl on response2 to generate response1 –> response 1(client output xml).

Create XSLT Module:

Please refer to part 1 for details to create XSLT module.

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:complexType name="CustomerRecord">
          <xsd:sequence>
              <xsd:element name="Customer" type="xsd:string" maxOccurs="unbounded"/>
          </xsd:sequence>
      </xsd:complexType>
      <xsd:complexType name="CustomerData">
          <xsd:sequence>
              <xsd:element name="Customer" form="qualified" maxOccurs="unbounded">
                 <xsd:complexType>
                     <xsd:sequence>
                         <xsd:element name="CustomerInfo" type="xsd:string"/>
                     </xsd:sequence>
                 </xsd:complexType>
               </xsd:element>
          </xsd:sequence>
       </xsd:complexType>
       <xsd:element name="CustomerRequest" type="tns:Customers"/>
       <xsd:element name="CustomerResponse" type="tns:CustomersResponse"/>
       <xsd:element name="CustomerOutput" type="tns:CustomerRecord"/>
       <xsd:element name="CustomerReply" type="tns:CustomerData"/>
</xsd:schema>

img10

Customer Request is input type and Customer output is Response type.

Create WSDL for exposing xsl as webservice

Since we are performing two xsl transformation we  need to wsdl operation . Lets create wsdl separately for those.

a)     CustomerServiceOneWs.wsdl

b)     CustomerServiceTwoWs.wsdl

CustomerServiceOneWs.wsdl

Operation of this wsdl will have actual customer request and response messages.Lets create this wsdl.

Details is covered in part one. See screenshot for message type of this wsdl.

img11

img12

CustomerServiceTwoWs.wsdl

img13

 Create XSLT Service

Right click on project and select XSLT Service then chose Service Bridge as show below.

img14

img15

Check operation under implements and calls .you can change the operation based on your requirement by clicking choose .it will display all wsdl operation you are performing in your project. Choose accordingly .Here operation under implements is one which we are exposing to client and then it passing it on to other webservice for multiple transformation as in our case we are using CustomerServiceTwoWs.wsdl .

Points to Notice :

You will notice two xsl file gets created.

InXslFile1 : transform input xml  (request1) to request of second operation.(request2)

<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="/">
        <xsl:apply-templates select="/ns0:CustomerRequest"/>
    </xsl:template>
  -->
    <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>

OutXslFile1 : transform  second response(response2) to response of first operation (response1).

<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:CustomerOutput>            
            <xsl:for-each select="/ns0:CustomerOutput/ns0:Customer">
                <ns0:Customer>  Customer Info (Received from module 2:)          
                    <xsl:value-of select="ns0:CustomerInfo"/>                    
                </ns0:Customer>                
            </xsl:for-each>            
        </ns0:CustomerOutput>        
    </xsl:template>
</xsl:stylesheet>

Now to transform second request(request 2) to second response (response2) we need to have separate xslt module which will implement second web service . We will discuss in detail later.

Transformmap.xml

As soon you created XSLT service in above part , Transformmap.xml will get populated accordingly , take a look at the content of this xml .

<transformmap
    ----------
    ----------
    <import namespace="http://j2ee.netbeans.org/wsdl/XsltModule2/src/CustomerServiceTwoWs" location="CustomerServiceTwoWs.wsdl"/>
    <import namespace="http://j2ee.netbeans.org/wsdl/XsltModule2/src/CustomerServiceOneWs" location="CustomerServiceOneWs.wsdl"/>
    <service name="Service1" portType="ns1:CustomerServiceOneWsPortType">
        <operation opName="CustomerServiceOneWsOperation" inputVariable="inOpVar1" outputVariable="outOpVar1">
            <transform file="InXslFile1.xsl" source="$inOpVar1.Request1" result="$inInvokeVar1.Request2" name="InTransform1"/>
            <invoke inputVariable="inInvokeVar1" outputVariable="outInvokeVar1" name="Invoke1" portType="ns2:CustomerServiceTwoWsPortType" opName="CustomerServiceTwoWsOperation"/>
            <transform file="OutXslFile1.xsl" source="$outInvokeVar1.Response2" result="$outOpVar1.Response1" name="OutTransform1"/>
        </operation>
    </service>
</transformmap>

a)Service name = service1 ( name we gave while creating XSLT service )

b)There are two transform node for two transformation which will apply on input xml.

c)Note that there is only one operation being implemented  here as we going to implement other operation ( webservice) in another xslt module .

d) Look at source and result values in both transform node, I have also shown this as flow in above project summary .

source=”$inOpVar1.Request1″ result=”$inInvokeVar1.Request2″ (Transforming Request 1  to Request 2)

source=”$outInvokeVar1.Response2″ result=”$outOpVar1.Response1″(Transforming response2 to response 1.)

As mentioned earlier transformation from request2 to response2 will  be implemented in another XSLT module which will implement second web service .

Clean and build this XSLT project .

Create XSLT module to implement second web service.

Copy paste XSD and WSDL created for 2nd web service from first XSLT module.

Create XSLT service as created above but here we need to choose request-reply Service. For details for Request-Reply service please refer to part 1 of this tutorial.

XSL file : Written to transform request2 to response2 .

<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:CustomerOutput>            
            <xsl:for-each select="/ns0:CustomerResponse/ns0:CustomerResponse">
                <ns0:Customer >Customer Info (Passing from module 2): 
                    <ns0:CustomerInfo>                
                        <xsl:value-of select="ns0:Name"/>
                        Customer Address :
                        <xsl:value-of select="ns0:Address" />
                        Customer Data :
                        <xsl:value-of select="ns0:PersonalData" />
                    </ns0:CustomerInfo> 
                </ns0:Customer>                
            </xsl:for-each>            
        </ns0:CustomerOutput>        
    </xsl:template>
</xsl:stylesheet>

Clean and build this project .

Create Composite Application :

Drag and Drop both XSLT module to CASA .

Build and deploy this project.

Results

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:CustomerOutput xmlns:msgns="http://j2ee.netbeans.org/wsdl/XsltModule2/src/CustomerServiceOneWs" xmlns:ns0="http://xml.netbeans.org/schema/CustomerDetails">
      <ns0:Customer>  Customer Info (Received from module 2:)          
                    jay Gupta
                    Customer Address :
                    Kolkata Bangalore
                    Customer Data :
                    M 27/12/1987</ns0:Customer>
      <ns0:Customer>  Customer Info (Received from module 2:)          
                    Param Singh
                    Customer Address :
                    Satna Bangalore
                    Customer Data :
                    M 19/10/1986</ns0:Customer>
    </ns0:CustomerOutput>

This content is restricted to site members. If you are an existing user, please log in. New users may register below.

Existing Users Log In
   
New User Registration
*Required field