You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xalan.apache.org by Daniel Jian <D....@tiscon.com> on 2002/06/27 14:19:15 UTC

transform a dom tree using xalan

I have a self generated org.w3c.dom.Document in memory and i try to transform
it into html using xsl.

DOMImplementation domImplementation =
DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementatio
n();
DocumentType documentType = domImplementation.createDocumentType(
DOCUMENT_ELEMENT_NAME, "", dtdFileUrl );
Document document = domImplementation.createDocument( "",
DOCUMENT_ELEMENT_NAME, documentType );

// ... construct the document according to the dtd ...
document.getDocumentElement().appendChild( ... ); and so on

transformer = TransformerFactory.newInstance().newTransformer( new
StreamSource( xslFileUrl ) );

Writer stringWriter = new StringWriter();
transformer.transform( new DOMSource( document ), new StreamResult(
stringWriter ) );

All nice and dandy but now comes the problem :( 
the only xsl:template that is matched is the root template ( <xsl:template
match="/"> ) all the other elements are not found.

Naturaly I think that there is a mistake in the creation of the xml file so I
print the document into a file. Surprise surprise the xml file is perfect.
Then I set the xsl declaration into the xml file and let internet explorer do
the transformation for me, works fine.
Next step: (Maybe I'm not setting all parameters to the transformer ??):

transformer.setOutputProperty( OutputKeys.INDENT, "yes" );
transformer.setOutputProperty( OutputKeys.METHOD, "html" );
transformer.setOutputProperty( OutputKeys.OMIT_XML_DECLARATION, "yes" );
transformer.setOutputProperty( OutputKeys.DOCTYPE_SYSTEM, dtdFileUrl );

No improvement.

Next step: ( Why is the first element - document element - not found? ) try
to change the last line of code into:

transformer.transform( new DOMSource( document.getDocumentElement() ), new
StreamResult( stringWriter ) );

Now all the targets (xsl:template) that match the children of the document
element are executed. The root target ( <xsl:template match="/"> ) and the
document element target ( <xsl:template match="ReverseLogistics"> ) are not
applied. By the way for this try the xsl document is:

....

<xsl:template mathc="/">
	<xsl:apply-templates select="ReverseLogistics"/>
</xsl:template>

<xsl:template match="ReverseLogistics">
 	...
</xsl:template>

...

Last step: (i'm tired and angry)  Print the dom tree into an output stream,
transform the output stream into an input stream, use the StreamSource. Gues
what ... now it's working.

So now it works but this is a workaround not a solution. If i have the dom
tree, I'm serializing it into a stream and then parse it again to optain a
new dom tree and then transform it via xsl. There must be an elegant way to
do it using the DOMSource.

Please help me, 
Regards
----------------------
Daniel Jian
Software Engineer

tiscon AG Infosystems

edisoncenter
Edisonallee 1-3
89231 Neu-Ulm
Germany

Fon: +49 (731) 9 54 48 - 3 54
Fax: +49 (731) 9 54 48 - 9 00