Hi
my XML contains that.Here i am giving my XML and XSL and the java program.
I am using weblogic6.1 and i kept my xml and xsl file
under C:\bea\wlserver6.1\config\mydomain\applications\DefaultWebApp
and my class file under
C:\bea\wlserver6.1\config\mydomain\applications\DefaultWebApp\web-inf\classes.
Here are thr files.
------------------XML--------------
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
</catalog>
--------------XSL------------
<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
<th>Price</th>
</tr>
<xsl:for-each select="catalog/cd">
<xsl:if test="number(price >= 10)">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
<td><xsl:value-of select="price"/></td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
----------java--------------------
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.IOException;import java.io.InputStream;
public class XSLTServlet extends HttpServlet
{
protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
throws ServletException, IOException
{
super.doGet(httpServletRequest, httpServletResponse);
try
{
System.out.println("-----Inside do get of XSLTSrvlet");
// Get access to the servlet context to load needed resources
ServletContext ctx = getServletContext();
// Load the stylesheet
//String transformFilePath = "/myWebApp/res/xslt/cdshow1.xsl";
String transformFilePath = "cdshow1.xsl";
System.out.println("-----transformFilePath"+transformFilePath);
InputStream transformStream = ctx.getResourceAsStream(transformFilePath);
System.out.println("-----creating resourceStream for :"+transformFilePath);
Source transformSource = new StreamSource(transformStream);
System.out.println("-----creating stream source for : "+transformSource);
// Create the transformer object that will perform the XSLT transformation
TransformerFactory transformerFactory = TransformerFactory.newInstance();
System.out.println("-----Transformer factory"+transformerFactory);
Transformer transformer = transformerFactory.newTransformer(transformSource);
// Load the xml file
//String xmlFilename = "/myWebApp/res/data/cd-collection.xml";
String xmlFilename = "cd.xml";
InputStream xmlStream = ctx.getResourceAsStream(xmlFilename);
Source xmlSource = new StreamSource(xmlStream);
// Create a result object to produce its result in the response text
Result result = new StreamResult(httpServletResponse.getOutputStream());
// Perform the transformation
transformer.transform(xmlSource, result);
} catch (IOException e)
{
e.printStackTrace();
} catch (TransformerFactoryConfigurationError transformerFactoryConfigurationError)
{
transformerFactoryConfigurationError.printStackTrace();
} catch (TransformerException e)
{
e.printStackTrace();
}
}
}
Regards
Ramesh R G V S