RE: [xsl] Java XSLT transformers and document('') problem

Subject: RE: [xsl] Java XSLT transformers and document('') problem
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Mon, 8 Sep 2008 09:36:23 +0100
You need to understand what document('') actually does. It treats '' as a
relative URI and resolves it against the base URI of the element containing
the call to the document() function. Normally if your stylesheet is read
from a file on disk, this will re-read that file from disk and return its
parsed representation. (In some processors it's possible that this will be
optimized - if the processor still has the original XML of the stylesheet
available at run-time, then it might not need to re-read it).

If your stylesheet wasn't read from disk, then this isn't going to work. The
only thing I can suggest is to supply a URIResolver which intercepts the
document('') request and returns whatever you want it to return.

Note that with XSLT 2.0 there is almost never a need to use document('') and
it should generally (in my view) be avoided. Even with 1.0, it is often used
to read "lookup data", and in my view it's better to put such lookup data in
a separate document rather than putting it in the stylesheet.

Michael Kay
http://www.saxonica.com/ 

> -----Original Message-----
> From: LS [mailto:leonid@xxxxxxxxxxxxx] 
> Sent: 08 September 2008 04:11
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Java XSLT transformers and document('') problem
> 
> I have strange behavior with XSLT and Java (tested on Xalan 
> and Saxon):
> 
> when I create transformer from XSLT file, function 
> document('') works as I expect:
> 
> document('')/* -> xsl:template
> count(document('')/*) -> 1
> 
> when I create transformer from Domsource, modify it (add some 
> templates, remove others) - something breaks function document(''):
> 
> document('')/* -> ?
> count(document('')/*) -> 0
> 
> all works well when I write modified XSL to file and create 
> transformer from that file but dumping modified XSL to string 
>  and create transformer from Streamsourse again works with errors
> 
> Is there any way to create transformation on-the fly and use 
> it without temporary dumping it to file?
> My question depends only on document('') function, because 
> all of the others seems like ok

Current Thread