Oracle8i Application Developer's Guide - XML
Release 3 (8.1.7)

Part Number A86030-01

Library

Product

Contents

Index

Go to previous page Go to beginning of chapter Go to next page

Using XML Parser for Java, 9 of 22


Using XML Parser for Java: XSL-T Processor

To implement the XSL-T Processor in the XML Parser for Java use XSLProcessor class.

Figure 17-6 shows the overall process used by the XSLProcessor class.

  1. A new XSLProcessor() class declaration begins the XSL-T process.

  2. There are two inputs:

    • "Stylesheet". First a stylesheet is built. A new XSLStylesheet()class is declared with any of the following available methods:

      • removeParam()

      • resetParam()

      • setParam()

    • "XML input". This can can repeat 1 through n times for a particular stylesheet. This inputs the "Process Stylesheet" step.

    Both inputs can be one of four types:

    • input stream

    • URL

    • XML document

    • Reader

  3. The resulting stylesheet object and the XML input, feed the "Process Styesheet" step, namely:

    XSLProcessor.processXSL(xslstylesheet, xml instance)
    
    
  4. The XSLProcessor.processXSL() method processes the XML input 1 through n times, using the selected stylesheet.

  5. XSLProcessor.processXSL() outputs either an output stream or a DOM document.

XML Parser for Java XSL-T Processor is illustrated by the following examples:

Figure 17-6 XSLProcessor Class Process


XML Parser for Java Example 4: (XSLSample.java)

/**
 * This file gives a simple example of how to use the XSL processing 
 * capabilities of the Oracle XML Parser V2.0. An input XML document is
 * transformed using a given input stylesheet
 */

import org.w3c.dom.*;
import java.util.*;
import java.io.*;
import java.net.*;
import oracle.xml.parser.v2.*;

public class XSLSample 
{
   /**
    * Transforms an xml document using a stylesheet
    * @param args input xml and xml documents
    */
   public static void main (String args[]) throws Exception
   {
      DOMParser parser;
      XMLDocument xml, xsldoc, out;
      URL xslURL;
      URL xmlURL;

      try 
      {

         if (args.length != 2) 
         {
            // Must pass in the names of the XSL and XML files
            System.err.println("Usage: java XSLSample xslfile xmlfile");
            System.exit(1);
         }

         // Parse xsl and xml documents
         
         parser = new DOMParser();
         parser.setPreserveWhitespace(true);

         // parser input XSL file
         xslURL = createURL(args[0]);
         parser.parse(xslURL);
         xsldoc = parser.getDocument();
         
         // parser input XML file
         xmlURL = createURL(args[1]);
         parser.parse(xmlURL);
         xml = parser.getDocument();

         // instantiate a stylesheet
         XSLStylesheet xsl = new XSLStylesheet(xsldoc, xslURL);
         XSLProcessor processor = new XSLProcessor();

         // display any warnings that may occur
         processor.showWarnings(true);
         processor.setErrorStream(System.err);

         // Process XSL
         DocumentFragment result = processor.processXSL(xsl, xml);

         // create an output document to hold the result
         out = new XMLDocument();

         // create a dummy document element for the output document
         Element root = out.createElement("root");
         out.appendChild(root);

         // append the transformed tree to the dummy document element
         root.appendChild(result);
         
         // print the transformed document
         out.print(System.out);
      }
      catch (Exception e)
      {
         e.printStackTrace();
      }
   }

   // Helper method to create a URL from a file name
   static URL createURL(String fileName)
   {
      URL url = null;
      try 
      {
         url = new URL(fileName);
      } 
      catch (MalformedURLException ex) 
      {
         File f = new File(fileName);
         try 
         {
            String path = f.getAbsolutePath();
            // This is a bunch of weird code that is required to
            // make a valid URL on the Windows platform, due
            // to inconsistencies in what getAbsolutePath returns.
            String fs = System.getProperty("file.separator");
            if (fs.length() == 1)
            {
               char sep = fs.charAt(0);
               if (sep != '/')
                  path = path.replace(sep, '/');
               if (path.charAt(0) != '/')
                  path = '/' + path;
            }
            path = "file://" + path;
            url = new URL(path);
         } 
         catch (MalformedURLException e) 
         {
            System.out.println("Cannot create url for: " + fileName);
            System.exit(0);
         }
      }
      return url;
   }
}

XML Parser for Java Example 5: Using the DOMAPI and XSL-T Processor

This example code is not included in the /sample subdirectory. The following Java code uses the XML Parser for Java, V2, to perform the following tasks.

}


Comments on XSL-T Example 5

See Figure 17-4 and Figure 17-6. The following provides comments for Example 5:

  1. The program inputs two URL documents:

    • URL xmlURL;

    • URL xslURL;

  2. Parse the two documents and set the preserve white space property:

    parser = new DOMParser();
    parser.setPreserveWhitespace(true);
    
    
  3. Get the XSL and XML documenst

    xslURL = createURL(args[0]);
    parser.parse(xslURL);
    xsldoc = parser.getDocument();
    
    xmlURL = createURL(args[1]);
    xmlURL = createURL(args[1]);
    parser.parse(xmlURL);
    xml = parser.getDocument();
    
    
  4. Initialize a new XSLStylesheet and XSLProcessor class:

    XSLStylesheet xsl = new XSLStylesheet(xsldoc, xslURL);
    
    XSLProcessor processor = new XSLProcessor();
    
        processor.setErrorStream(System.err);
    
    
  5. Process the stylesheet

    DocumentFragment result = processor.processXSL(xsl, xml);
    
    
  6. Output the DOM XML transformed document

    out = new XMLDocument();
    Element root = out.createElement("root");
    out.appendChild(root);
    root.appendChild(result);
    
    

Go to previous page Go to beginning of chapter Go to next page
Oracle
Copyright © 1996-2000, Oracle Corporation.

All Rights Reserved.

Library

Product

Contents

Index