I am getting the following error:
javax.xml.transform.TransformerException: stylesheet requires attribute: version.
Looking at documents there seems to be two reasons why tou would get this error:
reason 1) Ensure the URI for the xsl namespace is "http://www.w3.org/1999/XSL/Transform".
But in my XSL style sheet I have the correct format:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<HEAD>
<TITLE>My Books</TITLE>
</HEAD>
<body bgcolor="lightslategray">
<table border="4">
<TR>
<TH>Category</TH>
<TH>Title</TH>
<TH>Author</TH>
<TH>Price</TH>
</TR>
<xsl:apply-templates select="books/book">
<xsl:sort select="@category"></xsl:sort>
</xsl:apply-templates>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="books/book">
<TR>
<TD>
<xsl:value-of select="@category"></xsl:value-of>
</TD>
<TD>
<xsl:value-of select="./title"></xsl:value-of>
</TD>
<TD>
<xsl:value-of select="./author"></xsl:value-of>
</TD>
<TD>
<xsl:value-of select="./price"></xsl:value-of>
</TD>
</TR>
</xsl:template>
</xsl:stylesheet>
Reason 2 : The parser you are using to process a stylesheet Source and generate a Transformer does not have the namespaceAware property set to true.
But if you use a TransformerFactory to process a stylesheet Source and generate a Transformer, the TransformerFactory instructs the SAXParserFactory to set the parser's namespaceAware property to true. which is what I am using in my code:
package XSTLParser;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import java.io.File;
public class XSLTTest
{
public static void main(
String[] args)
{
StreamSource source = null;
StreamResult result = null;
File filexml = new File("C://javabay/XSTLParser/books.xml");
File filexsl = new File("C://javabay/XSTLParser/books.xml");
if(!filexml.exists())
{
System.out.println("Cant find XML file");
return;
}
else if(!filexsl.exists())
{
System.out.println("Cant find XSL file");
return;
}
else
{
System.out.println("Found file");
source = new StreamSource(filexml);
result = new StreamResult(System.out);
}
TransformerFactory factory = TransformerFactory.newInstance();
try
{
Transformer transformer = factory.newTransformer(new StreamSource(filexsl));
//System.out.println("Call transformer");
//transformer.transform(source,result);
}
catch(TransformerConfigurationException tce)
{
tce.printStackTrace();
}
/* catch(TransformerException te)
{
te.printStackTrace();
}*/
}
}
Thanks for any help
Tony