• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

I'm getting an error about my stylesheet missing the xsl:version attribute

 
Ranch Hand
Posts: 681
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Tony Evans
Ranch Hand
Posts: 681
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Solved the problem, a copy and paste mistake, I have a reasonable excuse its very late in the UK.
Correct line of code is
File filexsl = new File("C://javabay/XSTLParser/book.xsl"); I was calling the XML file.
Tony
reply
    Bookmark Topic Watch Topic
  • New Topic