Listing 1 An XML File with Three Bradygirls
<?xml version="1.0"?>
<!--This is a standard well formed XML file -->
<BRADYS>
<BRADYGIRL>
<FNAME>Marsha</FNAME>
<LNAME>Brady</LNAME>
<AGE>17</AGE>
<HOBBY>Boys</HOBBY>
<SCHOOL>Westdale Senior High</SCHOOL>
</BRADYGIRL>
<BRADYGIRL>
<FNAME>Jan</FNAME>
<LNAME>Brady</LNAME>
<AGE>15</AGE>
<HOBBY>Cheerleading</HOBBY>
<SCHOOL>Filmore Junior High</SCHOOL>
</BRADYGIRL>
<BRADYGIRL>
<FNAME>Cindy</FNAME>
<LNAME>Brady</LNAME>
<AGE>11</AGE>
<HOBBY>Dolls</HOBBY>
<SCHOOL>Clinton Avenue Elementary School</SCHOOL>
</BRADYGIRL>
</BRADYS>
Listing 2 Basic Stylesheet
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<title>
Brady Girls
</title>
</html>
</xsl:template>
</xsl:stylesheet>
Listing 3 Transformation Results
<html>
<title>
Brady Girls
</title>
</html>
Listing 4 Processing Child Nodes
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<xsl:apply-templates/>
</html>
</xsl:template>
<xsl:template match="BRADYS">
<body>
<xsl:apply-templates/>
</body>
</xsl:template>
<xsl:template match="BRADYGIRL">
Brady Girl Found!!!!<BR/>
</xsl:template>
</xsl:stylesheet>
Listing 5 Transformation Results
<html>
<body>
Brady Girl Found!!!!
Brady Girl Found!!!!
Brady Girl Found!!!!
</body>
</html>
Listing 6 Displaying the Actual Value
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<xsl:apply-templates/>
</html>
</xsl:template>
<xsl:template match="BRADYS">
<body>
<xsl:apply-templates/>
</body>
</xsl:template>
<xsl:template match="BRADYGIRL">
<xsl:value-of select="FNAME"/><BR/>
</xsl:template>
</xsl:stylesheet
Listing 7 HTML with the Tag Value
<html><body>
Marsha
Jan
Cindy
</body></html>
Listing 8 Displaying All Tags for a Current Node
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<xsl:apply-templates/>
</html>
</xsl:template>
<xsl:template match="BRADYS">
<body>
<xsl:apply-templates/>
</body>
</xsl:template>
<xsl:template match="BRADYGIRL">
<xsl:value-of select="."/><BR/>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<xsl:apply-templates/>
</html>
</xsl:template>
<xsl:template match="BRADYS">
<body>
<xsl:apply-templates/>
</body>
</xsl:template>
<xsl:template match="BRADYGIRL">
<xsl:value-of select="FNAME"/>
<xsl:value-of select="LNAME"/>
<xsl:value-of select="AGE"/>
<xsl:value-of select="HOBBY"/>
<xsl:value-of select="SCHOOL"/><BR/>
</xsl:template>
</xsl:stylesheet>
Listing 9 Use of "for-each"
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<xsl:apply-templates/>
</html>
</xsl:template>
<xsl:template match="BRADYS">
<body>
<xsl:for-each select="BRADYGIRL">
<xsl:value-of select="."/><BR/>
</xsl:for-each>
</body>
</xsl:template>
</xsl:stylesheet>
Listing 10 Resulting HTML File
<html>
<body>
Marsha Brady
17
Boys
Westdale Senior High
<br>
Jan Brady
15
Cheerleading
Filmore Junior High
<br>
Cindy Brady
11
Dolls
Clinton Avenue Elementary School
<br>
</body>
</html>
Listing 11 Microsoft Version of Stylesheet Support
XML File:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="bradysheet.xsl"?>
<!--This is a standard well formed XML file -->
<BRADYS>
<BRADYGIRL>
<FNAME>Marsha</FNAME>
<LNAME>Brady</LNAME>
<AGE>17</AGE>
<HOBBY>Boys</HOBBY>
<SCHOOL>Westdale Senior High</SCHOOL>
</BRADYGIRL>
<BRADYGIRL>
<FNAME>Jan</FNAME>
<LNAME>Brady</LNAME>
<AGE>15</AGE>
<HOBBY>Cheerleading</HOBBY>
<SCHOOL>Filmore Junior High</SCHOOL>
</BRADYGIRL>
<BRADYGIRL>
<FNAME>Cindy</FNAME>
<LNAME>Brady</LNAME>
<AGE>11</AGE>
<HOBBY>Dolls</HOBBY>
<SCHOOL>Clinton Avenue Elementary School</SCHOOL>
</BRADYGIRL>
</BRADYS>
Stylesheet:
<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<!-- This is a nonstandard stylesheet designed just for
Internet Explorer. It will not work with any standards
compliant XSLT processor. such as JAXP -->
<xsl:template match="/">
<html>
<xsl:apply-templates/>
</html>
</xsl:template>
<xsl:template match="BRADYS">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="BRADYGIRL">
<P>
<xsl:value-of select="."/>
</P>
</xsl:template>
</xsl:stylesheet>
Listing 12 Java Servlet
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class BradyXML extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException,IOException{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
try {
// Below file exist in the default server folder
StreamSource xml = new StreamSource(new File("bradys.xml"));
StreamSource xsl = new StreamSource(new File("bradysheet.xsl"));
StreamResult result = new StreamResult(out);
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(xsl);
transformer.transform(xml,result);
} // try
catch(Exception e){
System.out.println(e.getMessage());
}
}
}
Listing 13 Final Version of the Stylesheet
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<xsl:apply-templates/>
</html>
</xsl:template>
<xsl:template match="BRADYS">
<body>
<table border="2" bgcolor="pink">
<th>First Name</th>
<th>Last Name</th>
<th>Age</th>
<th>Hobby</th>
<th>School</th>
<xsl:for-each select="BRADYGIRL">
<tr>
<td><xsl:value-of select="FNAME"/></td>
<td><xsl:value-of select="LNAME"/></td>
<td><xsl:value-of select="AGE"/></td>
<td><xsl:value-of select="HOBBY"/></td>
<td><xsl:value-of select="SCHOOL"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</xsl:template>
</xsl:stylesheet>
Listing 14
<?xml version="1.0"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="wml" indent="yes"/>
<xsl:template match="/">
<wml>
<xsl:apply-templates/>
</wml>
</xsl:template>
<xsl:template match="BRADYS">
<card id="emp" title="Employees">
<table columns = "5">
<td>First Name</td>
<td>Last Name</td>
<td>Age</td>
<td>Hobby</td>
<td>School</td>
<xsl:for-each select="BRADYGIRL">
<tr>
<td><xsl:value-of select="FNAME"/></td>
<td><xsl:value-of select="LNAME"/></td>
<td><xsl:value-of select="AGE"/></td>
<td><xsl:value-of select="HOBBY"/></td>
<td><xsl:value-of select="SCHOOL"/></td>
</tr>
</xsl:for-each>
</table>
</card>
</xsl:template>
</xsl:stylesheet>
Listing 15 The Updated Servlet
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class BradyXML extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException,IOException{
// First, determine the type of browser we have been hit with
String acc = request.getHeader("Accept");
boolean HTML = false;
if(acc.indexOf("wml") != -1) {
// WAP browser
HTML = false;
response.setContentType("text/vnd.wap.wml");
}
else{
// HTML browser
HTML = true;
response.setContentType("text/html");
}
PrintWriter out = response.getWriter();
try {
// Below file exist in the default server folder
StreamSource xml = new StreamSource(new File("bradys.xml"));
StreamSource xsl;
if(HTML == true){
xsl = new StreamSource(new File("bradys_html.xsl"));
}
else{
xsl = new StreamSource(new File("bradys_wml.xsl"));
}
StreamResult result = new StreamResult(out);
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(xsl);
transformer.transform(xml,result);
} // try
catch(Exception e){
System.out.println(e.getMessage());
}
}
}