Using XSL Param

XSL is not just a transformational process, you can also add information content during the transformation. The following transform tweaks the identity transform to add a date element as the first child of the root.

Here are our changes:

  1. Access the EXSLT extensions for dates and times in Xalan-J by adding the date: namespace.
  2. Exclude the date: namespace from the results by adding it to the exclude-result-prefixes attribute.
  3. Define an XSL date param and initialize it to the current date. Note that the param value specified in the transform may be externally overridden.
  4. Explicitly match on the root element: match=”/*”
    1. Use the name() function to create an element with the same name as the root
    2. Apply the identity template to any attributes of the root element
    3. Add a new date element that contains the value of the date param
    4. Apply the identity template to any children of the root element Producing our new echoWithDate.xsl transform file:
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      <?xml version="1.0"?>
      <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:date="http://exslt.org/dates-and-times"
      exclude-result-prefixes="date"
      version="1.0">

      <xsl:output method="xml"/>

      <xsl:param name="date" select="date:date()"/>
      <xsl:template match="/*" priority="20">
      <xsl:element name="{name()}">
      <xsl:apply-templates select="@*"/>
      <date><xsl:value-of select="$date"/></date>
      <xsl:apply-templates select="node()"/>
      </xsl:element>
      </xsl:template>

      <xsl:template match="@*|node()" priority="10">
      <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
      </xsl:template>
      </xsl:stylesheet>
      We can execute our transform from the command line.
      1
      1 $ cat root.xml
      <Root/>
      2 $ java org.apache.xalan.xslt.Process -IN root.xml -XSL echoWithDate.xsl
      <?xml version="1.0" encoding="UTF-8"?><Root><date>2010-10-02-04:00</date></Root>
      And we can also specify an external value for the date param via the command line.
      1
      3 $ java org.apache.xalan.xslt.Process -IN root.xml -XSL echoWithDate.xsl -PARAM date yesterday
      <?xml version="1.0" encoding="UTF-8"?><Root><date>yesterday</date></Root>
      Now, let’s update the XSL Servlet to add support for parameter values. Previously, we used the path information to specify the transform to run. Here, we extend that to include slash delimited, comma separated parameter name/value pairs: /transform-name/param1-name,param1-value/param2-name,param2-value Here is the updated code from XSL servlet doPost method:
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      //
      // Apply named transform to the posted request data
      String path = request.getPathInfo();
      if ( path != null && path.length() > 1 ) {
      String[] pathArgs = path.substring(1).split("/");
      try {
      //
      // 1st path argument specifies the template</b>
      Templates template = templates.get(pathArgs[0]);
      if ( template != null ) {
      Transformer transform = template.newTransformer();
      //
      // Subsequent arguments are param,value pairs
      for ( int i=1 ; i<pathArgs.length ; ++i ) {
      String[] params = pathArgs[i].split(",");
      if ( params.length == 2 && params[0].length() > 0 ) {
      transform.setParameter(params[0],params[1]);
      }
      }
      transform.transform( new StreamSource(in), new StreamResult(out));
      out.close();
      return;
      }
      }
      catch ( TransformerException e ) {
      response.setStatus(response.SC_INTERNAL_SERVER_ERROR);
      return;
      }
      }
      After updating the servlet, adding the transform, and updating our Web Application web.xml; we can see how it works with curl:
      1
      4 $ curl http://localhost:8080/xsl/transform/echoWithDate -d '<root/>'
      <?xml version="1.0" encoding="UTF-8"?><root><date>2010-10-02-04:00</date></root>
      5 $ curl http://localhost:8080/xsl/transform/echoWithDate/date,today -d '<root/>'
      <?xml version="1.0" encoding="UTF-8"?><root><date>today</date></root>
      Here’s the updated XSL Servlet packaged as a war file. Once again, you’ll need to add serializer.jar, xalan.jar and xml-apis.jar from Xalan-J to the web-application WEB-INF/lib directory.