Showing posts with label XSLT. Show all posts
Showing posts with label XSLT. Show all posts

Wednesday, December 27, 2017

BPEL - Dynamic XSLT file selection and multiple inputs to XSLT

Hey guys,


In this post, I am explaining the steps to Dynamically select the XSLT files in BPEL process and pass on the multiple parameters to XSLT file as input.

This will need below implementation at High level :
1. DVM Lookup for dynamic XSLT file selection
2. Use of  ora:processXSLT() function to process XSLT and assign result to variable.


Implementations Steps :

1. Create SOA project, with BPEL process
2. Add the services and reference as per requirement
3. In the BPEL process, use assign to lookup DVM for XSLT file name.
4. Use another assign to execute the ora:processXSLT() function and assign result to variable.

Things to keep in mind while implementing ora:processXSLT() :

XSLT is processed in BPEL using the method: ora:processXSLT categorized under BPEL XPath Extension Functions.
The input parameters (signature) for the method is as defined by Oracle:

Signature:

processXSLT('template','input','properties/parameter'?)

Arguments definition:

template - The XSLT template or XSLT file Name with location (If using files from MDS)

input - The input data to be transformed

properties - This is optional. Additional inputs or parameters as defined in XSLT files.


e.g. ora:processXSLT('Transform_data.xsl', $inputVariable.body, bpws:getVariableData('metaDataVar'))

However, in our case, we are looking to dynamically select the XSLT file from DVM lookup, so the example will be like this

ora:processXSLT($xsltLocVar, $inputVariable.body, bpws:getVariableData('metaDataVar'))

Here Variable xsltLocVar already has XSLT file name fetched from DVM lookup.


The properties/parameter translated into parameter constructs of the XSLT and can be referenced within the XSLT scope. The property needs to be defined in a specific structure as mentioned below -


The definition of the schema contains for the following details:

<?xml version="1.0" encoding="windows-1252"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:p="http://schemas.oracle.com/service/bpel/common" xmlns="http://schemas.oracle.com/service/bpel/common" targetNamespace="http://schemas.oracle.com/service/bpel/common" elementFormDefault="qualified">
<xsd:element name="parameters">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="item" minOccurs="1" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="name" type="xsd:string"/>
<xsd:element name="value" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>




Consider this schema structure for any variable.
A variable will always have a name and a value. eg. a variable x = 100. In case the name of the variable is "x" and the value is "100".

So the parameter variable needs to be set for all the variables which needs to be passed to the XSLT.

Define the XSD in your BPEL project. Create a variable of type global in the BPEL project with name "metaDataVar" (the name can be anything)

Lets assume, in one variable "user" we have the username and in another variable "password" we have the password for an account stored. The final payload needs to be constructed which should have both the values.

In your BPEL, create a copy operation and provide an expression to the "name" element of the variable "params". May be use the expression builder to assign a value of "userName" to the element "name".

Similarly assign "passValue" to the element "name" of the "params" variable and the xpath expression to the value to instantiate the value of the params element.

Now, come down to the XSLT where these elements needs to be accessed. Use XSLT construct to define the param as depicted below:

<xsl:param name="userName"/>
<xsl:param name="passValue"/>

Now the values of the variables can be accessed using a simple expression "$userName" and "$passValue"

Sunday, February 21, 2016

SOA Interview Questions : Service Oriented Architecture Interview Questions Part 5



What is a Complex XML Element?

A complex element is an XML element that contains other elements and/or attributes.
There are four kinds of complex elements:
empty elements
elements that contain only other elements
elements that contain only text
elements that contain both other elements and text


What is a Simple XML Element?

A simple element is an XML element that can contain only text.
A simple element cannot have attributes
A simple element cannot contain other elements
A simple element cannot be empty
However, the text can be of many different types, and may have various restrictions applied to it.


What is XPATH ?

XPath, the XML Path Language, is a query language for selecting nodes from an XML document. In addition, XPath may be used to compute values (e.g., strings, numbers, or Boolean values) from the content of an XML document.
The XPath language is based on a tree representation of the XML document, and provides the ability to navigate around the tree, selecting nodes by a variety of criteria.

XPATH  Syntax & Operators ?
Expression
Description
nodename
Selects all nodes with the name "nodename"
/
Selects from the root node
//
Selects nodes in the document from the current node that match the selection no matter where they are
.
Selects the current node
..
Selects the parent of the current node
@
Selects attributes
*
Matches any element node
@*
Matches any attribute node
node()
Matches any node of any kind



Example of some XPath Expressions ?

Example XML document
<?xml version="1.0" encoding="UTF-8"?>
<books>
                <book>
                                <title lang="en">Harry Potter</title>
                                <price>29.99</price>
                </book>
                <book>
                                <title lang="en">Learning XML</title>
                                <price>39.95</price>
                </book>
</books>


Example XPATH expressions and Result

Path Expression
Result
/books/book[1]
Selects the first book element that is the child of the books element.
/books/book[last()]
Selects the last book element that is the child of the books element
//title[@lang]
Selects all the title elements that have an attribute named lang
//title[@lang='en']
Selects all the title elements that have a "lang" attribute with a value of "en"
/books/book[price>35.00]
Selects all the book elements of the books element that have a price element with a value greater than 35.00



What is XSLT ?

XSLT (Extensible Stylesheet Language Transformations) is a language for transforming XML documents into other XML documents, or other formats such as HTML for web pages, plain text or into XSL Formatting Objects.
The original document is not changed; rather, a new document is created based on the content of an existing one. Typically, input documents are XML files.


Example XSLT code ?

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="/persons">
    <root>
      <xsl:apply-templates select="person"/>
    </root>
  </xsl:template>
</xsl:stylesheet>



XSLT vs XPATH ?

XSLT uses XPath to identify subsets of the source document tree and perform calculations.
XPath also provides a range of functions, which XSLT itself further augments.



How to refer another XSL from main XSL file ?

The <xsl:import> element is a top-level element that is used to import the contents of one style sheet into another.

Note: This element must appear as the first child node of <xsl:stylesheet> or <xsl:transform>.
Syntax: <xsl:import href="URI"/>


Why we use Call-template inside XSL ?

Call-template works similar to the apply-template element in XSLT. Both attach a template to specific XML data. This provides formatting instructions for the XML. The main difference between the two processes is the call function only works with a named template. You must establish a 'name' attribute for the template in order to call it up to format a document.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
< xsl:call-template name="myTemplate">
< !-- Content: xsl -->
< /xsl:call-template>

< stylesheet>




Refer Previous post on Interview questions at

1. SOA Interview Questions : Service Oriented Architecture Interview Questions Part 1

http://osb-dheeraj.blogspot.com/2016/02/soa-interview-questions-service.html

2. SOA Interview Questions : Service Oriented Architecture Interview Questions Part 2


http://osb-dheeraj.blogspot.com/2016/02/soa-interview-questions-service_16.html

3. SOA Interview Questions : Service Oriented Architecture Interview Questions Part 3

http://osb-dheeraj.blogspot.com/2016/02/soa-interview-questions-service_21.html

4. SOA Interview Questions : Service Oriented Architecture Interview Questions Part 4

http://osb-dheeraj.blogspot.com/2016/02/soa-interview-questions-service_77.html


Saturday, February 06, 2016

XSLT - Transform Date format (YYYYMMDD to DD-MMM-YYYY)

Date Format Transformation Using XSLT



Sometimes in we may require to Transform date from one format to another format. Because, Source is sending date in a format which target does not accepts.

So here is an simple XSLT code which transforms Date from YYYYMMDD to DD-MMM-YYYY.
Actually this can be modified to suit your needs of any other format as well.

This XSLT can be used in SOA 11g, OSB 11g, SOA 12c, OSB 12c and could be used in any other application which supports XSL transform.

Code Snippet :


<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0">
  <xsl:template match="/">
  <Root>
    <Values>            
            
            <Value AttributeID="Description">              
                <xsl:value-of select="/Header/Description"/>              
            </Value>
           
           <xsl:choose>
            <xsl:when test="string-length(/Header/StartDateActive) = 8" >
                 <Value AttributeID="Introduction Date">
                      <xsl:call-template name="date">
                           <xsl:with-param name="yyyymmdd" select="/Header/StartDateActive"/>
                      </xsl:call-template>
                  </Value>
              </xsl:when>
          <xsl:otherwise>
                  <Value AttributeID="Introduction Date"></Value>
              </xsl:otherwise>
          </xsl:choose>    
            

            
     </Values>
    </Root>
  </xsl:template>
  
  <xsl:template name="date">
    <xsl:param name="yyyymmdd"/>
    <xsl:variable name="yyyy" select="substring($yyyymmdd, 1, 4)"/>       
    <xsl:variable name="mm" select="substring($yyyymmdd, 5, 2)"/>
    <xsl:variable name="dd" select="substring($yyyymmdd, 7, 2)"/>
   

  <xsl:value-of select=" concat($dd,'-') "/>
    <xsl:choose>
      <xsl:when test="$mm = '01'">Jan</xsl:when>
      <xsl:when test="$mm = '02'">Feb</xsl:when>
      <xsl:when test="$mm = '03'">Mar</xsl:when>
      <xsl:when test="$mm = '04'">Apr</xsl:when>
      <xsl:when test="$mm = '05'">May</xsl:when>
      <xsl:when test="$mm = '06'">Jun</xsl:when>
      <xsl:when test="$mm = '07'">Jul</xsl:when>
      <xsl:when test="$mm = '08'">Aug</xsl:when>
      <xsl:when test="$mm = '09'">Sep</xsl:when>
      <xsl:when test="$mm = '10'">Oct</xsl:when>
      <xsl:when test="$mm = '11'">Nov</xsl:when>
      <xsl:when test="$mm = '12'">Dec</xsl:when>
    </xsl:choose>
    <xsl:value-of select=" concat ('-', $yyyy) "/>
</xsl:template>

</xsl:stylesheet>



Testing the XSLT :

Input :

<Header>   <Description>My Description</Description>   <StartDateActive>20160206</StartDateActive></Header>

Output :


<Root>

  <Values>
<Value AttributeID="Description">My Description</Value>
<Value AttributeID="Introduction Date">06-FEB-2016</Value>  
  </Values>
</Root>