Default Template Values

Default Template Values

As I mentioned earlier, if you give a parameter a value when you declare it, that value can be overridden if you specify a different value in an <xsl: with-param > element. However, if you dont specify a different value, the original value acts like a default value.

The following example adapts the earlier COLORS example. That template has a parameter named COLOR , but I can call this template without setting COLOR to any particular value:

 <xsl:template match="PLANET">          <xsl:if test="NAME='Mercury'">              <xsl:call-template name="COLORS">                  <xsl:with-param name="COLOR" select="'RED'"/>              </xsl:call-template>          </xsl:if>          <xsl:if test="NAME='Venus'">              <xsl:call-template name="COLORS">                  <xsl:with-param name="COLOR" select="'GREEN'"/>              </xsl:call-template>          </xsl:if>          <xsl:if test="NAME='Earth'">              <xsl:call-template name="COLORS">              </xsl:call-template>          </xsl:if>      </xsl:template> 

In this case, the COLOR parameter defaults to the value Ive given it in the <xsl:param> element in the COLORS template, blue:

 <xsl:template match="PLANET">          <xsl:if test="NAME='Mercury'">              <xsl:call-template name="COLORS">                  <xsl:with-param name="COLOR" select="'RED'"/>              </xsl:call-template>          </xsl:if>          <xsl:if test="NAME='Venus'">              <xsl:call-template name="COLORS">                  <xsl:with-param name="COLOR" select="'GREEN'"/>              </xsl:call-template>          </xsl:if>          <xsl:if test="NAME='Earth'">              <xsl:call-template name="COLORS">              </xsl:call-template>          </xsl:if>      </xsl:template>      <xsl:template name="COLORS">         <xsl:param name="COLOR" select="'blue'"/>         <TR>            <TD><FONT COLOR="{$COLOR}"><xsl:value-of select="NAME"/></FONT></TD>            <TD><FONT COLOR="{$COLOR}"><xsl:apply-templates select="MASS"/></FONT></TD>            <TD><FONT COLOR="{$COLOR}"><xsl:apply-templates select="RADIUS"/></FONT></TD>            <TD><FONT COLOR="{$COLOR}"><xsl:apply-templates select="DAY"/></FONT></TD>         </TR>     </xsl:template> 

Specifying Template Values on the Command Line

In addition to using <xsl:param> and <xsl:with-param> , you can also set the values of stylesheet parameters on the command line with many XSLT processors. How you set this is processor-specific.

For example, the following example shows how you assign param1 the value value1 on the command line using Oracles XSLT processor in Windows. Note that when you set parameter values on the command line, you still need to declare them with <xsl:param> in your stylesheet:

 C:\>java oracle.xml.parser.v2.oraxsl -p param1='value1' planets.xml planets.xsl graphics/ccc.gif output.xml 

Heres how youd do the same thing with Saxon:

 C:\>saxon source.xml stylesheet.xsl param1=value1 > output.xml 

and with Xalan:

 C:\>java org.apache.xalan.xslt.Process -IN planets.xml       -XSL planets.xsl -OUT output.xml -PARAM parma1 value1 

and XT:

 C:\XSL>java -Dcom.jclark.xsl.sax.parser=org.apache.xerces.parsers.SAXParser com.  jclark.xsl.sax.Driver planets.xml planets.xsl output.xml param1=value1 


Inside XSLT
Inside Xslt
ISBN: B0031W8M4K
EAN: N/A
Year: 2005
Pages: 196

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net