What is XSLT?
XSL stands for EXtensible Stylesheet Language. XSLT (EXtensible Stylesheet Language Transformations) is a language used to transform XML documents into other XML documents or formats (HTML, CSV..etc). It is a styling language for XML just like CSS is a styling language for HTML.
It is an industry standard for XML data mapping, based on its flexibility and reusability.
What Is an XSLT Service?
A XSLT service transforms XML source data by applying the instructions in an associated document or style sheet. The XML data can be transformed into HTML for display as a web page, into plain text, or into different XML formats, depending on the XSLT code in the style sheet. You can create multiple XSLT services, each with its own style sheet, that define different types of transformations.
The XSLT rules for a specific XML transformation are stored in the service’s style sheet. At run time, the service uses the XSLT style sheet to transform XML data passed to the service.
XSLT services support the following standards:
- Java API for XML Processing (JAXP)
- XSL Transformations (XSLT) Version 1.0
- XML Path Language (XPath) Version 1.0
An XSLT service is identified in the Package Navigator view of Designer. If you rename, move, or delete an XSLT service, Designer automatically updates any references to it and renames the style sheet file to match the service name.
You can drag and drop an XSLT service into a flow service and call it as a step in the service, invoke it from a Java service, or invoke it directly using an HTTP request. The XSLT service must reside on the same server as the flow service.
Note:
XSLT services cannot be invoked by a trigger.
By default, Integration Server uses an interpretive processor to perform the transformation
If your requirement is XML to XML transformation then XSLT is the best solution, else alternate solution by using flow services, Map.
How Does an XSLT Service Work?
An XSLT service applies the rules in its XSLT style sheet to transform XML data.
When executed, the XSLT service calls an external XSLT engine to convert the XML data. The external XSLT engine must be Java API for XML Processing (JAXP)-compatible. By default, Integration Server includes the Xerces parser and the Xalan XSLT style sheet processor from the Apache Software Foundation. The Java archives for Xerces and Xalan are located in the
Software AG_directory \common\lib\ext directory.
To use a more current version of the Xalan or other 3rd-party XSLT processor, you must place all
the related jar files in the Integration Server_directory
\instances\instance_name\packages\WmXSLT\code\jars directory and configure the transformer
factory settings to specify the appropriate XSLT transformer class.
What Is a Translet?
A translet is a compiled java class that you can use to perform XSL transformations. By default,
Integration Server uses an interpretive processor to process a style sheet. But for greater efficiency,
you can instruct Integration Server to use a compiling processor provided by Xalan.
Integration Server writes the translet to the same folder that contains the associated style sheet.
The translet will be available even after Integration Server restarts and can be used in subsequent
transformations.
Let's see how to create XSLT service in Designer..
File>New>XSLT Service
Paste the XSLT - logic
Drag and drop XSLT service into your flow service.
Map the incoming XML data to XSLT service.
Execute the flow service and see the result.
Here, I've converted xml data in to HTML format. Based on your requirement, you can convert XML into JSON, CSV ..etc.
Save this HTML into notepad and file extension is .html
Then, open .html file. It will be opened in browser automatically.
Sample XML, XSLT and HTML attached for your reference.
XML Data:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<cd>
<title>Sylvias Mother</title>
<artist>Dr.Hook</artist>
<country>UK</country>
<company>CBS</company>
<price>8.10</price>
<year>1973</year>
</cd>
<cd>
<title>When a man loves a woman</title>
<artist>Percy Sledge</artist>
<country>USA</country>
<company>Atlantic</company>
<price>8.70</price>
<year>1987</year>
</cd>
<cd>
<title>Tupelo Honey</title>
<artist>Van Morrison</artist>
<country>UK</country>
<company>Polydor</company>
<price>8.20</price>
<year>1971</year>
</cd>
<cd>
<title>Soulsville</title>
<artist>Jorn Hoel</artist>
<country>Norway</country>
<company>WEA</company>
<price>7.90</price>
<year>1996</year>
</cd>
<cd>
<title>The very best of</title>
<artist>Cat Stevens</artist>
<country>UK</country>
<company>Island</company>
<price>8.90</price>
<year>1990</year>
</cd>
</catalog>
XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>CD Collection...</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th style="text-align:left">Title</th>
<th style="text-align:left">Artist</th>
<th style="text-align:left">Origin</th>
<th style="text-align:left">Cost</th>
<th style="text-align:left">Year of Release</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
<td><xsl:value-of select="country"/></td>
<td><xsl:value-of select="price"/></td>
<td><xsl:value-of select="year"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
HTML: (Output of the flow service)
<html>
<body>
<h2>CD Collection...</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th style="text-align:left">Title</th><th style="text-align:left">Artist</th><th style="text-align:left">Origin</th><th style="text-align:left">Cost</th><th style="text-align:left">Year of Release</th>
</tr>
<tr>
<td>Sylvias Mother</td><td>Dr.Hook</td><td>UK</td><td>8.10</td><td>1973</td>
</tr>
<tr>
<td>When a man loves a woman</td><td>Percy Sledge</td><td>USA</td><td>8.70</td><td>1987</td>
</tr>
<tr>
<td>Tupelo Honey</td><td>Van Morrison</td><td>UK</td><td>8.20</td><td>1971</td>
</tr>
<tr>
<td>Soulsville</td><td>Jorn Hoel</td><td>Norway</td><td>7.90</td><td>1996</td>
</tr>
<tr>
<td>The very best of</td><td>Cat Stevens</td><td>UK</td><td>8.90</td><td>1990</td>
</tr>
</table>
</body>
</html>
Source:
-w3schools.com
-documentation.softwareag.com
Thanks for reading :-)
No comments:
Post a Comment