You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by Martin Klang <ma...@pingdynasty.com> on 2000/08/03 20:05:06 UTC

DOM ResultTarget

Hi all!

I'm building a system where xml gets passed around and processed as dom
trees. For XSLT processing, i sometimes want to process a dom Node and
have the results in a separate dom Node, so i tried something like:

	XSLTInputSource xml = new XSLTInputSource(in);
	XSLTResultTarget target = new XSLTResultTarget(out);
	StylesheetRoot preprocessed = 
	    (StylesheetRoot)stylesheetCache.get(xslId);
	preprocessed.process(xml, target);

where 'in' is a document and 'out' is a (Xerces) Node object. Now, whether
or not i create an Element/DocumentFragment/whatever with
in.createXXX(), or create a brand new document for the 'out' Node, i still
get DOM005 : Wrong Document errors when Xalan tries to insertChild() :<

am i doing something wrong (probably!) or is it a problem with Xalan?

btw this is using Xalan-J-1.0.1, as i'm stuck with the cocoon version ;)

also, at a point where i wanted to do a two-stage xslt transform, as
a workaround i created a StringWriter for the first XSLTResultTarget, got
the string from it to make a StringReader to create the second 
XSLTInputSource, and it failed miserably... Xerces was complaining that
there was no root element (?!). So i took the resulting string from the
first transform, created a XercesParser and could successfully parse it
and pass it on to the next transform ->
	StringWriter buffer = new StringWriter();
	processor.process(doc, stylesheet1, buffer);
	org.xml.sax.InputSource xml = 
	    new org.xml.sax.InputSource(new
		StringReader(buffer.toString()));
	org.apache.xerces.parsers.DOMParser parser =
	    new org.apache.xerces.parsers.DOMParser();
	parser.parse(xml);
	Node result = parser.getDocument().getDocumentElement();
	processor.process(result, stylesheet2, target);

i've not had time yet to look at the src to see what Xalan does
differently when i pass in an input Reader.
i'll get the exact error messages/stack trace if anyone's interested.

any ideas why it failed when using a StringReader XSLTInputSource??


and thanks to everyone working on the project - xalan rocks!

regards,

/martin


Re: DOM ResultTarget

Posted by Gary L Peskin <ga...@firstech.com>.
Martin --

As for your first problem, it looks like it was fixed after the version
of Xalan that you're using (in version 1.36 thanks to bk@viae.de).  It
looks like if out is a Document Node, then you should be okay.  So, this
is a problem with the version of Xalan you are using.

With regard to your second problem, I have no idea why this doesn't
work.  I'll try to create a small test case and see if I can reproduce
the problem in the next few days.  It doesn't seem like you should have
to go through all of this.  Also, if you have the exact error message,
that might help.

Gary

Martin Klang wrote:
> 
> Hi all!
> 
> I'm building a system where xml gets passed around and processed as dom
> trees. For XSLT processing, i sometimes want to process a dom Node and
> have the results in a separate dom Node, so i tried something like:
> 
>         XSLTInputSource xml = new XSLTInputSource(in);
>         XSLTResultTarget target = new XSLTResultTarget(out);
>         StylesheetRoot preprocessed =
>             (StylesheetRoot)stylesheetCache.get(xslId);
>         preprocessed.process(xml, target);
> 
> where 'in' is a document and 'out' is a (Xerces) Node object. Now, whether
> or not i create an Element/DocumentFragment/whatever with
> in.createXXX(), or create a brand new document for the 'out' Node, i still
> get DOM005 : Wrong Document errors when Xalan tries to insertChild() :<
> 
> am i doing something wrong (probably!) or is it a problem with Xalan?
> 
> btw this is using Xalan-J-1.0.1, as i'm stuck with the cocoon version ;)
> 
> also, at a point where i wanted to do a two-stage xslt transform, as
> a workaround i created a StringWriter for the first XSLTResultTarget, got
> the string from it to make a StringReader to create the second
> XSLTInputSource, and it failed miserably... Xerces was complaining that
> there was no root element (?!). So i took the resulting string from the
> first transform, created a XercesParser and could successfully parse it
> and pass it on to the next transform ->
>         StringWriter buffer = new StringWriter();
>         processor.process(doc, stylesheet1, buffer);
>         org.xml.sax.InputSource xml =
>             new org.xml.sax.InputSource(new
>                 StringReader(buffer.toString()));
>         org.apache.xerces.parsers.DOMParser parser =
>             new org.apache.xerces.parsers.DOMParser();
>         parser.parse(xml);
>         Node result = parser.getDocument().getDocumentElement();
>         processor.process(result, stylesheet2, target);
> 
> i've not had time yet to look at the src to see what Xalan does
> differently when i pass in an input Reader.
> i'll get the exact error messages/stack trace if anyone's interested.
> 
> any ideas why it failed when using a StringReader XSLTInputSource??
> 
> and thanks to everyone working on the project - xalan rocks!
> 
> regards,
> 
> /martin