Node:Step 10, Previous:Step 9, Up:Tutorial



Step 10: XSLT parameters

Parameters can be passed into the XSLT scripts. This is an XSLT feature, and we won't be explaining how to write XSLT stylesheets that use parameters here. All that we'll be explaining is how to supply those parameters to the scripts.

Parameters are supplied using the param element in the contents of the xslt element. This is an empty element that specifies the name and value of the parameter as attributes. The name is defined by the name attribute. The value is defined by either the value-xpath or value-string attribute.

The parameter values are XPath expressions. In simple scripts, you will probably be passing in a literal string value. However, if you forget to quote the string properly, the processor treats it as an XPath expression which does not match any nodes and appears as a blank string where it is used - this is very confusing for beginners and experts alike. For this reason, there are these two ways of supplying the value: value-string which automatically quotes the string value, and value-xpath whose name is a reminder that it expects an XPath expression.

The following rule illustrates two parameters being passed into the XSLT stylesheet.

<rule source-suffix=".xml" target-suffix=".html">
  <xml-load>
    <dir path="transbuild://">
      <children file-name="index.xml"/>
    </dir>
    <ancestors file-name="index.xml"/>
    <children file-name="index.xml"/>
  </xml-load>
  <xslt stylesheet="scripts/tr10-a.xsl">
    <param name="cssfile" value-string="/site2.css"/>
    <param name="titletext" value-xpath="/article/title"/>
  </xslt>
</rule>

Since the value-string is just a convenience mechanism, you could get the same results by using value-xpath with a quoted value.

  ...
    <param name="cssfile" value-xpath="'/site2.css'"/>
  ...

Two alternative stylesheets have been provided for you to experiment with: site1.css and site2.css.

Since these Web pages start with many navigation links, we've also added a "skip navigation links" hyperlink at the very beginning of the page. This makes it more accessible to people using voice browsers or who are restricted in how they navigate through a page.

Finally, you should always always check that your site is standards compliant: using valid HTML markup and valid CSS. Check that it is accessible and usable, test it with a number of different Web browsers, and test it on a variety of different platforms.

This is the end of the tutorial. If you want to learn more, have a look at the other examples in the test directory and read the reference section.