Thursday, March 11, 2010

Sample XSLT Processor

Extensible Style Language Transformation (XSLT) helps to convert an XML from one form to another. Resulting format could be anything, it could be XML, HTML, XFDF, and etc.,.

XSLT Mapper file

Most of the IDE provides XSLT auto mapping facility to convert/map from one XSD format to another XSD format. XPATH, XQuery, and XSLT functions helps to locate and manipulate the source XML file and result the format in target.

XSLT Processor

XSLT processing capability/library comes with most of the popular languages.


import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URL;

import oracle.xml.parser.v2.DOMParser;
import oracle.xml.parser.v2.XMLDocument;
import oracle.xml.parser.v2.XMLParseException;
import oracle.xml.parser.v2.XSLException;
import oracle.xml.parser.v2.XSLProcessor;
import oracle.xml.parser.v2.XSLStylesheet;

import org.xml.sax.SAXException;

public class XSLTTransformation {
public static void main(String[] args) throws MalformedURLException,
XSLException, XMLParseException, SAXException, IOException {

URL xslURL = new URL("file:///tmp/shiporder.xsl");

URL xmlURL = new URL("file:///tmp/shiporder.xml");

XSLProcessor processor = new XSLProcessor();
XSLStylesheet xsl = processor.newXSLStylesheet(xslURL);

// parser input XML content
DOMParser parser = new DOMParser();
parser.setPreserveWhitespace(true);
parser.parse(xmlURL);

XMLDocument xml = parser.getDocument();
processor.showWarnings(true);
processor.setErrorStream(System.err);

// Transform the document
StringWriter strWriter = new StringWriter();
processor.processXSL(xsl, xml, new PrintWriter(strWriter));

System.out.println("Transformed XML file: "
 + strWriter.getBuffer().toString());
strWriter.close();
}
}
javas -cp xmlparserv2.jar XSLTTransformation.java

javas -cp xmlparserv2.jar XSLTTransformation

In the following xml file, If we apply below given XSLT then city and note element are get removed.

shiporder.xml

<?xml version="1.0" encoding="UTF-8" ?>
<shiporder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://www.example.org a.xsd" 
orderid="1" ns0="ns02" xsi="xsi3" xmlns="http://www.example.org">
   <orderperson>orderperson4</orderperson>
   <shipto>
      <name>name5</name>
      <address>address6</address>
      <city>city7</city>
      <country>country8</country>
   </shipto>
   <item>
      <title>title9</title>
      <note>note10</note>
      <quantity>11</quantity>
      <price>120.72</price>
   </item>
   <item>
      <title>title13</title>
      <note>note14</note>
      <quantity>15</quantity>
      <price>160.73</price>
   </item>
   <item>
      <title>title17</title>
      <note>note18</note>
      <quantity>19</quantity>
      <price>200.73</price>
   </item>
</shiporder>

shiporder.xsl

<?xml version="1.0" encoding="UTF-8" ?>
<?oracle-xsl-mapper
  <!-- SPECIFICATION OF MAP SOURCES AND TARGETS, DO NOT MODIFY. -->
  <mapSources>
    <source type="XSD">
      <schema location="../a.xsd"/>
      <rootElement name="shiporder" namespace="http://www.example.org"/>
    </source>
  </mapSources>
  <mapTargets>
    <target type="XSD">
      <schema location="../b.xsd"/>
      <rootElement name="shiporder" namespace="http://www.example.org"/>
    </target>
  </mapTargets>
  <!-- GENERATED BY ORACLE XSL MAPPER 11.1.1.2.0(build 091116.1400.3492) AT [THU MAR 11 12:51:59 IST 2010]. -->
?>
<xsl:stylesheet version="1.0"
                xmlns:xpath20="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.Xpath20"
                xmlns:bpws="http://schemas.xmlsoap.org/ws/2003/03/business-process/"
                xmlns:mhdr="http://www.oracle.com/XSL/Transform/java/oracle.tip.mediator.service.common.functions.MediatorExtnFunction"
                xmlns:oraext="http://www.oracle.com/XSL/Transform/java/oracle.tip.pc.services.functions.ExtFunc"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:ns0="http://www.example.org"
                xmlns:dvm="http://www.oracle.com/XSL/Transform/java/oracle.tip.dvm.LookupValue"
                xmlns:hwf="http://xmlns.oracle.com/bpel/workflow/xpath"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:med="http://schemas.oracle.com/mediator/xpath"
                xmlns:ids="http://xmlns.oracle.com/bpel/services/IdentityService/xpath"
                xmlns:bpm="http://xmlns.oracle.com/bpmn20/extensions"
                xmlns:xdk="http://schemas.oracle.com/bpel/extension/xpath/function/xdk"
                xmlns:xref="http://www.oracle.com/XSL/Transform/java/oracle.tip.xref.xpath.XRefXPathFunctions"
                xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                xmlns:ora="http://schemas.oracle.com/xpath/extension"
                xmlns:socket="http://www.oracle.com/XSL/Transform/java/oracle.tip.adapter.socket.ProtocolTranslator"
                xmlns:ldap="http://schemas.oracle.com/xpath/extension/ldap"
                exclude-result-prefixes="xsi xsl ns0 xsd xpath20 bpws mhdr oraext dvm hwf med ids bpm xdk xref ora socket ldap">
  <xsl:template match="/">
    <ns0:shiporder>
      <xsl:attribute name="orderid">
        <xsl:value-of select="/ns0:shiporder/@orderid"/>
      </xsl:attribute>
      <xsl:attribute name="ns0">
        <xsl:value-of select="/ns0:shiporder/@ns0"/>
      </xsl:attribute>
      <xsl:attribute name="xsi">
        <xsl:value-of select="/ns0:shiporder/@xsi"/>
      </xsl:attribute>
      <xsl:attribute name="schemaLocation">
        <xsl:value-of select="/ns0:shiporder/@schemaLocation"/>
      </xsl:attribute>
      <ns0:orderperson>
        <xsl:value-of select="/ns0:shiporder/ns0:orderperson"/>
      </ns0:orderperson>
      <ns0:shipto>
        <ns0:name>
          <xsl:value-of select="/ns0:shiporder/ns0:shipto/ns0:name"/>
        </ns0:name>
        <ns0:address>
          <xsl:value-of select="/ns0:shiporder/ns0:shipto/ns0:address"/>
        </ns0:address>
        <ns0:country>
          <xsl:value-of select="/ns0:shiporder/ns0:shipto/ns0:country"/>
        </ns0:country>
      </ns0:shipto>
      <xsl:for-each select="/ns0:shiporder/ns0:item">
        <ns0:item>
          <ns0:title>
            <xsl:value-of select="ns0:title"/>
          </ns0:title>
          <ns0:quantity>
            <xsl:value-of select="ns0:quantity"/>
          </ns0:quantity>
          <ns0:price>
            <xsl:value-of select="ns0:price"/>
          </ns0:price>
        </ns0:item>
      </xsl:for-each>
    </ns0:shiporder>
  </xsl:template>
</xsl:stylesheet>
Refer Convert XMLElement/XMLDocument to String

Recent Posts

Unix Commands | List all My Posts

Texts

This blog intended to share the knowledge and contribute to JAVA Community such a way that by providing samples and pointing right documents/webpages. We try to give our knowledege level best and no guarantee can be claimed on truth. Copyright and Terms of Policy refer blogspot.com