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

&lt;?xml version="1.0"?>
&lt;xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
&lt;xsl:template match="/">
&lt;html>
&lt;xsl:apply-templates/>
&lt;/html>
&lt;/xsl:template>
&lt;xsl:template match="BRADYS">
&lt;body>
&lt;xsl:apply-templates/>
&lt;/body>
&lt;/xsl:template>
&lt;xsl:template match="BRADYGIRL">
&lt;xsl:value-of select="FNAME"/>&lt;BR/>
&lt;/xsl:template>
&lt;/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


&lt;?xml version="1.0"?>
&lt;xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
&lt;xsl:template match="/">
&lt;html>
&lt;xsl:apply-templates/>
&lt;/html>
&lt;/xsl:template>
&lt;xsl:template match="BRADYS">
&lt;body>
&lt;xsl:apply-templates/>
&lt;/body>
&lt;/xsl:template>
&lt;xsl:template match="BRADYGIRL">
&lt;xsl:value-of select="."/>&lt;BR/>
&lt;/xsl:template>
&lt;/xsl:stylesheet>


&lt;?xml version="1.0"?>
&lt;xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
&lt;xsl:template match="/">
&lt;html>
&lt;xsl:apply-templates/>
&lt;/html>
&lt;/xsl:template>
&lt;xsl:template match="BRADYS">
&lt;body>
&lt;xsl:apply-templates/>
&lt;/body>
&lt;/xsl:template>
&lt;xsl:template match="BRADYGIRL">
&lt;xsl:value-of select="FNAME"/>
&lt;xsl:value-of select="LNAME"/>
&lt;xsl:value-of select="AGE"/>
&lt;xsl:value-of select="HOBBY"/>
&lt;xsl:value-of select="SCHOOL"/>&lt;BR/>
&lt;/xsl:template>
&lt;/xsl:stylesheet>



Listing 9 Use of "for-each"


&lt;?xml version="1.0"?>
&lt;xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
&lt;xsl:template match="/">
&lt;html>
&lt;xsl:apply-templates/>
&lt;/html>
&lt;/xsl:template>
&lt;xsl:template match="BRADYS">
&lt;body>
&lt;xsl:for-each select="BRADYGIRL">
&lt;xsl:value-of select="."/>&lt;BR/>
&lt;/xsl:for-each>

&lt;/body>
&lt;/xsl:template>
&lt;/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:
&lt;?xml version="1.0"?>
&lt;?xml-stylesheet type="text/xsl" href="bradysheet.xsl"?>
&lt;!--This is a standard well formed XML file -->
&lt;BRADYS>
&lt;BRADYGIRL>
&lt;FNAME>Marsha&lt;/FNAME>
&lt;LNAME>Brady&lt;/LNAME>
&lt;AGE>17&lt;/AGE>
&lt;HOBBY>Boys&lt;/HOBBY>
&lt;SCHOOL>Westdale Senior High&lt;/SCHOOL>
&lt;/BRADYGIRL>
&lt;BRADYGIRL>
&lt;FNAME>Jan&lt;/FNAME>
&lt;LNAME>Brady&lt;/LNAME>
&lt;AGE>15&lt;/AGE>
&lt;HOBBY>Cheerleading&lt;/HOBBY>
&lt;SCHOOL>Filmore Junior High&lt;/SCHOOL>
&lt;/BRADYGIRL>
&lt;BRADYGIRL>
&lt;FNAME>Cindy&lt;/FNAME>
&lt;LNAME>Brady&lt;/LNAME>
&lt;AGE>11&lt;/AGE>
&lt;HOBBY>Dolls&lt;/HOBBY>
&lt;SCHOOL>Clinton Avenue Elementary School&lt;/SCHOOL>
&lt;/BRADYGIRL>
&lt;/BRADYS>


Stylesheet:
&lt;?xml version="1.0" ?>
&lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
&lt;!-- This is a nonstandard stylesheet designed just for
Internet Explorer. It will not work with any standards
compliant XSLT processor. such as JAXP -->
&lt;xsl:template match="/">
&lt;html>
&lt;xsl:apply-templates/>
&lt;/html>
&lt;/xsl:template>
&lt;xsl:template match="BRADYS">
&lt;xsl:apply-templates/>
&lt;/xsl:template>
&lt;xsl:template match="BRADYGIRL">
&lt;P>
&lt;xsl:value-of select="."/>
&lt;/P>
&lt;/xsl:template>
&lt;/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


&lt;?xml version="1.0"?>
&lt;xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
&lt;xsl:template match="/">
&lt;html>
&lt;xsl:apply-templates/>
&lt;/html>
&lt;/xsl:template>
&lt;xsl:template match="BRADYS">
&lt;body>
&lt;table border="2" bgcolor="pink">
&lt;th>First Name&lt;/th>
&lt;th>Last Name&lt;/th>
&lt;th>Age&lt;/th>
&lt;th>Hobby&lt;/th>
&lt;th>School&lt;/th>

&lt;xsl:for-each select="BRADYGIRL">
&lt;tr>
&lt;td>&lt;xsl:value-of select="FNAME"/>&lt;/td>
&lt;td>&lt;xsl:value-of select="LNAME"/>&lt;/td>
&lt;td>&lt;xsl:value-of select="AGE"/>&lt;/td>
&lt;td>&lt;xsl:value-of select="HOBBY"/>&lt;/td>
&lt;td>&lt;xsl:value-of select="SCHOOL"/>&lt;/td>
&lt;/tr>
&lt;/xsl:for-each>
&lt;/table>
&lt;/body>

&lt;/xsl:template>
&lt;/xsl:stylesheet>




Listing 14


&lt;?xml version="1.0"?>
&lt;xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
&lt;xsl:output method="wml" indent="yes"/>
&lt;xsl:template match="/">
&lt;wml>
&lt;xsl:apply-templates/>
&lt;/wml>
&lt;/xsl:template>
&lt;xsl:template match="BRADYS">
&lt;card id="emp" title="Employees">
&lt;table columns = "5">

&lt;td>First Name&lt;/td>
&lt;td>Last Name&lt;/td>
&lt;td>Age&lt;/td>
&lt;td>Hobby&lt;/td>
&lt;td>School&lt;/td>
&lt;xsl:for-each select="BRADYGIRL">
&lt;tr>
&lt;td>&lt;xsl:value-of select="FNAME"/>&lt;/td>
&lt;td>&lt;xsl:value-of select="LNAME"/>&lt;/td>
&lt;td>&lt;xsl:value-of select="AGE"/>&lt;/td>
&lt;td>&lt;xsl:value-of select="HOBBY"/>&lt;/td>
&lt;td>&lt;xsl:value-of select="SCHOOL"/>&lt;/td>
&lt;/tr>
&lt;/xsl:for-each>
&lt;/table>
&lt;/card>

&lt;/xsl:template>
&lt;/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());
}
}
}