Home > Articles > Web Services > XML

  • Print
  • + Share This
From the author of

Style Sheet Parameters

One more way to customize a style sheet is through the use of parameters. We've already added a parameter to the style sheet to help us single out a location for the weather:

...
<xsl:param name="weathercity">Tampa, FL</xsl:param>
<xsl:template match="weather">
	<h4>Local Weather:</h4>
	<xsl:apply-templates select="location[@city=$weathercity]"/>
	<br /><b>Other Locations:</b><br />
	<xsl:apply-templates select="location[@city!=$weathercity]"/>
</xsl:template>
...

First, we created the parameter and called it weathercity, giving it a default value. This allows us to use it in the conditional select statement by designating it with a dollar sign($). If the processor doesn't provide another value, the default value will be used, but we can give the processor the value the user selected.

Setting the Parameter in ASP

Setting the parameter is a simple matter of adding it once the processor has been created:

...
 'Create the XSL Processor
 set xslTemplate=createObject("MSXML2.XSLTemplate")
 xslTemplate.stylesheet = style
 set processor = xslTemplate.createProcessor

 'Set the parameter for the default city
 call processor.addParameter("weathercity", weatherlocation)

 'Set the source and destination
 processor.input = source
 processor.output = Response

 'Perform the transformation
 processor.transform
%>

The processor will then use any value that the user has selected.

Setting the Parameter in Java

Setting the parameter in Java is virtually identical to setting it in ASP:

...
      //Create the XSL processor
      TransformerFactory transFactory = TransformerFactory.newInstance();
      Transformer transformer = transFactory.newTransformer(style);

      //Set default weather location
      transformer.setParameter("weathercity", weatherlocation);
		  
      //Transform the document
      transformer.transform(source, result);
  
   } catch (Exception e) {
     out.print(e.getMessage());
     e.printStackTrace();
   }
  }
}
  • + Share This
  • 🔖 Save To Your Account