GNOME Bugzilla – Bug 655156
document('') fails to return stylesheets parsed from memory
Last modified: 2012-08-16 13:25:55 UTC
Created attachment 192512 [details] Program demonstrating the issue If an XSL stylesheet is from memory (e.g. xmlParseMemory), then xpath expressions referencing "document('')" will never return a match. Attached is a program that shows the problem using the libxslt/tests/general/array.xsl testcase. It's read via xsltParseStylesheetFile and applied (successfully) and then read again via xmlParseMemory/xsltParseStylesheetDoc, and the two document('') expressions fail to match.
Created attachment 192515 [details] [review] Patch for function.c This was originally addressed in 2001: http://mail.gnome.org/archives/xslt/2001-July/msg00112.html style->doc->URL is NULL when XSL is read from memory, and the change allowed it to match a NULL URI. That change in xsltDocumentFunctionLoadDocument was no longer being hit. xsltDocumentFunction doesn't call the former if URI=NULL, and the former has a check to make sure URI isn't NULL. This patch has xsltDocumentFunction check for style->doc->URL=NULL before bailing due to NULL URI. Figured this was easier/cleaner than trying to propagate NULL URI down through Load again.
Just found that this was discussed in 2002, and the exact behavior implemented in the patch was deemed "not a good idea" by Daniel Veillard. :) http://mail.gnome.org/archives/xslt/2002-July/msg00030.html http://mail.gnome.org/archives/xslt/2002-June/msg00060.html It was recommended to instead use xmlNodeSetBase() to give the doc a non-NULL base. When getting down into it, I understand the logic "document() is resolved relative to the XSL's location, reading from memory has no location, therefore buzz off", but the XSL spec seems to address this: http://www.w3.org/TR/xslt#document "Note that a zero-length URI reference is a reference to the document relative to which the URI reference is being resolved; thus document("") refers to the root node of the stylesheet; the tree representation of the stylesheet is exactly the same as if the XML document containing the stylesheet was the initial source document." The fact that the behavior differs in this case between fromFile & fromMemory definitely caught me off guard, and IMHO fixing that scenario is worth the one-off-ness of the fix.
Okay, based on the spec comment, I agreed, though I still think it's kind of an abuse :) Applied: http://git.gnome.org/browse/libxslt/commit/?id=5e184e7154e8aa466e988f17c8b97b65196f94bf thanks a lot :-) Daniel