To use parameters in an XSLT stylesheet, declare the parameters as global parameters in the stylesheet itself.
<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://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>
// Create the factory XFactory factory = XFactory.newInstance(); // Prepare the XSLT stylesheet XSLTExecutable xslt = factory.prepareXSLT(xsltSource);
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.
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. |
// 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);
In this information ... | IBM Redbooks, demos, education, and more(Index) Use IBM Suggests to retrieve related content from ibm.com and beyond, identified for your convenience. This feature requires Internet access. Most of the following links will take you to information that is not part of the formal product documentation and is provided "as is." Some of these links go to non-IBM Web sites and are provided for your convenience only and do not in any manner serve as an endorsement by IBM of those Web sites, the material thereon, or the owner thereof. |