XSLT

XSLT content
Mirjana's picture

Pull a part of Process Data by XSLT

Request: get a specific Output from the following Process Data
 
<ProcessData>
          <OUTPUT>
                   <Data>
                             <naziv>STAVKA1</naziv>
                             <kolicina>2</kolicina>
                             <cijena>10.20</cijena>
                             <uk_cijena>20.40</uk_cijena>
                             <produkt_ID>123</produkt_ID>
                             <adresa>Heinzelova 1</adresa>
                   </Data>
                   <Data>
                             <naziv>STAVKA2</naziv>
                             <kolicina>3</kolicina>
                             <cijena>15.10</cijena>
                             <uk_cijena>45.30</uk_cijena>
                             <produkt_ID>234</produkt_ID>
                             <adresa>Heinzelova 2</adresa>
                   </Data>
                   <Data>
                             <naziv>STAVKA3</naziv>
                             <kolicina>5</kolicina>
                             <cijena>12.41</cijena>
                             <uk_cijena>62.05</uk_cijena>
                             <produkt_ID>345</produkt_ID>
                             <adresa>Heinzelova 3</adresa>
                   </Data>
          </OUTPUT>
</ProcessData>
 
Output:
 
<ENABLED>
<naziv>STAVKA1</naziv>
          <produkt_ID>123</produkt_ID>
</ENABLED>
<ENABLED>
          <naziv>STAVKA2</naziv>
          <produkt_ID>234</produkt_ID>
</ENABLED>
<ENABLED>
          <naziv>STAVKA3</naziv>
          <produkt_ID>345</produkt_ID>
</ENABLED>
 
XSLT:
 
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
          <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
          <xsl:template match="/">
                   <ROOT>
                             <xsl:for-each select="//Data">
                                      <xsl:element name="ENABLED">
                                                <xsl:element name="naziv">
                                                          <xsl:value-of select="naziv/text()"/>
                                                </xsl:element>
                                                <xsl:element name="produkt_ID">
                                                          <xsl:value-of select="produkt_ID/text()"/>
                                                </xsl:element>
                                      </xsl:element>
                             </xsl:for-each>
                   </ROOT>
          </xsl:template>
</xsl:stylesheet>
 
BPML – XSLT Service configuration (result goes to PrimaryDocument):
 
<operation name="XSLT Service">
          <participant name="XSLTService"/>
          <output message="XSLTServiceTypeInputMessage">
                   <assign to="." from="*"/>
                   <assign to="input_pd_xpath">/</assign>
                   <assign to="xml_input_from">ProcData</assign>
                   <assign to="xslt_name">TEST_ProcessData_pull</assign>
          </output>
          <input message="inmsg">
                   <assign to="." from="*"/>
          </input>
</operation>
 
Result in the Primary Document:
 
PrimaryDocument
 
Process Name: TEST_ProcessData_pull     Instance ID: 1342884
Service Name: XSLTService
Document Name: xslt_result      Document Store: Database 
Document ID: 911:7252702:12e71919a89:lin2:node1 
Document in process data:   text/xml 
 
<?xml version="1.0" encoding="UTF-8"?>
<ROOT>
          <ENABLED>
                   <naziv>STAVKA1</naziv>
                   <produkt_ID>123</produkt_ID>
          </ENABLED>
          <ENABLED>
                   <naziv>STAVKA2</naziv>
                   <produkt_ID>234</produkt_ID>
          </ENABLED>
          <ENABLED>
                   <naziv>STAVKA3</naziv>
                   <produkt_ID>345</produkt_ID>
          </ENABLED>
</ROOT>
 
 
Assign for moving it back to the ProcessData:
 
<assign to="." from="DocToDOM(PrimaryDocument)/*"></assign>
 
Result in the Process Data after the last assign:
 
<?xml version="1.0" encoding="UTF-8"?>
<ProcessData>
          <OUTPUT>
                   <Data>
                             <naziv>STAVKA1</naziv>
                             <kolicina>2</kolicina>
                             <cijena>10.20</cijena>
                             <uk_cijena>20.40</uk_cijena>
                             <produkt_ID>123</produkt_ID>
                             <adresa>Heinzelova 1</adresa>
                   </Data>
                   <Data>
                             <naziv>STAVKA2</naziv>
                             <kolicina>3</kolicina>
                             <cijena>15.10</cijena>
                             <uk_cijena>45.30</uk_cijena>
                             <produkt_ID>234</produkt_ID>
                             <adresa>Heinzelova 2</adresa>
                   </Data>
                   <Data>
                             <naziv>STAVKA3</naziv>
                             <kolicina>5</kolicina>
                             <cijena>12.41</cijena>
                             <uk_cijena>62.05</uk_cijena>
                             <produkt_ID>345</produkt_ID>
                             <adresa>Heinzelova 3</adresa>
                   </Data>
          </OUTPUT>
          <PrimaryDocument SCIObjectID="746:7278775:12e71919a89:lin2:node1"/>
          <ENABLED>
                        <naziv>STAVKA1</naziv>
                        <produkt_ID>123</produkt_ID>
            </ENABLED>
            <ENABLED>
                        <naziv>STAVKA2</naziv>
                        <produkt_ID>234</produkt_ID>
            </ENABLED>
            <ENABLED>
                        <naziv>STAVKA3</naziv>
                        <produkt_ID>345</produkt_ID>
            </ENABLED>
</ProcessData>
 
 
 
admin's picture

XSLT Examples

Input document for examples is the same as one used in XPath examples!
 
1. The simplest possible stylesheet
 
Every XSL stylesheet must start with xsl:stylesheet element. The atribute version='1.0' specifies version of XSL(T) specification. This example shows the simplest possible stylesheet.
 
<xsl:stylesheet version = '1.0'
     xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
 
 
</xsl:stylesheet>
 
2. xsl:template element
 
Each <xsl:template> element contains rules to apply when a specified node is matched.
The match attribute is used to associate the template with an XML element.
 
match="/" defines the whole document
 
XSLT stylesheet
 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
          <xsl:template match="/">
                   <html>
                             <body>
                                      <h2>Example</h2>
                                      <br/>
                             </body>
                   </html>
          </xsl:template>
</xsl:stylesheet>
 
Browser view
 
Example
 
Text view
 
<html>
          <body>
                   <h2>Example</h2><br></body>
</html>
 
3. xsl:value-of element
 
With the following XSLT we will take just contents of the firstname and the lastname elements
 
XSLT stylesheet
 
<xsl:stylesheet version = '1.0'
     xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
          <xsl:template match="/">
                   <h1>
                             <xsl:value-of select="//firstname"/>
                   </h1>
                   <h2>
                             <xsl:value-of select="//lastname"/>
                   </h2>
          </xsl:template>
</xsl:stylesheet>
 
Text view:
 
<h1>Joe</h1>
<h2>User</h2>
 
Browser view:
 
Joe
User
 
5. xsl:for-each element – getting product description and productID(s) in a table
 
The <xsl:for-each> element allows you to do looping in XSLT
 
XSLT stylesheet
 
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
          <xsl:template match="/">
                   <html>
                             <body>
                                      <h2>List of products</h2>
                                      <table border="1">
                                                <tr>
                                                          <th>Product description</th>
                                                          <th>Product name</th>
                                                </tr>
                             <xsl:for-each select="Data/instanceData/POdocument/items/item">
                                                <tr>
                                                          <td>
                                      <xsl:value-of select="string(productID/@description)"/>
                                                          </td>
                                                          <td>
                                      <xsl:value-of select="productID/text()"/>
                                                          </td>
                                                </tr>
                             </xsl:for-each>
                                      </table>
                             </body>
                   </html>
          </xsl:template>
</xsl:stylesheet>
 
Text view:
 
<html>
          <body>
                   <h2>List of products</h2>
                   <table border="1">
                             <tr>
                                      <th>Product description</th>
                                      <th>Product name</th>
                             </tr>
                             <tr>
                                      <td>Product_1</td>
                                      <td>CO11</td>
                             </tr>
                             <tr>
                                      <td>Product_2</td>
                                      <td>CO12</td>
                             </tr>
                             <tr>
                                      <td>Product_3</td>
                                      <td>CO13</td>
                             </tr>
                   </table>
          </body>
</html>
 
Browser view:
 
List of products

Product description
Product name
Product_1
CO11
Product_2
CO12
Product_3
CO13

 
 
6. xsl:for-each element – getting all the product details in a table
 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
          <xsl:template match="/">
                   <html>
                             <body>
                                      <h2>List of products</h2>
                                      <table border="1">
                                                <tr>
                                                          <th>Product description</th>
                                                          <th>Product name</th>
                                                          <th>Unit price</th>
                                                          <th>Quantity</th>
                                                </tr>
                             <xsl:for-each select="Data/instanceData/POdocument/items/item">
                                                <tr>
                                                          <td>
                                      <xsl:value-of select="string(productID/@description)"/>
                                                          </td>
                                                          <td>
                                      <xsl:value-of select="productID/text()"/>
                                                          </td>
                                                          <td>
                                      <xsl:value-of select="price/text()"/>
                                                          </td>
                                                          <td>
                                      <xsl:value-of select="quantity/text()"/>
                                                          </td>
                                                </tr>
                             </xsl:for-each>
                                      </table>
                             </body>
                   </html>
          </xsl:template>
</xsl:stylesheet>
 
 
Text view:
 
<html>
          <body>
                   <h2>List of products</h2>
                   <table border="1">
                             <tr>
                                      <th>Product description</th>
                                      <th>Product name</th>
                                      <th>Unit price</th>
                                      <th>Quantity</th>
                             </tr>
                             <tr>
                                      <td>Product_1</td>
                                      <td>CO11</td>
                                      <td>1.23</td>
                                      <td>5</td>
                             </tr>
                             <tr>
                                      <td>Product_2</td>
                                      <td>CO12</td>
                                      <td>2.34</td>
                                      <td>7</td>
                             </tr>
                             <tr>
                                      <td>Product_3</td>
                                      <td>CO13</td>
                                      <td>3.45</td>
                                      <td>9</td>
                             </tr>
                   </table>
          </body>
</html>
 
Browser view:
 
List of products

Product description
Product name
Unit price
Quantity
Product_1
CO11
1.23
5
Product_2
CO12
2.34
7
Product_3
CO13
3.45
9

 
 
7. xsl:sort element – sorting by price in desc order
 
The <xsl:sort> element is used to sort the output.
 
XSLT stylesheet
 
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
          <xsl:template match="/">
                   <html>
                             <body>
                                      <h2>List of products</h2>
                                      <table border="1">
                                                <tr>
                                                          <th>Product description</th>
                                                          <th>Product name</th>
                                                          <th>Unit price</th>
                                                          <th>Quantity</th>
                                                </tr>
                                                <xsl:for-each select="Data/instanceData/POdocument/items/item">
                                                          <xsl:sort order="descending"/>
                                                          <tr>
                                                                   <td>
                                                                             <xsl:value-of select="string(productID/@description)"/>
                                                                   </td>
                                                                   <td>
                                                                             <xsl:value-of select="productID/text()"/>
                                                                   </td>
                                                                   <td>
                                                                             <xsl:value-of select="price/text()"/>
                                                                   </td>
                                                                   <td>
                                                                             <xsl:value-of select="quantity/text()"/>
                                                                   </td>
                                                          </tr>
                                                </xsl:for-each>
                                      </table>
                             </body>
                   </html>
          </xsl:template>
</xsl:stylesheet>
 
Text view:
 
<html>
          <body>
                   <h2>List of products</h2>
                   <table border="1">
                             <tr>
                                      <th>Product description</th>
                                      <th>Product name</th>
                                      <th>Unit price</th>
                                      <th>Quantity</th>
                             </tr>
                             <tr>
                                      <td>Product_3</td>
                                      <td>CO13</td>
                                      <td>3.45</td>
                                      <td>9</td>
                             </tr>
                             <tr>
                                      <td>Product_2</td>
                                      <td>CO12</td>
                                      <td>2.34</td>
                                      <td>7</td>
                             </tr>
                             <tr>
                                      <td>Product_1</td>
                                      <td>CO11</td>
                                      <td>1.23</td>
                                      <td>5</td>
                             </tr>
                   </table>
          </body>
</html>
 
Browser view:
 
List of products

Product description
Product name
Unit price
Quantity
Product_3
CO13
3.45
9
Product_2
CO12
2.34
7
Product_1
CO11
1.23
5

 
admin's picture

XSLT Introduction

XSLT is a language for transforming XML documents into other XML documents
 
XSLT - XSL Transformations
 
XSLT is the most important part of the XSL Standards. It is the part of XSL that is used to transform an XML document into another XML document, or another type of document that is recognized by a browser, like HTML and XHTML or XML can be transformed into plain text by XSLT.
 
XSLT uses XPath to define the matching patterns for transformations
 
XSLT can also:
·        add new elements into the output file
·        remove elements
·        it can rearrange and sort elements
·        test and make decisions about which elements to display, and a lot more.
 
XSLT elements:
 

  • xsl:stylesheet
  • xsl:template
  • xsl:value-of
  • xsl:text
  • xsl:copy-of
  • xsl:copy
  • xsl:for-each
  • xsl:sort
  • xsl:if
  • xsl:choose
  • xsl:variable

  

admin's picture

XSLT

You can find some simple explanation and few examples of XSLT in this document. XSLT is very useful in Sterling Integrator. It can translate XML source into another XML, HTML or simple text.
A typical usage of XSLT translation is preparing message for mail body with information about successful processing or error notification.
XSLT can take either Primary Document or Process Data as a source/input for translation.
 

Syndicate content