XSLT has a very powerful and cool feature that doesn’t seem to be that well known: The ability to import other stylesheets into the stylesheet you’re working on. Why is this a big deal, you ask? Mainly because it presents an elegant solution to an oft seen problem: How to create a slight variation on an existing mapping program without copying the entire program and modifying the copy.
What was the problem again?
XSLT to the rescue
Adding the xsl:import magic
element, we can import another stylesheet into the stylesheet we’re working on. The true power of this feature lies in the fact that templates in the imported stylesheet can be overriden by templates in the importing stylesheet. Here’s a complete stylesheet that implements the customer’s request:, imports the general stylesheet and implements only the single template that’s needed to accommodate the customer’s request. The same result could have been achieved by making a physical copy of the general stylesheet and modifying the
template. That would lead to potentially massive amounts of duplicated code, though. The proposed solution contains no duplicated code whatsoever. The specialized stylesheet is also very compact and it’s immediately obvious how it extends the general stylesheet. Furthermore, bug fixes and improvements in the general stylesheet will automatically trickle down to the specialized stylesheet.
element, please refer to the “Stylesheet Import” section of the W3C XSLT 1.0 recommendation.</p>