Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle duplicate imports in XSLT?

A warning is thrown whenever the same file is imported multiple times in an XSL transformation. Usually something along the lines of Stylesheet module file:/Users/blake/Documents/workspace/course-connect-parent/course-connect-publisher/src/main/xsl/config.xsl is included or imported more than once. This is permitted, but may lead to errors or unexpected behavior

However, it seems that if the same xsl needs to be imported by multiple 'children' xsl calls (say, a util.xsl containing important functions), this situation is unavoidable.

Is there a way to avoid these warnings, or a better way to do things?

Here is the problem setup:

FILE_A.xsl

imports FILE_B.xsl and FILE_C.xsl
uses functions/parameters from util.xsl, which it gets from FILE_B.xsl & FILEC.xsl
calls templates in FILE_B.xsl
calls templates in FILE_C.xsl

FILE_B.xsl

imports util.xsl
contains templates used by FILE_A.xsl
uses functions/parameters from util.xsl

FILE_C.xsl

imports util.xsl
contains templates used by FILE_A.xsl
uses functions/parameters from util.xsl

util.xsl

Contains functions/variables used by FILE_A, FILE_B, FILE_C
like image 365
Riplikash Avatar asked Apr 10 '12 20:04

Riplikash


2 Answers

These messages are produced by recent releases of Saxon. It's not an error to import the same file more than once, but in some processors (including earlier releases of Saxon) it's very inefficient; it's also unnecessary, and it can make code very difficult to debug. Basically it means that you have multiple instances of the same template rules present with different precedences.

In XSLT, unlike other languages, a module doesn't need to have an import for everything it depends on. You only need one import anywhere in the stylesheet. It's true enough that if A.xsl uses util.xsl and you want to reuse A.xsl in lots of places, then it can be convenient if A.xsl declares its dependency using an import or include declaration, which means that if B.xsl also uses util.xsl you will end up with this situation of multiple imports.

I added the messages because of two incidents: one a user who was getting completely bizarre effects from xsl:next-match, the other a user who was getting pathologically bad compile-time performance because one module was imported combinatorially by about 100,000 different routes and therefore at about 100,000 different import precedences. There's always a tendency when a problem like this arises to try to ensure that it never happens again, and the warnings were a response to that, but like health and safety warnings it's hard to know when the balance is right. You can always ignore a warning if everything is working OK, and if you really want you can always suppress it by filtering messages in your ErrorListener.

like image 176
Michael Kay Avatar answered Nov 15 '22 09:11

Michael Kay


If you only use the stylesheets in that collection, i.e. never use FILE_C.xsl as a self-standing top level stylesheet, you can simply take the import of utils.xsl out, the imports have global scope so the templates in FILE_C.xsl will "see" the templates from utils.xsl even if that is imported higher up the import tree. The utils.xsl will only be imported once and the warning will go.

Alternatively you can leave things as they are, it is not an error and the system shouldn't really be bothering you with non-errors. Which XSLT system is that? perhaps it has an option to be less aggressive in its warnings?

like image 32
David Carlisle Avatar answered Nov 15 '22 10:11

David Carlisle