Chapter 2 - Writing Your First XSLT Stylesheet | |
XSLT For Dummies | |
by Richard Wagner | |
Hungry Minds 2002 |
An XSLT stylesheet never acts alone. Its always applied to an XML document. So your first step in beginning XSL transformations is to create a simple XML document you can use as a sample. Start out by creating a working folder on your computer into which you can put in documents and stylesheets you create or download from the XSLT For Dummies Web site. I recommend creating a new folder called xslt on your root C: drive (c:\xslt), although feel free to place it anywhere convenient . To create an XML document, open X-Factor and choose New XML Source File from the File menu. Enter the following code in the XML editor window: <?xml version="1.0" encoding="UTF-16"?> <scores> <score id="1"> <film>A Little Princess</film> <composer>Patrick Doyle</composer> <year>1995</year> <grade>100</grade> </score> <score id="2"> <film>Chocolat</film> <composer>Rachel Portman</composer> <year>2001</year> <grade>90</grade> </score> <score id="3"> <film>Vertigo</film> <composer>Bernard Herrmann</composer> <year>1956</year> <grade>95</grade> </score> <score id="4"> <film>Field of Dreams</film> <composer>James Horner</composer> <year>1989</year> <grade>96</grade> </score> <score id="5"> <film>Dead Again</film> <composer>Patrick Doyle</composer> <year>1991</year> <grade>97</grade> </score> </scores> XML files use an .xml extension, so save the file as score.xml. Tip You can save time typing in the XML by downloading the score.xml file from the XSLT For Dummies Web site. In this sample XML document, the first line contains what is called a processor directive <?xml version="1.0"?> . This is simply a statement telling an XML processor: "Hello, I am an XML file. Please process me." The rest of the document contains the actual XML data. XML information is hierarchical: The outermost elements contain the elements that are inside of them. Therefore, the scores element contains five score elements in what is known as a parent-child relationship . (I discuss element relationships more in Chapter 3.) Each score element has an attribute that provides an id value and has four child elements: film , composer , year , and grade .
|