Accessibility

Table of Contents

Using XSL Filtering in Dreamweaver 8

Filtering XML data

In every programming language that I've had the pleasure (argh!) to deal with—and probably just a matter of fact in every programming language—there is a concept of conditional execution of code. In other words, a method to compare selectively one value to another and then, based upon the result, either display a certain item, perform a task, or perhaps do nothing at all.

In the XSL world, there are actually two methods for determining a conditional execution: <xsl:if> and <xsl:choose>. Which one you choose to implement will depend upon your desired results. In this article, I review the <xsl:if> element.

A simple <xsl:if> element might look like the following:

<xsl:if test="title">put the title here</xsl:if>

Notice that there is a test attribute for the <xsl:if> tag. This, in fact, is the only possible attribute for the <xsl:if> tag. The test attribute returns a value of true or false by evaluating the statement in quotes—in this case, whether or not the title node (or tag) exists. If the statement evaluates to true, then the code within the <xsl:if> tags is executed. Again, in this case, the text "put the title here" would be written into the document.

In Dreamweaver 8, you can create a simple "if" conditional visually using the Conditional Region icon in the Insert panel, or by manually writing the code. Let's first examine how to filter for a known entity such as the value of the <location> tag using the visual tools in Dreamweaver.

Begin by selecting all of the placeholders that you have placed on the page. Click the Conditional Region icon in the Insert panel. In the provided dialog box, type location = 'San Francisco, CA' (see Figure 4).

Applying a conditional region to the placeholders

Figure 4. Applying a conditional region to the placeholders

Now save the document and preview in the browser. Notice that you see only the entry that is happening in San Francisco.

Back in Dreamweaver, take a look at the code that has been generated for the conditional statement. You should see that it is not exactly as you entered it in the dialog box. Instead of using the single quotes, Dreamweaver substituted &apos; around "San Francisco". In Code view, replace &apos; with single quotes in each position. Save the page and test it again in the browser. You should see no change—in other words, both notations work. However, in order to conform truly to the XSL specifications, you should use the &apos; notation.

OK, so you've now successfully limited the data display based upon a known value. But what if the page needs to display all of the events happening in California? To accomplish this task, XSL provides a wide range of functions using the XPath language, through which you can parse the XML in order to locate specific entries based upon "partial" data.

As you'll notice, there are several entries in the XML with a city located in California. Locate just the cities in California that begin with "San". To accomplish this, you can use the XPath function starts-with, as follows:

<xsl:if test="starts-with(./location, 'San;')">
...display the proper entries here...
</xsl:if>

Let's break down the starts-with function. There are two parameters for the function, both of which are string values. The first parameter defines the node, or tag, to examine. In order to understand where this node is located, however, the parser needs to understand the scope, or amount of data, through which it needs to parse.

When you applied the Repeat Region behavior, Dreamweaver inserted the following XSL code:

<xsl:for-each select="events/item">

The select parameter has defined the context root as events/item. In other words, any elements or additional processing instructions within the <xsl:for-each> tag are going to refer to the contents of each <item> entry.

In this case, the ./ is simply shorthand for <events/item>. The parameter ./location is then instructing the parser to "look for the <location> tag in each item entry."

The next string, which you will notice is in single quotes, instructs the parser to look for the letters "San" as the first three letters of the <location> tag's value.

But if you want to look for all of the California entries, you'll need to tell the parser to examine the full node value, not just the first three letters, and see if it contains "CA". To do this, you will use an XPath function with the same name contains:

<xsl:if test="contains(./location, &apos;, CA&apos;)">
display the proper entries
</xsl:if>

The contains function uses the same two-string parameter approach that you used in the starts-with example, but you might notice that I also included the comma and space in the string to eliminate the possibility of a location value being found which simply contained "ca", as in "Calcutta."

To try this yourself, place the cursor in the placeholder content on the page and select the <xsl:if> tag from the tag line at the bottom of the document window. Using the Property inspector, replace the previous entry, location = 'San Francisco, CA', with contains(./location, ', CA'). Save your file and preview in the browser. You should now see entries listing the events in San Francisco, San Diego, and Los Angeles.

Where to go from here

There are many more XPath functions for extracting and comparing the contents of a string value, such as substring, substring-before, and substring-after. Hopefully, this article has given you some insight into the use of <xsl:if> to help filter elements of your XML data.

To learn more about XPath, visit the W3C's XPath tutorials.