Hi,
I am having trouble with Xalan in Java. It doesn't seem to transform XML properly in an XSLT transformation. The only slightly weird thing I am doing is I want to have both the XML and XSLT as strings to begin with (not as files) and have the result be returned as a string. It seems like all the examples I have seen in the Xalan docs have the xml and xsl being read from a file, which is not what I want to do.
Following is an XML and XSLT, with the expected result, and then the actual:
XML (this is passed to the method below): <xml>foo</xml>
XSLT: <xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'> <xsl:template match='/'> <root>This should work.</root> </xsl:template> </xsl:stylesheet>
EXPECTED: <root>This should work.</root>
ACTUAL: foo
What is going on here? when I do the transformation from with XMLSpy, it works as expected.
There must be something wrong with what I am doing in the method below - any help is appreciated! Here's the whole java file, for completeness. only one class, one method.
// Imported TraX classes import javax.xml.transform.TransformerFactory; import javax.xml.transform.Transformer; import javax.xml.transform.stream.StreamSource; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.dom.DOMResult;
// Imported java.io classes import java.io.StringWriter; import java.io.StringReader;
// Imported SAX classes import org.xml.sax.InputSource; import org.xml.sax.SAXException;
// Imported DOM classes import org.w3c.dom.Document; import org.w3c.dom.Node;
// Imported JAVA API for XML Parsing classes import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException;
public class Translator { // xXML contains the xml to be transformed (a string) // sLang isn't used yet... public String Translate(String sXML, String sLang) throws Exception, SAXException, TransformerException, TransformerConfigurationException,ParserConfigurationException { String sXSL = "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'><xsl:template match='/xml'><root>This should work.</root></xsl:template></xsl:stylesheet>";
// convert the XML passed in the sXML parameter to a Document DocumentBuilderFactory oFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder oBuilder = oFactory.newDocumentBuilder(); InputSource oInputSource = new InputSource(new StringReader(sXML)); Document oXMLDOC = oBuilder.parse(oInputSource);
// create a DOMsource for the xml... DOMSource oDOMSource = new DOMSource(oXMLDOC.getDocumentElement());
// create a StreamSource for the xsl... StreamSource oXSLSource = new StreamSource(new StringReader(sXSL));
// create a StreamResult to catch the output of the transformation... StringWriter oStringWriter = new StringWriter(); StreamResult oStreamResult = new StreamResult(oStringWriter);
// do the transformation and return...
TransformerFactory oTransFact = TransformerFactory.newInstance();
Transformer oTrans = oTransFact.newTransformer(oXSLSource);
oTrans.setOutputProperty("omit-xml-declaration", "yes");
oTrans.transform(oDOMSource, oStreamResult);return oStringWriter.toString(); } }
Thanks so much for any help you can offer!
-Jesse
_________________________________________________________________
Join the world�s largest e-mail service with MSN Hotmail. http://www.hotmail.com
