Reputation: 12729
How does one import a style-sheet, applying an actual parameter value to the called style-sheet? Here is an illustration.
Let's say I have a general purpose style-sheet which takes a parameter "x". It looks like this, and is located at "general.xslt".
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:param name="x" as="xs:string" />
... style-sheet content ...
</xsl:stylesheet>
I have a higher level style-sheet (specific.xslt) which wants to incorporate the functionality of general.xslt by importation. This higher level style-sheet (specific.xslt) takes a parameter "y". The higher level style-sheet needs to import general.xslt applying an actual parameter, being some function of y, to the formal parameter x. If this were legal XSLT 2.0 syntax, it would read some-thing like this:
higher level style-sheet:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:param name="y" as="xs:string" />
<xsl:import href="general.xslt">
<xsl:with-param name="x" select="some-function($y)" />
</xsl:import>
<xsl:function name="some-function" as="xs:string">
<xsl:param name="value" as="xs:string" />
... content goes here ...
</xsl:function>
... more content ...
</xsl:stylesheet>
Of course the above is not legal syntax, but it illustrates what I want to achieve - invoke style-sheets with actual parameters in a similar way to to invoking templates with parameters. Is this possible in any version of XSLT?
Illustration of Michael Kay's Answer
general.xslt: This low-level style-sheet takes a parameter. The formal parameter is x.
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="2.0"
exclude-result-prefixes="xsl xs fn">
<xsl:param name="x" as="xs:string" />
<xsl:template match="/">
<root>
The value of x is <xsl:value-of select="$x" />
</root>
</xsl:template>
</xsl:stylesheet>
specific.xslt: This high-level style-sheet takes a parameter. The formal parameter is y.
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:my="http://my.com"
version="2.0"
exclude-result-prefixes="xsl xs fn my">
<xsl:import href="general.xslt" />
<xsl:param name="y" as="xs:string" />
<xsl:function name="my:some-function" as="xs:string">
<xsl:param name="value" as="xs:string" />
<xsl:value-of select="concat( $value, '!') " />
</xsl:function>
<xsl:variable name="x" select="my:some-function($y)" />
<xsl:template match="/">
<xsl:apply-imports/>
</xsl:template>
</xsl:stylesheet>
Command line invocation of Saxon:
Transform.exe -s:specific.xslt -xsl:specific.xslt -o:specific-out.xml y=abc
Output:
<?xml version="1.0" encoding="UTF-8"?><root>The value of x is abc!</root>
The actual parameter of general.xslt is 'abc!'
Upvotes: 0
Views: 1363
Reputation: 163468
You can override the xsl:param with an xsl:param or xsl:variable (appearing at the top level, i.e a child of xsl:stylesheet in the importing module).
<xsl:variable name="x" select="some-function($y)" />
Upvotes: 1