Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2004

Tip: Looking for answers? Try searching our database.

Hello World for XSLT

Thread view: 
Larry Coon - 30 Mar 2004 16:01 GMT
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 - 30 Mar 2004 16:56 GMT
> I'm trying to do the equivalent of a "Hello, world"
> program for XSLT, and it's failing.  I have Sun's
[quoted text clipped - 13 lines]
> <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>
[quoted text clipped - 31 lines]
> or forgetting to do something obvious, but I certainly don't
> see it.
Larry Coon - 31 Mar 2004 02:36 GMT
> *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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.