While an XSLT page generates a complete web page from an XML file, an XSLT fragment is a scrap of code that generates only a portion of a page. After you populate the XSLT fragment with the XML data, you reference it and the XML source inside an inserted XSLT fragment. This include file is then inserted into a dynamic page and placed on a server equipped with scripts for reading and transforming XML. When someone visits the dynamic page, the web server inserts the data from the XML file into this section of the page.
You begin this tutorial by creating an XSLT fragment.
Place the insertion point after the closing bracket (>) of the <div id="main"> tag and click the Select Parent Tag button,
, on the Coding tool bar.
Note: The Coding tool bar is new in Dreamweaver 8. You can find it on the right side of the Code view.
Select File > New and in the New Document Dialog, choose XSLT (Fragment) and click Create.
Figure 2. The New Document dialog box
You will then be prompted to enter the location for the XML file. This file can reside on your site or on the Internet. For now, select “Attach a local file on my computer or local area network” and click Browse.
Figure 3. The Locate XML Source dialog box
In Code view, move the cursor to just after the <xsl:template match=”/”> tag, press Return. Then select Edit > Paste. The div you cut from the static page should now be in the fragment as shown in Figure 4.
Figure 4. The code for the fragment after pasting div into it
If you look at the first line of code for the XLST fragment you’ll see a comment after the XML version declaration:
<!-- DWXMLSource="specials.xml" -->
Note: This comment is used by Dreamweaver to display the schema for specials.xml in the Bindings panel. Since it’s only a comment and not an actual link, it’s not like attaching a link to an entire XSLT page inside an XML file.
The specials.xml file contains the daily menu for Café Townsend. In a typical scenario, this XML file is generated from a web application that the chef uses to enter the appetizers, entrées, and desserts for a single day. The beauty of this solution is that form is separated from content. The Specials page does not need manual updating and the chef updates the menu using a simple web application that the customer never sees.
Look at the XML in the Bindings panel. There is one repeating element, menu_item, with two attributes, id and course, and several nonrepeating elements: name, link, description, and price.
Figure 5. The XML Schema for specials.xml
The final design for the menu will list each course followed by its items. For each item, the item’s name, price, description, and a link to a page showing the item is displayed. In the following steps you will bind these elements to heading and paragraph tags.
Now double-click the non-repeating element, description, to insert it onto the page. This will place the description data placeholder inside the paragraph you’ve just created. You should now have something that looks like Figure 6:
Figure 6. The XSLT fragment in progress
The XML feed also includes a nonrepeating element for a link. This link is a URL to a page showing the appetizer, entrée, or dessert. Applying a link to a XML data placeholder is slightly more involved than setting a link normally for ordinary static text. Here are the steps:
Figure 7. Choose the Data Sources option to see the XML Schema
In the Property inspector, the Link field will have {specials/menu_item/link} specified as the link attribute for the item name. The curly braces are important in that they instruct the XSL parser on a server or in a web browser to insert the link element from the XML schema as the source for this link text.
Figure 8. The correct way to specify a link using an element from an XML schema is to set it in curly braces
Now you will create a repeat region so that the final page will list menu items within a course.
In Code view, select the level three heading containing the name and price and the paragraph containing the description.
Figure 9. Selecting the basis for the repeat region
From the Insert tool bar, select XSLT and then click the Repeat Region button,
. The XPath Expression Builder will appear.
Figure 10. The XPath Expression builder dialog box
Switch to Design view. You should now see a tabbed region around the level three heading and paragraph. If you do not see the tabbed region, make sure that Invisible Elements is selected in the View > Visual Aids menu and that Hide All is not selected in the same menu. Also notice how each data placeholder has been shortened to only the element name. This has happened because you have set the context to start at menu_item and so the path between it and each element is no longer required.
Figure 11. The Repeat Region displayed in Design view
Preview the page in a browser by choosing File > Preview in Browser (F12 on Windows and Option + F12 on Mac). The browser is not doing the transformation in this case and Dreamweaver is not sending the file to the testing server for preview. Dreamweaver is doing the transform itself and creating a temporary file for the browser to display.
Figure 12. Previewing the fragment in a browser window