Помогите разобраться в проблеме. Я не могу понять, почему появляется исключение TransformerException.
Сделал всё как было написано в тьюториале, но не работает. Вылетает вот какая ошибка
ERROR: 'The input document is not a stylesheet (the XSL namespace is not declared in the root element).'
FATAL ERROR: 'Could not compile stylesheet'
* Transformer Factory error
Could not compile stylesheet
javax.xml.transform.TransformerConfigurationException: Could not compile stylesheet
at
com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.jav
a:824)
at
com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTransformer(TransformerFactoryImpl.j
ava:619)
at Stylizer.main(Stylizer.java:50)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)
Не понимаю, почему компилятор находит проблемы в xsl-файле.
article.xml
Код |
<?xml version="1.0"?> <ARTICLE> <TITLE>A Sample Article</TITLE> <SECT>The First Major Section <PARA>This section will introduce a subsection.</PARA> <SECT>The Subsection Heading <PARA>This is the text of the subsection. </PARA> </SECT> </SECT> </ARTICLE>
|
article1a.xsl
Код |
<?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet xmlns:xsl="http://www.w3c.org/1999/XSL/Transform" version="1.0" > <xsl:output method="html"/>
<xsl:template match="/"> <html><body> <xsl:apply-templates/> </body></html> </xsl:template>
<xsl:template match="/ARTICLE/TITLE"> <h1 align="center"> <xsl:apply-templates/> </h1> </xsl:template>
<xsl:template match="/ARTICLE/SECT"> <h2> <xsl:apply-templates select="text()|B|I|U|DEF|LINK"/> </h2> <xsl:apply-templates select="SECT|PARA|LIST|NOTE"/> </xsl:template>
<xsl:template match="/ARTICLE/SECT/SECT"> <h3><xsl:apply-templates select="text()|B|I|U|DEF|LINK"/></h3> <xsl:apply-templates select="SECT|PARA|LIST|NOTE"/> </xsl:template>
<xsl:template match="/ARTICLE/SECT/SECT/SECT"> <xsl:message terminate="yes"> Error: Sections can only be nested 2 deep. </xsl:message> </xsl:template>
<xsl:template match="PARA"> <p><xsl:apply-templates/></p> </xsl:template>
</xsl:stylesheet>
|
Stylizer.java
Код |
/** * Created by IntelliJ IDEA. * User: Bogdan * Date: 30.12.2004 * Time: 12:47:30 * To change this template use File | Settings | File Templates. */ import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException; import org.xml.sax.SAXParseException;
import org.w3c.dom.Document;
import javax.xml.transform.*; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamSource; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.OutputKeys;
import java.io.*;
public class Stylizer { static Document document;
public static void main(String argv[]) { if (argv.length != 2) { System.err.println("Usage: java Stylizer stylesheet zmlfile"); System.exit(1); }
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //factory.setNamespaceAware(true); //factory.setValidating(true);
try { File stylesheet = new File(argv[0]); File datafile = new File(argv[1]);
DocumentBuilder builder = factory.newDocumentBuilder(); document = builder.parse(datafile); //Use a Transformer for output TransformerFactory tFactory = TransformerFactory.newInstance(); StreamSource stylesource = new StreamSource(stylesheet); Transformer transformer = tFactory.newTransformer(stylesource);
DOMSource source = new DOMSource(document); StreamResult result = new StreamResult(System.out); transformer.transform(source, result);
if (document.getDoctype() != null) { String systemValue = (new File(document.getDoctype().getSystemId())).getName(); transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,systemValue); } } catch(SAXParseException spe) { //Error generated by the parser System.out.println("\n** Parsing error" + ", line " + spe.getLineNumber() + ", uri " + spe.getSystemId()); System.out.println(" "+spe.getMessage());
//Use the contained exception, if any Exception x = spe; if (spe.getException() != null) x = spe.getException(); x.printStackTrace(); } catch(SAXException sxe) { //Error generated by this application //(or a parser-initialization error) Exception x = sxe; if (sxe.getException() != null) x = sxe.getException(); x.printStackTrace(); } catch(ParserConfigurationException pce) { //Parser with specified options can't be built pce.printStackTrace(); } catch(IOException ioe) { //I/O error ioe.printStackTrace(); } catch(TransformerConfigurationException tce) { //Error generated by the parser System.out.println("* Transformer Factory error"); System.out.println(" " + tce.getMessage());
//Use the contained exception, if any Throwable x = tce; if (tce.getException() != null) x = tce.getException(); x.printStackTrace(); } catch(TransformerException te) { //Error generated by the parser System.out.println("* Transformation error"); System.out.println(" "+te.getMessage());
//Use the contained exception, if any Throwable x = te; if (te.getException() != null) x = te.getException(); x.printStackTrace(); } } }
|