Setting parameters with XSLT

To use parameters in an XSLT stylesheet, declare the parameters as global parameters in the stylesheet itself.

Procedure

  • Use the as attribute to declare a type for the parameter.
    The following stylesheet declares two parameters, explicitly giving one of them a type.
    <?xml version="1.0"?>
    <xsl:stylesheet xmlns:xsl="https://www.w3.org/1999/XSL/Transform"
                    xmlns:xs="https://www.w3.org/2001/XMLSchema"
                    exclude-result-prefixes="xs"
                    version="2.0">
    
      <xsl:param name="targetDate" as="xs:date"/>
      <xsl:param name="window" select="3"/>
    
      <xsl:template match="/">
        <table>
          <tr><td>Task</td><td>Due</td><td>Status</td></tr>
    
          <xsl:apply-templates select="todo-list/task[xs:date(@due) le $targetDate + xs:dayTimeDuration(concat('P', $window, 'D'))]">
              <xsl:sort select="@due"/>
          </xsl:apply-templates>
        </table>
      </xsl:template>
    
      <xsl:template match="task">
        <tr>
          <td><xsl:value-of select="."/></td>
          <td><xsl:value-of select="format-date(xs:date(@due), '[MNn] [D1o]')"/></td>
          <td>
              <xsl:choose>
              <xsl:when test="xs:date(@due) lt $targetDate">OVERDUE by
                 <xsl:value-of select="days-from-duration($targetDate - xs:date(@due))"/> day(s)</xsl:when>
              <xsl:otherwise>Due in <xsl:value-of select="days-from-duration(xs:date(@due) - $targetDate)"/> day(s)</xsl:otherwise>
              </xsl:choose>
          </td>
        </tr>
      </xsl:template>
    
    </xsl:stylesheet>
  • Prepare an XSLT stylesheet with global parameters in exactly the same way as any other stylesheet because there is no need to declare anything in the static context.
    Assuming that this stylesheet is available using the xsltSource Source object, the following code prepares the stylesheet.
    // Create the factory
    XFactory factory = XFactory.newInstance();
    
    // Prepare the XSLT stylesheet
    XSLTExecutable xslt = factory.prepareXSLT(xsltSource);
  • To execute an XSLT stylesheet that uses parameters, supply (or bind) values for each parameter using an XDynamicContext instance.

    You must supply a value for any required parameters or an error is raised. For other parameters, if you do not supply a value, a default value is used—either one supplied in the stylesheet or a zero-length string.

    The XDynamicContext has a number of bind, bindItem, and bindSequence methods. Each has two parameters, where the first is a QName object corresponding to the name of the parameter and the second is the value.
    Table 1. XDynamicContext bind, bindItem, and bindSequence methods .

    The following table explains when to use each form of the XDynamicContext bind, bindItem, and bindSequence methods.

    Method Purpose
    bind Use when binding a single atomic value

    There is one form of this method for each of the Java™ types that is used in the standard mapping of built-in types to Java types. There are two additional forms—one that takes a Node object and one that takes a Source object. These are used for binding any node from a DOM tree and parsing a new source to yield a document node, respectively.

    bindItem Use when binding a single item as an XItemView object

    An XItemView object can be obtained from the result of executing another expression or constructed using an XItemFactory instance.

    bindSequence Use when binding sequences of less than or greater than one item

    There is one form of this method for each of the Java types that is used in the standard mapping of built-in types to Java types; each accepts an array of values the given type.

    There is an additional form that takes an XSequenceCursor. An XSequenceCursor can be the result of executing another expression or can be constructed using an XItemFactory instance.

    The following example executes the XSLT stylesheet prepared in the first example, first binding values for each of the parameters it will use. It assumes that a Result object called xsltResult has already been created.
    // Create an xs:date value for the "targetDate" parameter with date "April 10, 2009"
    XMLGregorianCalendar date = DatatypeFactory.newInstance().newXMLGregorianCalendarDate(2009, 4, 10, DatatypeConstants.FIELD_UNDEFINED);
    
    // Create a new dynamic context from the factory
    XDynamicContext dynamicContext = factory.newDynamicContext();
    
    // Bind an atomic value for the "targetDate" parameter
    dynamicContext.bind(new QName("targetDate"), date);
    
    // Bind an atomic value for the "window" parameter
    dynamicContext.bind(new QName("window"), 7);
    
    // Create an XML input document
    String xml = "<todo-list>" +
    "<task due='2009-03-31' completed=''>File Quarterly Report</task>" +
    "<task due='2009-05-04' completed='2009-04-22'>Review candidate resumes</task> +
    "<task due='2009-04-16' completed=''>Order stock</task>" +
    "<task due='2009-05-01' completed=''>Buy concert tickets</task>" +
    "</todo-list>";
    StreamSource source = new StreamSource(new StringReader(xml));
    
    // Execute the stylesheet
    xslt.execute(source, dynamicContext, xsltResult);