Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT stylesheet parameters in imported stylesheets

Is it possible to assign a value to a parameter of an imported stylesheet?

I was expecting something like

<xsl:import ... >
  <xsl:with-param ... 
</xsl:import>

but that is not allowed.

Also tunnel="yes" is forbidden in stylesheet parameters.

like image 530
j_maly Avatar asked Jan 19 '12 18:01

j_maly


People also ask

How do you use parameters in XSLT?

To use an XSLT parameterCreate an XsltArgumentList object and add the parameter using the AddParam method. Call the parameter from the style sheet. Pass the XsltArgumentList object to the Transform method.

How is XSL stylesheet declared?

The root element that declares the document to be an XSL style sheet is <xsl:stylesheet> or <xsl:transform>. Note: <xsl:stylesheet> and <xsl:transform> are completely synonymous and either can be used!

How do I import one XSLT to another?

Remarks. An XSLT file can import another XSLT file using an <xsl:import> element. Importing an XSLT file is the same as including it except that definitions and template rules in the importing file take precedence over those in the imported XSLT file.

Can you use CSS with XSLT?

An XSLT style sheet can emit HTML <STYLE> elements, including CSS specifications, directly into the HTML that results from the XSLT transformation. This option works best when the number of CSS rules is small and easily managed.


1 Answers

Try this:

main.xsl

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:import href="import.xsl"/>

  <xsl:variable name="param" select="'some-value'"/>

  <xsl:template match="/">
    <xsl:call-template name="foo"/>    
  </xsl:template>

</xsl:stylesheet>

import.xsl

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output omit-xml-declaration="yes" indent="yes"/>

  <xsl:param name="param" select="'default'"/>

  <xsl:template name="foo">
    <out><xsl:value-of select="$param"/></out>    
  </xsl:template>

</xsl:stylesheet>

An xsl:variable in an importing stylesheet can override an xsl:param in an imported stylesheet, and this effectively sets the value of the parameter.

like image 70
Michael Kay Avatar answered Nov 16 '22 02:11

Michael Kay