I want to transform an XML file into many HTML files using XSL and JAXP. I am able to successfully generate many HTML files. But when description element is more than one paragraph, I am not able to do this. Also any HTML formatting to the text in description is not working. ex: to show some words in bold.
Below are the code snippets I am using. I use command prompt to call the classfile and passing xml and xsl file names as arguments.
can some one throw some light on this.
Regards,
Nagesh
------------------
A sample of XML file is as below
products.xml
<products>
<category>gift products</category>
<product>
<title>this is product One </title>
<image>images/product1.gif</image>
<price-range>$30-$50</price-range>
<description>This product is useful for the below occasions. two more praragraphs </description>
</product>
<product>
<title>this is product Two </title>
<image>images/product2.gif</image>
<price-range>$70-$80</price-range>
<description>Kids like this product. paragraphs of discription</description>
</product>
</products>
my xsl file is as below
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xalan="org.apache.xalan.xslt.extensions.Redi rect"
extension-element-prefixes="xalan">
<xsl:output method="html"/>
<xsl:template match="products">
<html>
<head><title><xsl:value-of select="category"/></title></head>
<body>
<h1><xsl:value-of select="category"/></h1>
<p>The details are <a href="product-1.html">here</a>.</p>
</body>
</html>
<xsl:apply-templates select="product"/>
</xsl:template>
<xsl:template match="product">
<xalan:write select="concat('product-',position(),'.html')">
<html>
<head><title><xsl:value-of select="title"/></title></head>
<body>
<TABLE border ="1" width ="70%">
<TR >
<TD>Title :</TD>
<td><h1><xsl:value-of select="title"/></h1></td>
</TR>
<TR>
<TD>Price range:</TD>
<TD><xsl:value-of select="price-range"/></TD>
</TR>
<TR>
<TD>Description:</TD>
<TD><xsl:value-of select="description"/></TD>
</TR>
<TR>
<TD>Photo: </TD>
<TD><img src="{image}" align="left"/> </TD>
</TR>
</TABLE>
</body>
</html>
</xalan:write>
</xsl:template>
</xsl:stylesheet>
my java file is as below
import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
public class Transform
{
public final static void main(String[] args)
throws IOException, TransformerException, TransformerConfigurationException
{
if(args.length < 2)
{
System.err.println(" incorrect parameters");
return;
}
File input = new File(args[0]),
style = new File(args[1]);
String base = args[0];
int pos = base.lastIndexOf('.');
if(pos != -1)
base = base.substring(0,pos);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(new StreamSource(style));
Source source = new StreamSource(input);
File resultFile = new File(base + '.' + transformer.getOutputProperty(OutputKeys.METHOD));
if(resultFile.getAbsoluteFile().equals(input.getAb soluteFile()))
resultFile = new File("transform-" + base + '.' + transformer.getOutputProperty(OutputKeys.METHOD));
Result result = new StreamResult(resultFile);
transformer.transform(source,result);
}
}