XML/XSLT in Code
If you've done any XML work at all, you know that there are a variety of XML parsers available for every major programming language. In general, these parsers open an XML document and assign that document's root node to either a Document or an Element class. Traversal functionality (such as getNextChild()) is also provided to allow the XML tree to be "walked" in code. Of course, not all XML content is stored within a file. In practice, most XML is actually retrieved from a standard relational database, either through the use of raw XML storage in the database or some translation software provided by the database vendor that runs on top of the database. Once the XML document has been initialized in code, a stylesheet can be applied to generate content to be sent back to the client. As an example, the following code snippet shows this operation being completed using the Oracle XML Developer's Kit Java API. (Note: xslin is a String containing the XSL file contents; xmlin is a String containing the XML file contents).
DOMParser parser; XMLDocument xmldoc, xsldoc; URL xslURL; URL xmlURL; try { parser = new DOMParser(); parser.setPreserveWhitespace(true); // parser input XSL file parser.parse(xslin); xsldoc = parser.getDocument(); // parser input XML string parser.parse(new StringReader(xmlin)); xmldoc = parser.getDocument(); // instantiate a stylesheet XSLStylesheet xslsheet = new XSLStylesheet(xsldoc, xslin); XSLProcessor processor = new XSLProcessor(); // display any warnings that may occur processor.showWarnings(true); processor.setErrorStream(System.err); // Process XSL processor.processXSL(xslsheet, xmldoc, out); } catch (Exception e) { e.printStackTrace(); }