Oracle8i Application Developer's Guide - XML Release 3 (8.1.7) Part Number A86030-01 |
|
Using XML Parser for Java, 9 of 22
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.
new XSLStylesheet()class
is declared with any of the following available methods:
Both inputs can be one of four types:
XSLProcessor.processXSL(xslstylesheet, xml instance)
XML Parser for Java XSL-T Processor is illustrated by the following examples:
/** * 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; } }
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.
import org.w3c.dom.*; import java.util.*; import java.io.*; import java.net.*; import oracle.xml.parser.v2.*; public class XSLTransform { public static void main (String args[]) throws Exception { DOMParser parser; XMLDocument xml, xsldoc, out; URL xslURL; URL xmlURL; try { if (args.length != 2) { // Pass in the names of the XSL and XML files System.err.println("Usage: java XSLTransform xslfile xmlfile"); System.exit(1); } // Parse XSL and XML documents parser = new DOMParser(); parser.setPreserveWhitespace(true); xslURL = createURL(args[0]); parser.parse(xslURL); xsldoc = parser.getDocument(); xmlURL = createURL(args[1]); parser.parse(xmlURL); xml = parser.getDocument(); // Instantiate the 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(); } }
See Figure 17-4 and Figure 17-6. The following provides comments for Example 5:
parser = new DOMParser(); parser.setPreserveWhitespace(true);
xslURL = createURL(args[0]); parser.parse(xslURL); xsldoc = parser.getDocument(); xmlURL = createURL(args[1]); xmlURL = createURL(args[1]); parser.parse(xmlURL); xml = parser.getDocument();
XSLStylesheet xsl = new XSLStylesheet(xsldoc, xslURL); XSLProcessor processor = new XSLProcessor();
processor.setErrorStream(System.err);
DocumentFragment result = processor.processXSL(xsl, xml);
out = new XMLDocument(); Element root = out.createElement("root"); out.appendChild(root); root.appendChild(result);
|
Copyright © 1996-2000, Oracle Corporation. All Rights Reserved. |
|