Advanced XSLT Tricks

XSLT is a complex language with lots of bells and whistles—and just as many pitfalls. It’s all too easy to head ambitiously into Code view and, with just a few keystrokes, completely break your XSLT style sheet. But since Dreamweaver’s XSLT tools take you only so far, you’ll undoubtedly find yourself wanting to dip into the code. Here are a couple of examples to help your explorations go a little more smoothly.

Sorting Data in a Repeat Region

The Repeat Region feature normally works by spitting out data it retrieves from an XML document in the order it appears in the XML file. In the case of an RSS feed, that’s usually OK, since RSS feed usually sort items in chronological order, with the most recent feed first. But what if you want information sorted another way, or you have an XML file with other kinds of data, like the employee listing on Inserting a Repeat Region? In that example, you might want employees listed in alphabetical order. Dreamweaver doesn’t have a tool that lets you do this. Fortunately, adding the code yourself is pretty easy:

  1. Click inside a repeat region and then click the “Code” or “Split” button in the document window’s toolbar.

    Alternatively, you can choose View→Code or View→“Code and Design”. Doing so drops you into the scary world of XSLT code.

  2. Locate the beginning of the repeat region.

    You’re looking for something like this: <xsl:for-each select="company-Info/employeeList/employee">, where the stuff in quotes after “select” is the XPath expression ...

Get Dreamweaver CS5: The Missing Manual now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.