Hello World for XSLT

From: Larry Coon (lcnospam_at_assist.org)
Date: 03/30/04


Date: Tue, 30 Mar 2004 07:01:20 -0800

I'm trying to do the equivalent of a "Hello, world"
program for XSLT, and it's failing. I have Sun's
JAXP 1.2.4 & JDK 1.4.2, on an XP Pro box. I have
successfully been able to create DOMs and write them
to disk as an XML file using a Transformer. This is
my first attempt at using an XSL file. When I try
this, it fails (I've shortened it as much as possible
while preserving the problem):

import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.w3c.dom.*;

public class HelloXSLT {
  public static void main(String[] args) throws Exception {
    String inputXML = "c:\\java\\xml\\hello.xml";
    String inputXSL = "c:\\java\\xml\\hello.xsl";
    String outputHTML = "c:\\java\\xml\\hello.html";

    DocumentBuilderFactory builderFactory;
    DocumentBuilder builder;
    Document document;
    TransformerFactory transformerFactory;
    Transformer transformer;
    Source xmlSource, xslSource;
    Result result;

    builderFactory = DocumentBuilderFactory.newInstance();
    transformerFactory = TransformerFactory.newInstance();
    builder = builderFactory.newDocumentBuilder();
    document = builder.parse(new File(inputXML));
    xmlSource = new DOMSource(document);
    xslSource = new StreamSource(new File(inputXSL));
    result = new StreamResult(new File(outputHTML));
    transformer = transformerFactory.newTransformer(xslSource);
    transformer.transform(xmlSource, result);
  }
}

----------------------------------------------------------
Here is my xsl file:

<?xml version="1.0"?>
<!-- hello.xsl -->

<xsl:style*** version = "1.0"
  xmlns:xsl = "http://w3.org/1999/XSL/Transform">

  <xsl:template match = "hello">
    <html>
      <body><xsl:value-of select = "message"/></body>
    </html>
  </xsl:template>
</xsl:style***>

---------------------------------------------------------
And here is my xml file:

<?xml version="1.0"?>
<!-- hello.xml -->

<hello>
  <message>Hello, XSLT</message>
</hello>

---------------------------------------------------------

Running it produces a -very- long stack trace (I won't reproduce
the entire thing unless I need to) on the transform() method
call. The topmost line of the stack trace is:

javax.xml.transform.TransformerConfigurationException:
javax.xml.transform.TransformerConfigurationException:
javax.xml.transform.TransformerException:
javax.xml.transform.TransformerException: style*** requires attribute:
version

The stack trace repeats the "style*** requires attribute:
version" several times. I've stepped through my program in
the debugger, and to my novice eye it LOOKS like everything
is set up correctly. I'm sure I'm missing something obvious,
or forgetting to do something obvious, but I certainly don't
see it.


Quantcast