For Programmers: Free Programming Magazines  


Home > Archive > Java Help > March 2004 > Hello World for XSLT









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Hello World for XSLT
Larry Coon

2004-03-30, 10:49 am

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:stylesheet 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:stylesheet>

---------------------------------------------------------
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: stylesheet requires attribute:
version


The stack trace repeats the "stylesheet 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.
Kristoffel

2004-03-30, 11:49 am

Larry Coon wrote:
> 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):
>
> ----------------------------------------------------------
> Here is my xsl file:
>
> <?xml version="1.0"?>
> <!-- hello.xsl -->
>
> <xsl:stylesheet version = "1.0"
> xmlns:xsl = "http://w3.org/1999/XSL/Transform">


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


*www*.w3.org (don't ask me why)

>
> <xsl:template match = "hello">
> <html>
> <body><xsl:value-of select = "message"/></body>
> </html>
> </xsl:template>
> </xsl:stylesheet>
>
> ---------------------------------------------------------
> 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: stylesheet requires attribute:
> version
>
>
> The stack trace repeats the "stylesheet 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.

Larry Coon

2004-03-30, 9:37 pm

Kristoffel wrote:

> *www*.w3.org (don't ask me why)


*sigh* I can't tell you how many times I stared at that URL
and didn't spot that.

Thanks! It works correctly now.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com