aspose file tools*
The moose likes XML and Related Technologies and the fly likes Simple XSL transformation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Simple XSL transformation" Watch "Simple XSL transformation" New topic
Author

Simple XSL transformation

Jason Hoskins
Greenhorn

Joined: Oct 30, 2003
Posts: 5
Hi Guys,
I'm new to XSLT, and I'm trying to do a very simple transformation using the Xerces DOMParser and the Xalan XSL engine. However I can't get it to work.
Here is what my xml doc looks like (slightly trimmed):
<?xml version="1.0" encoding="UTF-8"?>
<storydetail>
<title>Network Status: Important notice</title>
<byline>Last Update</byline>
<date>10/28/03 5:10 AM</date>
<body>Various text in here</body>
</storydetail>
My xsl stylesheet is extremely simple:
<?xml version='1.0' encoding='ISO-8859-1'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl utput method="html" media-type="text/html" encoding="ISO-8859-1"/>
<xsl:template match="storydetail">
<br />
<b><xsl:value-of select="title"/></b><br />
<p><xsl:apply-templates select="body"/></p>
</xsl:template>
<xsl:template match="htmltag" mode="hidename">
<xsl:text disable-output-escaping="yes"><</xsl:text>
<xsl:value-of select="@htmlname"/>
<xsl:value-of select="@htmlattrs"/>
<xsl:text disable-output-escaping="yes">></xsl:text>
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="image">
<img src="{.}" align="right" vspace="10" hspace="8"/>
<br clear="all"/>
</xsl:template>
<xsl:template match="a[@href]">
<a class="textred" href="javascript:launch('{@href}')">
<xsl:value-of select="."/>
</a>
</xsl:template>
<xsl:template match="@* | * | text()">
<xsl:copy>
<xsl:apply-templates select="@* | * | text()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
The XML document basically sits at a URI on a separate web server. I'm just trying to build the Document object using the DOMParser, and feed the entire Document into XSL.
Here is the guts of my code (stripped down for space):

String strBulletinUrl = "http:www.site.com";
DocumentBuilder objDB = null;
DocumentBuilderFactory objDBF = null;
org.jdom.input.DOMBuilder objDOMBuilder = null;
objDBF = DocumentBuilderFactory.newInstance();
objDB = objDBF.newDocumentBuilder();
objDocument = objDB.parse(strBulletinUrl);
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(new StreamSource(new File("stylesheet.xsl")) );
DOMSource xmlSource = new DOMSource(objDocument);
DOMResult xmlResult = new DOMResult();
transformer.transform(xmlSource, xmlResult);
Seems simple enough but it keeps bombing on me big time . I keep getting the following error:
javax.xml.transform.TransformerException: XSL-1103: (Fatal Error) DOMResult can not be this kind of node.

Just a little background info. I'm trying to run this from a jsp page and I have all the proper jar files installed. Any help would be appreciated.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Try using org.jdom.transform.JDOMSource and org.jdom.transform.JDOMResult instead of the standard DOMSource and DOMResult.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Jason Hoskins
Greenhorn

Joined: Oct 30, 2003
Posts: 5
Thanks LAsse...are the xalan libs not able to perform this ??
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
The problem was that you were using a mix JDOM objects and "standard" DOM objects.
Jason Hoskins
Greenhorn

Joined: Oct 30, 2003
Posts: 5
Hmm...I did instatiate the jdom DOMBuilder class but I never actually called any of its methods.
Right now I have left the code mentioned above and went straight with an JDOM solution...which still does not work.
Here is what I have:
//Build XML Document:
SAXBuilder builder = new SAXBuilder();
org.jdom.Document doc = builder.build("c:/source.xml");
//Convert with JDOM using Xalan
Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(new File("c:/stylesheet.xsl")));
// Run the transformation
JDOMSource source = new JDOMSource(doc);
JDOMResult result = new JDOMResult();
transformer.transform(source, result);
//output the result into a string (for now):
Document doc2 = result.getDocument();
XMLOutputter outp = new XMLOutputter();
outp.setOmitDeclaration(true);
outp.setTextNormalize(true);
outp.setIndent(" ");
outp.setNewlines(true);
String test = outp.outputString(doc2);
Of course this doesn't work. It throws this exception : a java.lang.IllegalStateException: Root element not set
From reading the documentation, it should just be a matter of feeding your XML document into your xslt engine. The results will go in your result Document, and you just write it out using the XMLOutputer class. However, for some reason my result Document will not print.
Anyone have any ideas ??
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
I'm really surprised to realize that the XSLTransform.java sample in jdom-b9 distribution doesn't work (the same "Root element not set" exception).
Jason Hoskins
Greenhorn

Joined: Oct 30, 2003
Posts: 5
I guess the next question is....is there any easy way to use xalan to convert a w3c document into html ?? Every example i have seen on the net have been transforming xml files with Sreamsource.
I can create the exact w3c Document I want to convert. But when I feed the Document into the transformer class it bombs (code below):
DOMSource xmlSource = new DOMSource(objDocument);
DOMResult xmlResult = new DOMResult();
transformer.transform(xmlSource, xmlResult);
My questions is why does the Xalan convert XML docs fine as Streamsource (which I can't use) and not DOMSource ?? Even if the content of the document I supply to the DOMSource is exactly the same ??
The apache javadoc is pretty sparse.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Do you get the same error as before in this thread? I've used the DOMSource for transformation without problems. Check if you can do this without errors.
 
 
subject: Simple XSL transformation
 
Similar Threads
XML to Text with XSLT
WSAD 5 XML/XSL Validator
Converting XML into legacy record layout
Generating xsl to convert Xml into text format using transformation engine.
Webservices in java