|
From: David B. <viv...@gm...> - 2005-02-18 20:23:00
|
I do like that idea, but let me mention a few downsides. One place where it might not be as flexible is in specifying where the resource is. For example, you couldn't do: <?xml-stylesheet href="classpath:/transform.xsl" type="text/xml"?> If we plugged in a new javax.xml.transform.URIResolver, we might be able to still resolve paths using Spring specific URI prefixes. Along these same lines, it seems like it would be more desirable to specify an xsl transform outside the XML file. If I'm using the same xml context file for both production and for a unit test, I can easily specify the same file using different paths. For example, in a running web app, the path would be /WEB-INF/appContext.xml, but if I'm running a UnitTest using FileSystemXMLApplicationContext, I might want to specify an absolute path to create my context. By this same token, it would be nice to be able to specify the location of the XSL transform from outside the spring context file itself. So my preference would be a syntax like this: transform:/(customContext.xml, customTransform.xsl). We could recurse through the "parameters" to the URI-type specification, so if someone .really wanted to hang themselves, they could put a file through multiple transforms, using sources from the classpath or normal URL. Maybe support of inline xsl should be supported in addition. /David Bowers On Fri, 18 Feb 2005 18:48:46 +0200, Juha Komulainen <juh...@gm...> wrote: > Actually the XML-file itself can specify the used XSL-stylesheet by > having a processing instruction like: > > <?xml-stylesheet href="transform.xsl" type="text/xml"?> > > (See http://www.w3.org/TR/xml-stylesheet/ for details.) > > Unfortunately, by default XML-parsers (or at least Xerces) won't try > to apply the specified XSLT-stylesheets when parsing. Still, it's easy > enough by loading the document with a transformer: > > Source source = new StreamSource(file); > TransformerFactory tf = TransformerFactory.newInstance(); > > // Extract the stylesheet from document, if there is one > Source stylesheet = tf.getAssociatedStylesheet(source, null, null, null); > > // If document had stylesheet, use it. Otherwise use identity transform. > Transformer transformer = stylesheet != null > ? tf.newTransformer(stylesheet) > : tf.newTransformer(); > > DOMResult result = new DOMResult(); > transformer.transform(source, result); > Document document = (Document) result.getNode(); > > So if Spring would load the context-files using this strategy it would > be possible to get XSL-transforms for free. > > Are there any problems with this solution? |