This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: Can input xml and stylesheet be passed as strings to
- To: xsl-list at mulberrytech dot com
- Subject: Re: Can input xml and stylesheet be passed as strings to
- From: Dimitre Novatchev <dnovatchev at yahoo dot com>
- Date: Mon, 31 Jul 2000 15:05:28 -0700 (PDT)
- Reply-To: xsl-list at mulberrytech dot com
From: David_Marston@lotus.com
Subject: Re: Can input xml and stylesheet be passed as strings to
Xalan?
David Marston writes:
>Dmitre Novatchev writes:
>>I am trying to call the methods of some key Xalan's classes in such
>>a way as to be able to pass the xml input and the xsl stylesheet as
>>strings.
>
>On the page
>http://xml.apache.org/xalan/usagepatterns.html
>it says that the XSLTInputSource can use a character stream (of the
>java.io.Reader class). Does that meet your needs?
>.................David Marston
Thanks David,
Yes they say this, but the only example, which is given and really
works is for the output stream.
Here is my code -- it seems a little long, but the important code is in
the doTransform() method(at the end):
------------------------------------
import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.lang.reflect.Constructor;
import org.apache.xalan.xslt.*;
import org.apache.xalan.xpath.xml.*;
import org.apache.xalan.xslt.res.XSLTErrorResources;
// import org.apache.xalan.xpath.xml.xml4j.*;
import org.w3c.dom.*;
public class myStartingXSLTProcessor
{
/**
* The XML Parser Liaison.
*/
XMLParserLiaison m_liaison = null;
XSLTProcessor m_processor = null;
/**
* @serial
*/
private String m_styleURL;
/**
* @serial
*/
private String m_documentURL;
// Parameter names. To change a name of a parameter, you need only
make
// a single change. Simply modify the value of the parameter string
below.
//--------------------------------------------------------------------------
/**
* @serial
*/
private final String PARAM_styleURL = "styleURL";
/**
* @serial
*/
private final String PARAM_documentURL = "documentURL";
/**
* @serial
*/
private final String PARAM_parser = "parser";
/**
* @serial
*/
private String whichParser = null;
// We'll keep the DOM trees around, so tell which trees
// are cached.
/**
* @serial
*/
private String m_styleURLOfCached = null;
/**
* @serial
*/
private String m_documentURLOfCached = null;
/**
* I save this for use on the worker thread, but i don't think I
* need to do this.
* @serial
*/
private URL m_codeBase = null;
private URL m_documentBase = null;
private Document m_docTree = null;
private Document m_styleTree = null;
/**
* The XSLTProcessorApplet constructor takes no arguments.
*/
public myStartingXSLTProcessor()
{
init();
}
public void init()
{
initLiaison();
}
/**
* Try to init the XML liaison object: currently not implemented.
*/
protected void initLiaison()
{
//String parserName = (whichParser.equals("ANY")) ?
Constants.LIAISON_CLASS : whichParser ;
String parserName = Constants.LIAISON_CLASS;
try
{
Class parserLiaisonClass = Class.forName(parserName);
Constructor parserLiaisonCtor =
parserLiaisonClass.getConstructor(null);
m_liaison =
(XMLParserLiaison)parserLiaisonCtor.newInstance(null);
}
catch(Exception e)
{
e.printStackTrace();
//System.err.println(XSLMessages.createMessage(XSLTErrorResources.ER_COULD_NOT_CREATE_XML_PROC_LIAISON,
new Object[] {parserLiaisonClassName})); //"Could not create XML
Processor Liaison: "+parserLiaisonClassName);
return;
}
try
{
m_processor = XSLTProcessorFactory.getProcessor();
m_processor.setDiagnosticsOutput(System.out);
}
catch(org.xml.sax.SAXException se)
{
se.printStackTrace();
throw new RuntimeException(se.getMessage());
}
}
public String doTransform(String inputXML, String styleSheet)
{
StringWriter osw = new StringWriter();
PrintWriter pw = new PrintWriter(osw, false);
try
{
char[] xslbuf = styleSheet.toCharArray();
CharArrayReader carStyle = new CharArrayReader(xslbuf);
char[] xmlbuf = inputXML.toCharArray();
CharArrayReader carInput = new CharArrayReader(xslbuf);
{
XSLTInputSource xmlSource = new XSLTInputSource(carInput);
Node xmlNode = m_processor.getSourceTreeFromInput(xmlSource);
XSLTInputSource xslSource = new XSLTInputSource(carStyle);
Node xslNode = m_processor.getSourceTreeFromInput(xslSource);
// Node type should always be Node.DOCUMENT_NODE (9)
if (xmlNode.getNodeType()== Node.DOCUMENT_NODE)
m_docTree = (Document)xmlNode;
else
System.out.println("XML source document node type is " +
xmlNode.getNodeType());
if (xslNode.getNodeType()== Node.DOCUMENT_NODE)
m_styleTree = (Document)xslNode;
else
System.out.println("Stylesheet source document node type is "
+ xslNode.getNodeType());
System.out.println("Stylesheet is: " + '"' + styleSheet + '"');
StylesheetRoot stylesheet = null;
stylesheet = m_processor.processStylesheet(xslSource);
if(null != stylesheet)
{
Node sourceTree =
m_processor.getSourceTreeFromInput(xmlSource);
stylesheet.process(m_processor, sourceTree, new
XSLTResultTarget(pw));
}
}
catch(Exception e)
{
//this.showStatus("Could not prime the pump!");
System.out.println("Could not prime the pump!");
e.printStackTrace();
}
return osw.toString();
}
}
------------------------------------------------
I'm getting exactly the same exceptions as reported in my first
posting:
Input XSL; Line 1; Column 1
org.apache.xalan.xslt.XSLProcessorException: The root element is
required in a well-formed document.
at
org.apache.xalan.xslt.XSLTEngineImpl.error(XSLTEngineImpl.java:1630)
at
org.apache.xalan.xslt.XSLTEngineImpl.processStylesheet(XSLTEngineImpl
.java:722)
at
myStartingXSLTProcessor.doTransform(myStartingXSLTProcessor.java:241)
Once again, I would greatly appreciate any help, advice, hints.
I'd love to hear from someone who has actually made Xalan accept its
input xml and stylesheet in strings.
Dimitre Novatchev.
__________________________________________________
Do You Yahoo!?
Kick off your party with Yahoo! Invites.
http://invites.yahoo.com/
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list