GEN TRAN XMLcom - Ibm.xtt - Xml.tutorial
GEN TRAN XMLcom - Ibm.xtt - Xml.tutorial
ii
Contents
Generate and transform an XML file. . . 1
Lesson 1.1: Create your DTD . . . . Switch to the Resource perspective . Create a project . . . . . . . Create a DTD . . . . . . . . Lesson checkpoint . . . . . . Lesson 1.2: Customize your DTD . . Create and name elements . . . . Edit an elements content model . . Edit an elements attributes . . . Edit a child elements content model Add attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1 1 1 2 2 2 3 3 3 4 Lesson checkpoint . . . . . . . . Lesson 1.3: Create and edit your XML file . Generate an XML file based on your DTD Edit in the Design view . . . . . . . Edit in the Source view . . . . . . . Lesson checkpoint . . . . . . . . Lesson 1.4: Create and edit an XSL style sheet Create the style sheet . . . . . . . Edit the style sheet . . . . . . . . Transform the XML file to HTML . . . Lesson checkpoint . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 5 5 5 6 7 7 7 8 8 9 9
iii
iv
Learning objectives
This tutorial is divided into several exercises you must complete in sequence. This tutorial teaches you to create an XML file based on a DTD and transform it using an XSL file. While completing the exercises, you will perform these tasks: v Create a DTD v Add and modify elements and attributes in a DTD v Generate XML files from DTD files v Explore the concept of grammar constraints v Create and modify an XSL file v Transform XML files using XSL files
Time Required
This tutorial should take approximately 30 minutes to finish. If you explore other concepts related to this tutorial, it could take longer to complete. Related information View the PDF version
Create a project
1. Click File New Project. 2. Expand the General folder and select Project. Click Next. 3. In the Project name field, type MyProject. 4. Click Finish.
Create a DTD
1. Click File New Other.
2. Expand the XML folder and select DTD file. If you do not see the XML folder, select the Show All Wizards check box. 3. Click Next. If you are prompted to enable XML Development capabilities, click OK. 4. Select MyProject and in the File name field, type CDtitle.dtd. 5. Click Finish. The CDTitle.dtd file is created and opened in the DTD editor.
Lesson checkpoint
The DTD editor allows you to create and edit a DTD file. Now that you have completed this lesson, you should be able to: v Switch perspectives. v Create a new project. v Create a DTD file.
1. For a CD, there can be more than one contributing artist. Indicate this by selecting the artist child element (that is, the artist element that is listed under CD) and changing its occurrence to be One or more. 2. Most titles fit on one CD. For such titles, the format element is optional. Indicate this by selecting the format child element and change its occurrence to be Optional. The resulting CD element should look like this (in the Source view):
<!ELEMENT CD (artist+,format?,description,price)>
Add attributes
You now need to add the attribute title to the CD element (the parent element, not the child element under CDLib). To do so, follow these steps: 1. In the Outline view, right-click the CD parent element, and select Add Attribute. Note: All elements must have their attributes listed in an attribute list, therefore, if you add an attribute to an element that does not already have an attribute list, an attribute list will automatically be created and the new attribute added to it. 2. Expand the attribute list and select NewAttribute. In the Properties view, change the name of the attribute to title. The attribute type value defaults to Character Data (CDATA), and the attribute default value should already be set to #IMPLIED. 3. To save the DTD, select File Save. You have now completed your CDLib definition. It should look similar to the following:
Lesson checkpoint
The DTD editor allows you to efficiently create, and edit, elements and attributes. Now that you have completed this lesson, you should be able to: v Create and name elements. v Edit elements content models. v Add attributes to elements.
2. In the Design view, select the CDLib element and expand it. 3. Edit the contents of the CD element. Change the artist value to John Brown, the description to Opera and the price to 15.00. 4. CDTitle.xml currently only contains one CD. You are going to add another: a. Right-click CDLib and select Add Child CD. b. A new CD element is automatically created, and it contains three elements: artist, description, and price. c. Change the artist value to Jane Smith, the description to Classical and the price to 20.00. d. Click File Save to save your changes. 5. Try to add and remove certain elements and attributes from the first CD element: a. Right-click artist. The menu option Remove is not available because artist is a required element. (You specified this in your DTD file.) b. Right-click description and price. The menu option Remove is not available because they are also required items. c. Right-click CD and click Add Child. You have the option to add another artist, but not another description or price. The option to add another description or price is not available, because in CDTitle.dtd, you specified that these items could only occur once inside a CD element. However, you specified that you could have one or more artist elements, which is why you can select to add another artist. 6. You are now going to turn the grammar constraints off, and continue to edit CDTitle.xml. Click XML Turn Grammar Constraints Off. 7. Right-click artist in the first CD element and click Remove. You can remove this required element because the grammar constraints are off. 8. Right-click description in the first CD element and click Remove. 9. Right-click the first CD element and select Add Child. You now have the option to add another price. Click price. 10. Save your file. Note that you are allowed to save it even with all the syntactical errors. This is because you turned the constraints off. 11. In the Navigator view, right-click the XML file and click Validate XML File. The errors you have introduced into the file are listed in the Problems view.
The source code for your file should appear similar to the following:
Lesson checkpoint
The Design and Source views within the XML editor allow you to easily make changes to you XML file based on the constraints of the associated DTD. The grammar constraints option allows you to choose whether to apply the rules to the file at any given time. Now that you have completed this lesson, you should be able to: v Generate an XML file based on your DTD. v Edit the file in the Design view with grammar constraints on and off. v Edit the file in the Source view.
Lesson checkpoint
The XSL editor provides a number of wizards to help you create a content filled style sheet. It also provides a method to create an HTML table based on that style sheet. Now that you have completed this lesson, you should be able to: v Create an XSL style sheet. v Fill the style sheet with contents which will generate an HTML table. v Transform an XML file to HTML.
Summary
Congratulations! You have successfully completed these tasks: v Created and customized a DTD. v Generated an XML file from your DTD. v Worked with grammar constraints. v Created and edited an XSL file. v Transformed your XML file using your XSL file.
More information
If you want to learn more about the topics covered in this tutorial, consider the following sources: v The W3 Web site: www.w3.org/Style/XSL/ contains information on DTDs, XML files, and XSL files and transformation. v The online help (Help Help Contents) includes documentation for the DTD editor, the XML editor, and the XSL editor.
10