![]()
Rendering XML Documents
An XML document contains data, but does not contain information about how that data should be displayed. That information is in an associated stylesheet.
- If an XML document is associated with a CSS2 type stylesheet, your application can parse and render it without any additional libraries.
- To render an XML document associated with an XSLT stylesheet, you need to include additional libraries on the classpath. Include the ib6xalan.jar so that the Xalan module can perform the XSL transformations. Also include the xalan.jar and possibly the xml-apis.jar, depending on what version of Xalan you download from http://xml.apache.org/.
- You also must make a static call to the DOM to set the Transformer Factory:
DOM.setTransformFactory( stormBase, transformFactory );- This call ensures that the core classes will correctly handle XSL transformations.
- If an XML document is not associated with either a CSS2 or XSLT stylesheet, it may optionally be displayed in the browser as a Document Tree. In this case, the HTML4 pilot internally converts the XML document into an HTML document suitable for display and navigation in the browser. To enable the automatic conversion of XML documents to a Document Tree in the HTML4 Pilot, the following system property must be defined:
ice.pilots.html4.viewXmlAsTree=trueNote: An XML document will also be displayed as a Document Tree if it references an XSLT stylesheet and support for the optional Xalan XSL transformation engine is not configured.
XML Examples
For information on how to use the browser component as an XML parser without instantiating any graphical objects, see the example Parser.java, described in Example Files.
The following example (t1.xml) shows an XML document with corresponding CSS file.
<?xml-stylesheet href="t1style.css" type="text/css"?> <docu> <heading>XML rendering test</heading> <para>This test demonstrates the ability of ICEbrowser to display arbitrary XML data with CSS2 stylesheets.</para> <para>Starting with the final release of 5.0, ICEbrowser supports the XML/stylesheet linking recommendation from W3C.</para> <para>A shortcut: There is no DTD support for XML (for now) so you must make sure that all the tags are opened and closed correctly.</para> <para>Just for fun: <red>This text is red, move mouse over it</red></para> </docu>The CSS (t1style.css) tells the browser how to display the page:
docu {display: block; padding: 10px;} heading, para {display: block; margin: 20px 0px;} para:hover {outline: 4px solid black;} red {color: white; background-color: red;} red:hover {background-color: blue; outline: 2px solid green;} heading {font-weight: bold; font-size: 2em; text-align: center;} bold {font-weight: bold;}XML Document With XSLT Stylesheet
The following example (poem1.xml) shows an XML document with corresponding XSLT stylesheet. This example can be found in src/ICEbrowserExamples/src/xslt.
<?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="poemstyle.xsl"?> <POEM author="Robert Frost" title="Birches"> <LINE> When I see birches bend to left and right </LINE> <LINE> Across the lines of straighter darker trees, </LINE> <LINE> I like to think some boy's been swinging them. </LINE> <LINE> But swinging doesn't bend them down to stay. </LINE> <LINE> Ice-storms do that. </LINE> </POEM>The accompanying XSLT stylesheet (poemstyle.xsl) is as follows:
<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <HTML> <HEAD> </HEAD> <BODY> <xsl:apply-templates select="POEM"/> </BODY> </HTML> </xsl:template> <xsl:template match="POEM"> <CENTER> <H2> <xsl:value-of select="@title"/> </H2> <br/> by <xsl:value-of select="@author"/> <p> <xsl:apply-templates select="LINE"/> </p> </CENTER> </xsl:template> <xsl:template match="LINE"> <BR> <xsl:value-of select="."/> </xsl:template> </xsl:stylesheet>
|
Copyright 2005. ICEsoft Technologies, Inc. http://www.icesoft.com |