[http://creativecommons.org/licenses/by/3.0/]
This work is licensed under a CC
Attribution 3.0 Unported License [http://creativecommons.org/licenses/by/3.0/]
XSLT's template matching mechanism lets the XSLT processor find the best match
to process a selected node. XSLT also supports a more traditional way of using templates, where they are called in a way very similar for function calls in most programming languages. Another interesting area of XSLT are variables and parameters, which are used for storing or passing values within XSLT code. One special property of XSLT variables is that they cannot be changed, which is a result of the functional design of the language.
overwritingtemplates, the behavior can be customized
<xsl:variable name="sum" select="$op1 + $op2"/> <xsl:variable name="result" select="$sum * $factor"/>
<xsl:value-of select="$email-prefix"/> <!- - $email-prefix = 'You have ' - -> <xsl:value-of select="count(//message)"/> <xsl:value-of select="$email-suffix"/> <!- - $email-suffix = ' e-mail messages.' - ->
updated, they have to be re-created
variablesif they are constants?
<xsl:variable name="name" select="'value'"/> <xsl:template match="/"> <xsl:choose> <xsl:when test="$name = 'value'"> <xsl:variable name="result" select="'ok'"/> </xsl:when> <xsl:otherwise> <xsl:variable name="result" select="'problem'"/> </xsl:otherwise> </xsl:choose> <xsl:value-of select="$result"/> </xsl:template>
<xsl:variable name="name" select="'value'"/> <xsl:template match="/"> <xsl:variable name="result"> <xsl:choose> <xsl:when test="$name = 'value'"> <xsl:value-of select="'ok'"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="'problem'"/> </xsl:otherwise> </xsl:choose> </xsl:variable> <xsl:value-of select="$result"/> </xsl:template>
<xsl:param name="number" select="0"/> <xsl:variable name="number-param"> <xsl:if test="string(number($number)) = 'NaN'"> <xsl:message terminate="yes">"number" must be a proper number!</xsl:message> </xsl:if> <xsl:value-of select="$number"/> </xsl:variable> <xsl:template match="/"> <xsl:value-of select="10 * $number-param"/> </xsl:template>
main param start = 1 ; param count = 10 ; { loop (0) }; loop param counter ; { print $start + $counter ; if ( $counter < $count - 1) then loop ($counter + 1) ; }
<xsl:param name="start" select="1"/> <xsl:param name="count" select="10"/> <xsl:template match="/"> <xsl:call-template name="loop"> <xsl:with-param name="counter" select="0"/> </xsl:call-template> </xsl:template> <xsl:template name="loop"> <xsl:param name="counter"/> <xsl:message> <xsl:value-of select="$start + $counter"/> </xsl:message> <xsl:if test="$counter < $count - 1"> <xsl:call-template name="loop"> <xsl:with-param name="counter" select="$counter + 1"/> </xsl:call-template> </xsl:if> </xsl:template>
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:param name="message" select="'plain'"/> <!-- messaging style: 'silent', 'plain', or 'prefixed' --> <xsl:template name="message"> <xsl:param name="level" select="'fatal'"/> <!-- message level: 'warning', 'error', or 'fatal' --> <xsl:param name="text" select="'no text given'"/> <xsl:choose> <xsl:when test="$message = 'silent'"/> <xsl:when test="$level = 'warning'"> <xsl:call-template name="print_message"> <xsl:with-param name="level" select="'warning'"/> <xsl:with-param name="terminate" select="'no'"/> <xsl:with-param name="text" select="$text"/> </xsl:call-template> </xsl:when> <xsl:when test="$level = 'error'"> <xsl:call-template name="print_message"> <xsl:with-param name="level" select="'error'"/> <xsl:with-param name="terminate" select="'no'"/> <xsl:with-param name="text" select="$text"/> </xsl:call-template> </xsl:when> <xsl:when test="$level = 'fatal'"> <xsl:call-template name="print_message"> <xsl:with-param name="level" select="'fatal'"/> <xsl:with-param name="terminate" select="'yes'"/> <xsl:with-param name="text" select="$text"/> </xsl:call-template> </xsl:when> <xsl:otherwise> <xsl:call-template name="print_message"> <xsl:with-param name="level" select="'fatal'"/> <xsl:with-param name="terminate" select="'no'"/> <xsl:with-param name="text" select="concat('fatal:unknown level for message [', $text, ']')"/> </xsl:call-template> </xsl:otherwise> </xsl:choose> </xsl:template> <xsl:template name="print_message"> <!-- this template should never be called from outside; for printing messages, call the "message" template. --> <xsl:param name="level"/> <xsl:param name="terminate"/> <xsl:param name="text"/> <xsl:choose> <xsl:when test="$message = 'plain'"> <xsl:message terminate="{$terminate}"> <xsl:value-of select="$text"/> </xsl:message> </xsl:when> <xsl:otherwise> <xsl:message terminate="{$terminate}"> <xsl:value-of select="concat($level, ':', $text)"/> </xsl:message> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>