XSLT Logic, Variables and Parameters

Some XSLT Logic

XSLT has some logic commands that useful to processing criteria based output. These commands are the "if" and "choose" commands.

The "if" command is useful for deciding to show content or not based on a single criteria. There is no "else" in XSLT, so, the if is somewhat limited.


<xsl:if test="@nodeName = 'example' ">


<!-- do this -->


</xsl:if>


The "choose" command is a bit more powerful in that it allows for multiple choices to be selected from.

<xsl:choose>


<xsl:when test="">


<!-- do this -->


</xsl:when>


<xsl:otherwise>


<!-- do this -->


</xsl:otherwise>


</xsl:choose>


In this example, you can have as many when statements as you like, but must have the otherwise statement.
Using Variables and Parameters

Variables and parameters are temporary storage devices in XSLT for holding simple values to complete node sets of data.

Variables are defined with a name, and can be filled two different ways. The first method is to select the data into the variable as follows:


<xsl:variable name="myID" select="$currentPage/@id"/>

The second method is to fill the variable by having the content rendered into the variable.

<xsl:variable name="myID">


<xsl:value-of select="$currentPage/@id"/>


</xsl:variable>

The second method is useful when applying logic to filling the variable, in which case an if command or choose command would allow you to have the value selected logically.

Parameters are handled the same way, except their command is "xsl:param".

As for the difference, it is a matter of where they can be assigned.

0 comments:

:a: :b: :c: :d: :e: :f: :g: :h: :i: :j: :k: :l: :m: :n:

Post a Comment