Dreamweaver

Create and edit conditional XSLT regions

You can create simple conditional regions or multiple conditional regions on an XSLT page. You can either select an element in Design view and apply a conditional region to the selection, or you can insert a conditional region wherever the insertion point is in the document.

For example, if you wanted to display the word “Unavailable” next to the price of an item when the item is unavailable, you type the text “Unavailable” on the page, select it, and then apply a conditional region to the selected text. Dreamweaver surrounds the selection with <xsl:if> tags, and only displays the word “Unavailable” on the page when the data match the conditions of the conditional expression.

Apply a conditional XSLT region

You can write a simple conditional expression to insert into your XSLT page. If content is selected when you open the Conditional Region dialog box, the content will be wrapped in an <xsl:if> block. If you content is not selected, the <xsl:if> block is added at the insertion point on the page. It’s a good idea to use the dialog box to get started and then customize the expression in Code view.

The <xsl:if> element is similar to the if statement in other languages. The element provides a way for you to test a condition and take a course of action based on the result. The <xsl:if> element allows you to test an expression for a single true or false value.

  1. Select Insert > XSLT Objects > Conditional Region or click the Conditional Region icon in the XLST category of the Insert bar.
  2. In the Conditional Region dialog box, enter the conditional expression to use for the region.

    In the following example, you want to test to see if the context node’s @available attribute value is true.

    The conditional expression to test in the Conditional Region dialog box

  3. Click OK.

    The following code is inserted in your XSLT page:

    <xsl:if test="@available=&apos;true&apos;">
    	Content goes here
    </xsl:if>
    Note: You must surround string values such as true in quotes. Dreamweaver encodes the quotes for you (&apos;) so that they are entered as valid XHTML.

    In addition to testing nodes for values, you can use any of the supported XSLT functions in any conditional statement. The condition is tested for the current node within your XML file. In the following example, you want to test for the last node in the result set:

    The conditional statement that tests the current node in your XML file

    For more information and examples on writing conditional expressions, see the <xsl:if> section in the Reference panel (Help > Reference).

Apply multiple conditional XSLT regions

You can write a simple conditional expression to insert into your XSLT page. If content is selected when you open the Conditional Region dialog box, the content is wrapped in an <xsl:choose> block. If you do not select content, the <xsl:choose> block is added at the insertion point on the page. It’s a good idea to use the dialog box to get started and then customize the expression in Code view.

The <xsl:choose> element is similar to the case statement in other languages. The element provides a way for you to test a condition and take a course of action based on the result. The <xsl:choose> element allows you to test for multiple conditions.

  1. Select Insert > XSLT Objects > Multiple Conditional Region or click the Multiple Conditional Region icon in the XLST category of the Insert bar.
  2. In the Multiple Conditional Region dialog box, enter the first condition.

    In the following example, you want to test to see if the context node’s price subelement is less than 5.

    The condition that tests the value of the context node’s subelement

  3. Click OK.

    In the example, the following code is inserted in your XSLT page:

    <xsl:choose>
    	<xsl:when test="price&lt;5">
    		Content goes here
    	</xsl:when>
    	<xsl:otherwise>
    		Content goes here
    	</xsl:otherwise>
    </xsl:choose>
  4. To insert another condition, place the insertion point in Code view between <xsl:when> tag pairs or just before the <xsl:otherwise> tag, and then insert a conditional region (Insert > XSLT Objects > Conditional Region).

    After you specify the condition and click OK, another <xsl:when> tag is inserted in the <xsl:choose> block.

    For more information and examples on writing conditional expressions, see the <xsl:choose> sections in the Reference panel (Help > Reference).

Set conditional region (If) properties

The purpose of the Set Conditional Region Property inspector is to change the condition used in a conditional region in your XSL page. The conditional region tests the condition and takes a course of action based on the result.

 In the Test box, enter a new condition, and then press Enter.

Set conditional (When) properties

The purpose of the Set Conditional Region Property inspector is to change the condition used in a multiple conditional region in your XSL page. The multiple conditional region tests the condition and takes a course of action based on the result.

 In the Test box, enter a new condition, and then press Enter.