web analytics

XSLT Programming Instructions

Options

codeling 1599 - 6654
@2016-03-03 23:11:42

XSLT has a number of traditional programming instructions. Their form tends to be verbose, because their syntax is built from XML elements.

  • <xsl:choose> Programming Instruction
  • <xsl:for-each> Programming Instruction
  • <xsl:if> Programming Instruction

<xsl:choose> Programming Instruction

The <xsl:choose> instruction is a conditional construct that causes different instructions to be processed in different circumstances. It is similar to a switch statement in traditional programming languages. The <xsl:choose> instruction contains one or more <xsl:when> elements, each of which tests an XPath expression. If the test evaluates to true, the XSLT processor executes the instructions in the <xsl:when> element. After the XSLT processor finds an XPath expression in an <xsl:when> element that evaluates to true, the XSLT processor ignores all subsequent <xsl:when> elements contained in the <xsl:choose> instruction, even if their XPath expressions evaluate to true. In other words, the XSLT processor processes only the instructions contained in the first <xsl:when> element whose test attribute evaluates to true. If none of the <xsl:when> elements’ test attributes evaluate to true, the content of the optional <xsl:otherwise> element, if one is present, is processed.

The <xsl:choose> instruction is similar to a switch statement in other programming languages. The <xsl:when> element is the “case” of the switch statement, and you can add any number of <xsl:when> elements. The <xsl:otherwise> element is the “default” of the switch statement.

<xsl:choose>
     <xsl:when test="xpath-expression">
        ...
    </xsl:when>
    <xsl:when test="another-xpath-expression">
        ...
     </xsl:when>
    <xsl:otherwise>
        ...
     </xsl:otherwise>
</xsl:choose>

<xsl:for-each> Programming Instruction

The <xsl:for-each> element tells the XSLT processor to gather together a set of nodes and process them one by one. The nodes are selected by the XPath expression specified by the select attribute. Each of the nodes is then processed according to the instructions held in the <xsl:for-each> construct.

<xsl:for-each select="xpath-expression">

     ...

</xsl:for-each>

Code inside the <xsl:for-each> instruction is evaluated recursively for each node that matches the XPath expression. That is, the current context is moved to each node selected by the <xsl:for-each> clause, and processing is relative to that current context.

In the following example, the <xsl:for-each> construct recursively processes each node in the [system syslog file] hierarchy. It updates the current context to each matching node and prints the value of the name element, if one exists, that is a child of the current context.

<xsl:for-each select="system/syslog/file">

    <xsl:value-of select=”name”/>

</xsl:for-each>

<xsl:if> Programming Instruction

An <xsl:if> programming instruction is a conditional construct that causes instructions to be processed if the XPath expression held in the test attribute evaluates to true.

<xsl:if test="xpath-expression">

     ...executed if test expression evaluates to true

</xsl:if>

There is no corresponding else clause.

Comments

You must Sign In to comment on this topic.


© 2024 Digcode.com