Dreamweaver

Use the XPath Expression Builder to add expressions for XML data

XPath (XML Path Language) is a non-XML syntax for addressing portions of an XML document. It is used mostly as a query language for XML data, just as the SQL language is used to query databases. For more information on XPath, see the XPath language specification on the W3C website at www.w3.org/TR/xpath.

The XPath Expression Builder is a Dreamweaver feature that lets you build simple XPath expressions for identifying specific nodes of data and for repeating regions. The advantage of using this method instead of dragging values from the XML schema tree is that you can format the value that is displayed. The current context is identified based on the position of the insertion point in the XSL file when the XPath Expression Builder dialog box is opened. The current context is in boldface type in the XML schema tree. As you make selections within this dialog box, the correct XPath statements, relative to your current context, are generated. This simplifies the process of writing correct XPath expressions for beginners and advanced users.

Note: This feature is designed to help you build simple XPath expressions to identify a specific node or for repeating regions. It does not allow you to edit the expressions by hand. If you need to create complex expressions, use the XPath Expression Builder to get started and then customize your expressions in Code view or with the Property inspector.

Create an XPath expression to identify a specific node

  1. Double-click the XML data placeholder on the page to open the XPath Expression Builder.
  2. In the XPath Expression Builder (Dynamic Text) dialog box, select any node in the XML schema tree.

    The correct XPath expression is written in the Expression box to identify the node.

    Note: If you select a different node in the XML schema tree, the expression changes to reflect your choice.

    In the following example, you want to display the price subelement of the item node:

    Select the node in the XML schema tree to display

    This selection would insert the following code in your XSLT page:

    <xsl:value-of select="price"/>
  3. (Optional) Select a formatting option from the Format pop‑up menu.

    Formatting a selection is useful when the value of your node returns a number. Dreamweaver provides a predefined list of formatting functions. For a complete list of available formatting functions and examples, see the Reference panel.

    In the following example, you want to format the price subelement as a currency with two decimal places:

    Specify the format of the price subelement as a currency with two decimal places

    These options would insert the following code in your XSLT page:
    <xsl:value-of select="format-number(provider/store/items/item/price,'$#.00')"/>
  4. Click OK.
  5. To display the value of each node in the XML file, apply a repeating region to the element containing the dynamic text (for example, an HTML table row or a paragraph).

    For more information and examples on selecting nodes to return a value, see the <xsl:value-of/> section in the Reference panel.

Select a node to repeat

You can select a node to repeat over and, optionally, to filter the results. In the XPath Expression Builder dialog box, your selected content will be wrapped inside an <xsl:for-each> block. If you have not selected content, the <xsl:for-each> block will be entered at the insertion point of your cursor.

  1. Double-click the XML data placeholder on the page to open the XPath Expression Builder.
  2. In the XPath Expression Builder (Repeating Region) dialog box, select the item to repeat in the XML schema tree.

    The correct XPath expression is written in the Expression box to identify the node.

    Note: Repeating items are identified with a Plus (+) symbol in the XML schema tree.

    In the following example, you want to repeat each item node within the XML file.

    Select the item node as the repeating item in the XML file

    When you click OK, the following code is inserted in your XSLT page:

    <xsl:for-each select="provider/store/items/item">
     Content goes here
    </xsl:for-each>

    In some cases, you may want to work with a subset of the repeating nodes—for example, you may only want items where an attribute has a specific value. In this case, you need to create a filter.

Filter the data to be repeated

Use a filter to identify repeating nodes that have specific attribute values.

  1. In the XML schema tree, select a node to repeat.
  2. Click the Build Filter expander button.
  3. Click the Plus (+) button to create an empty filter.
  4. Enter the filter criteria in the following fields:
    Filter By
    Specifies the repeating node that contains the data you want to filter by. The pop‑up menu provides a list of ancestor nodes relative to the node you selected in the XML schema tree.

    Where
    Specifies the attribute or subelement of the Filter By node that will be used to limit the results. You can select an attribute or subelement from the pop‑up menu, or you can enter your own XPath expression in this field to identify children that exist deeper within the schema tree.

    Operator
    Specifies the comparison operator to use in the filter expression.

    Value
    Specifies the value to check for in the Filter By node. Enter the value. If dynamic parameters are defined for your XSLT page, you can select one from the pop‑up menu.

  5. To specify another filter, click the Plus (+) button again.

    As you enter values or make selections in the pop‑up menus, the XPath expression in the Expression box changes.

    In the following example, you want to restrict the result set to those item nodes where the value of the @available attribute is true.

    Select the node and value that restrict the result set

    When you click OK, the following code is inserted in your XSLT page:
    <xsl:for-each select="provider/store/items/item[@available = &apos;true&apos;]">
      	Content goes here
    </xsl:for-each>
    Note: You need to surround string values such as true in quotes. Dreamweaver encodes the quotes for you (&apos;) so that they are entered as valid XHTML.

    You can create more complex filters that allow you to specify parent nodes as part of your filter criteria. In the following example, you want to restrict the result set to those item nodes where the store’s @id attribute is equal to 1 and the item’s price node is greater than 5.

    Restrict the result set by specifying parent nodes as filter criteria

    When you click OK, the following code is inserted in your XSLT page:
    <xsl:for-each select="provider/store[@id = 1]/items/item[price &gt; 5]">
    	Content goes here
    </xsl:for-each>

    For more information and examples of repeating regions, see the <xsl:for-each> section in the Reference panel.