Reputation: 2797
I've got 4 almost identical stylesheets (in XSLT 1.0), that are copy pasted, so 2 of them would look like this.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<xsl:variable name="foo" select="input[@name = 'foo1']"/>
<xsl:template match="/">
<foo>
<xsl:value-of select="$foo"/>
</foo>
</xsl:template>
</xsl:stylesheet>
and another
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<xsl:variable name="foo" select="input[@name = 'foo2']"/>
<xsl:template match="/">
<foo>
<xsl:value-of select="$foo"/>
</foo>
</xsl:template>
</xsl:stylesheet>
as you see the only difference is the definition of the variable foo (and in reality the xslt itself is much much more complex).
whats the best way to just define the xslt once, and have 4 wrappers that simply decorate this shared one with a different definition of the variable.
There seems to be at least 3 options, include, import and then XML option XInclude etc?
Is there an idiomatic way to do this? (I assume this is a pretty common requirement).
Upvotes: 0
Views: 106
Reputation: 163468
The basic approach is to put the common code in a shared module that's imported by all the 4 wrappers.
In the shared module you can either declare the variable with a dummy value, or not declare it at all.
In the wrapper module, you just need two declarations:
<xsl:import href="common.xsl"/>
<xsl:variable name="foo" select="input[@name = 'foo2']"/>
With XSLT 3.0 you can get a lot more elaborate using packages, in which declarations can be annotated as private, abstract, final etc.
Upvotes: 2