XSLT in JSP 2 : XML « JSP « Java






XSLT in JSP 2


/*
  Java, XML, and Web Services Bible
  Mike Jasnowski
  ISBN: 0-7645-4847-6
*/
<%@ page contentType="image/svg-xml" %>
<%@ page import="org.xml.sax.*" %>
<%@ page import="org.apache.xalan.xslt.*" %>
<%@ page import="java.io.*" %>
<%

try {

    XSLTProcessor processor = XSLTProcessorFactory.getProcessor(); 
       
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    
    processor.process(new XSLTInputSource("games.xml"),
                      new XSLTInputSource("games.xsl"),
                    new XSLTResultTarget(bos));

    out.println(bos);
    
} catch (Exception ex) {
    out.println(ex);
}

%>


<?xml version="1.0"?>
<games>
  <game genre="rpg">XML Invaders</game>
  <game genre="rpg">A Node in the XPath</game>
  <game genre="rpg">XPath Racers</game>
</games>

//games.xsl

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html"/>
<xsl:template match="/">
<svg width="200" height="200">
<g>
<xsl:for-each select="games/game">
<text x="10" y="{number(10)*position()}">
<xsl:value-of select="text()"/>
</text>
</xsl:for-each>
</g>
</svg>
</xsl:template>
</xsl:stylesheet>

           
       








Related examples in the same category

1.Using the Core XML tags
2.XML transformation
3.Performing XSL Transformations
4.JSP in pure XML generating conforming XHTML
5.XSLT In JSP
6.JSP Parsing using the DOM
7.JSP Parsing using JDOM
8.JSP Parsing using the DOM and JSTL
9.JSP and SAX
10.JSP Displaying a Subset in XML
11.JSP XML and XSLT transform
12.JSP List of data in the XML document
13.Deal With XML In JSP