- Sams Teach Yourself XML in 21 Days, Third Edition
- Table of Contents
- About the Author
- Acknowledgments
- We Want to Hear from You!
- Introduction
- Part I: At a Glance
- Day 1. Welcome to XML
- All About Markup Languages
- All About XML
- Looking at XML in a Browser
- Working with XML Data Yourself
- Structuring Your Data
- Creating Well-Formed XML Documents
- Creating Valid XML Documents
- How XML Is Used in the Real World
- Online XML Resources
- Summary
- Q&A
- Workshop
- Day 2. Creating XML Documents
- Choosing an XML Editor
- Using XML Browsers
- Using XML Validators
- Creating XML Documents Piece by Piece
- Creating Prologs
- Creating an XML Declaration
- Creating XML Comments
- Creating Processing Instructions
- Creating Tags and Elements
- Creating CDATA Sections
- Handling Entities
- Summary
- Q&A
- Workshop
- Day 3. Creating Well-Formed XML Documents
- What Makes an XML Document Well-Formed?
- Creating an Example XML Document
- Understanding the Well-Formedness Constraints
- Using XML Namespaces
- Understanding XML Infosets
- Understanding Canonical XML
- Summary
- Q&A
- Workshop
- Day 4. Creating Valid XML Documents: DTDs
- All About DTDs
- Validating a Document by Using a DTD
- Creating Element Content Models
- Commenting a DTD
- Supporting External DTDs
- Handling Namespaces in DTDs
- Summary
- Q&A
- Workshop
- Declaring Attributes in DTDs
- Day 5. Handling Attributes and Entities in DTDs
- Specifying Default Values
- Specifying Attribute Types
- Handling Entities
- Summary
- Q&A
- Workshop
- Day 6. Creating Valid XML Documents: XML Schemas
- Using XML Schema Tools
- Creating XML Schemas
- Dissecting an XML Schema
- The Built-in XML Schema Elements
- Creating Elements and Types
- Specifying a Number of Elements
- Specifying Element Default Values
- Creating Attributes
- Summary
- Q&A
- Workshop
- Day 7. Creating Types in XML Schemas
- Restricting Simple Types by Using XML Schema Facets
- Creating XML Schema Choices
- Using Anonymous Type Definitions
- Declaring Empty Elements
- Declaring Mixed-Content Elements
- Grouping Elements Together
- Grouping Attributes Together
- Declaring all Groups
- Handling Namespaces in Schemas
- Annotating an XML Schema
- Summary
- Q&A
- Workshop
- Part I. In Review
- Well-Formed Documents
- Valid Documents
- Part II: At a Glance
- Day 8. Formatting XML by Using Cascading Style Sheets
- Our Sample XML Document
- Introducing CSS
- Connecting CSS Style Sheets and XML Documents
- Creating Style Sheet Selectors
- Using Inline Styles
- Creating Style Rule Specifications in Style Sheets
- Summary
- Q&A
- Workshop
- Day 9. Formatting XML by Using XSLT
- Introducing XSLT
- Transforming XML by Using XSLT
- Writing XSLT Style Sheets
- Using <xsl:apply-templates>
- Using <xsl:value-of> and <xsl:for-each>
- Matching Nodes by Using the match Attribute
- Working with the select Attribute and XPath
- Using <xsl:copy>
- Using <xsl:if>
- Using <xsl:choose>
- Specifying the Output Document Type
- Summary
- Q&A
- Workshop
- Day 10. Working with XSL Formatting Objects
- Introducing XSL-FO
- Using XSL-FO
- Using XSL Formatting Objects and Properties
- Building an XSL-FO Document
- Handling Inline Formatting
- Formatting Lists
- Formatting Tables
- Summary
- Q&A
- Workshop
- Part II. In Review
- Using CSS
- Using XSLT
- Using XSL-FO
- Part III: At a Glance
- Day 11. Extending HTML with XHTML
- Why XHTML?
- Writing XHTML Documents
- Validating XHTML Documents
- The Basic XHTML Elements
- Organizing Text
- Formatting Text
- Selecting Fonts: <font>
- Comments: <!-->
- Summary
- Q&A
- Workshop
- Day 12. Putting XHTML to Work
- Creating Hyperlinks: <a>
- Linking to Other Documents: <link>
- Handling Images: <img>
- Creating Frame Documents: <frameset>
- Creating Frames: <frame>
- Creating Embedded Style Sheets: <style>
- Formatting Tables: <table>
- Creating Table Rows: <tr>
- Formatting Table Headers: <th>
- Formatting Table Data: <td>
- Extending XHTML
- Summary
- Q&A
- Workshop
- Day 13. Creating Graphics and Multimedia: SVG and SMIL
- Introducing SVG
- Creating an SVG Document
- Creating Rectangles
- Adobe's SVG Viewer
- Using CSS Styles
- Creating Circles
- Creating Ellipses
- Creating Lines
- Creating Polylines
- Creating Polygons
- Creating Text
- Creating Gradients
- Creating Paths
- Creating Text Paths
- Creating Groups and Transformations
- Creating Animation
- Creating Links
- Creating Scripts
- Embedding SVG in HTML
- Introducing SMIL
- Summary
- Q&A
- Workshop
- Day 14. Handling XLinks, XPointers, and XForms
- Introducing XLinks
- Beyond Simple XLinks
- Introducing XPointers
- Introducing XBase
- Introducing XForms
- Summary
- Workshop
- Part III. In Review
- Part IV: At a Glance
- Day 15. Using JavaScript and XML
- Introducing the W3C DOM
- Introducing the DOM Objects
- Working with the XML DOM in JavaScript
- Searching for Elements by Name
- Reading Attribute Values
- Getting All XML Data from a Document
- Validating XML Documents by Using DTDs
- Summary
- Q&A
- Workshop
- Day 16. Using Java and .NET: DOM
- Using Java to Read XML Data
- Finding Elements by Name
- Creating an XML Browser by Using Java
- Navigating Through XML Documents
- Writing XML by Using Java
- Summary
- Q&A
- Workshop
- Day 17. Using Java and .NET: SAX
- An Overview of SAX
- Using SAX
- Using SAX to Find Elements by Name
- Creating an XML Browser by Using Java and SAX
- Navigating Through XML Documents by Using SAX
- Writing XML by Using Java and SAX
- Summary
- Q&A
- Workshop
- Day 18. Working with SOAP and RDF
- Introducing SOAP
- A SOAP Example in .NET
- A SOAP Example in Java
- Introducing RDF
- Summary
- Q&A
- Workshop
- Part IV. In Review
- Part V: At a Glance
- Day 19. Handling XML Data Binding
- Introducing DSOs
- Binding HTML Elements to HTML Data
- Binding HTML Elements to XML Data
- Binding HTML Tables to XML Data
- Accessing Individual Data Fields
- Binding HTML Elements to XML Data by Using the XML DSO
- Binding HTML Tables to XML Data by Using the XML DSO
- Searching XML Data by Using a DSO and JavaScript
- Handling Hierarchical XML Data
- Summary
- Q&A
- Workshop
- Day 20. Working with XML and Databases
- XML, Databases, and ASP
- Storing Databases as XML
- Using XPath with a Database
- Introducing XQuery
- Summary
- Q&A
- Workshop
- Day 21. Handling XML in .NET
- Creating and Editing an XML Document in .NET
- From XML to Databases and Back
- Reading and Writing XML in .NET Code
- Using XML Controls to Display Formatted XML
- Creating XML Web Services
- Summary
- Q&A
- Workshop
- Part V. In Review
- Appendix A. Quiz Answers
- Quiz Answers for Day 1
- Quiz Answers for Day 2
- Quiz Answers for Day 3
- Quiz Answers for Day 4
- Quiz Answers for Day 5
- Quiz Answers for Day 6
- Quiz Answers for Day 7
- Quiz Answers for Day 8
- Quiz Answers for Day 9
- Quiz Answers for Day 10
- Quiz Answers for Day 11
- Quiz Answers for Day 12
- Quiz Answers for Day 13
- Quiz Answers for Day 14
- Quiz Answers for Day 15
- Quiz Answers for Day 16
- Quiz Answers for Day 17
- Quiz Answers for Day 18
- Quiz Answers for Day 19
- Quiz Answers for Day 20
- Quiz Answers for Day 21
Building an XSL-FO Document
Now you've seen XSL-FO and gotten an overview of what objects and properties are available; the next step is to see how XSL-FO works by understanding the XSL-FO example. The following sections describe the elements that are involved in the example.
Using <fo:root>
The document element of XSL-FO documents has to be <fo:root> (although, of course, you can give the namespace any name, not just fo:). In the sample XSLT style sheet, you converted the sample XML document's document element, <states>, into <fo:root> this way:
<?xml version='1.0'?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" version='1.0'> <xsl:template match="states"> <fo:root> . . .
The <fo:root> element can contain both a master set layout and page sequences.
The master set layout, which uses the <fo:layout-master-set> element, describes the masters, or templates, that you want to use in the document. You can specify the default page layout, such as margin sizes, here.
The page sequences, which use the <fo:page-sequence> element, specify the format for a sequence of pages. For example, you might give a series of pages the same headers and footers.
The example uses both a master set layout and page sequences, so you can see what they both do.
Using <fo:layout-master-set>
In XSL-FO you can use a master as a template for a page, a sequence of pages, or a region on a page. After you create a master, including specifying fonts and margin sizes, you can use it in a page sequence. For example, to create master templates, you can use the <fo:layout-master-set> element:
<?xml version='1.0'?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" version='1.0'> <xsl:template match="states"> <fo:root> <fo:layout-master-set> . . .
Now you list the masters you want to use in the document in the <fo:layout-master-set> element. For example, you can create a master for each page by using the <fo:simple-page-master> element. You can also create page sequence masters to format pages in a sequence (as when you alternate odd/even page formatting for the left and right sides of a book). In this case, you can format the main page by using an <fo:simple-page-master> element, as described in the next section.
Using <fo:simple-page-master>
You use the page master <fo:simple-page-master> to create a template for a page and outline what goes where. After you create a page master, you can use it where you want in a document. Currently, the XSL specification supports only the <fo:simple-page-master> page master. These are the properties you can use in this element to define the page master:
- Block margin properties: margin-top, margin-bottom, margin-left, margin-right, space-before, space-after, start-indent, and end-indent
- master-name
- page-height
- page-width
- reference-orientation
- writing-mode
In today's example, the page master is named mainPage. You can set the page height, width, and margins like this:
<?xml version='1.0'?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" version='1.0'> <xsl:template match="states"> <fo:root> <fo:layout-master-set> <fo:simple-page-master master-name="mainPage" page-height="300mm" page-width="200mm" margin-top="20mm" margin-bottom="20mm" margin-left="20mm" margin-right="20mm"> . . .
That lays out the overall geometry of the page, but there's more to the story: You can also lay out regions in a page, such as the header, footer, and body. You can have up to five regions in a page master in the XSL-FO 1.0 specification:
- Body— The body region is the body of the page. To create this region, you use <fo:region-body>.
- Before— The before region is the header. To create this region, you use <fo:region-before>.
- After— The after region is the footer. To create this region, you use <fo:region-after>.
- Start— The start region appears to the left of the body. To create this region, you use <fo:region-start>.
- End— The end region appears to the right of the body. To create this region, you use <fo:region-end>.
Each of the region elements also has a number of properties that you can use, as listed here:
- Border, padding, and background properties: background-attachment, background-color, background-image, background-repeat, background-position-horizontal, background-position-vertical, border-before-color, border-before-style, border-before-width, border-after-color, border-after-style, border-after-width, border-start-color, border-start-style, border-start-width, border-end-color, border-end-style, border-end-width, border-top-color, border-top-style, border-top-width, border-bottom-color, border-bottom-style, border-bottom-width, border-left-color, border-left-style, border-left-width, border-right-color, border-right-style, border-right-width, padding-before, padding-after, padding-start, padding-end, padding-top, padding-bottom, padding-left, and padding-right
- Block margin properties: margin-top, margin-bottom, margin-left, margin-right, space-before, space-after, start-indent, and end-indent
- clip
- column-count
- column-gap
- display-align
- extent
- overflow
- region-name
- reference-orientation
- writing-mode
Using <fo:region-body> and <fo:region-after>
In today's example, you set the margins of the body region by using the available margin properties. In addition, you can set the width or height of the other four regions by using the extent property, as in the following markup, which sets the footer region to 20mm height:
<xsl:template match="states"> <fo:root> <fo:layout-master-set> <fo:simple-page-master master-name="mainPage" page-height="300mm" page-width="200mm" margin-top="20mm" margin-bottom="20mm" margin-left="20mm" margin-right="20mm"> <fo:region-body margin-top="0mm" margin-bottom="10mm" margin-left="0mm" margin-right="0mm"/> <fo:region-after extent="20mm"/> </fo:simple-page-master> </fo:layout-master-set> . . .
You have now completed the page master you'll use in this document. Besides a page master, you also need to define a page sequence when you format documents in XSL-FO, and that's coming up next.
Using <fo:page-sequence>
A page sequence is a run of pages that are part of a group, such as a chapter in a book or a section in a report. You create page sequences by using the <fo:page-sequence> element. You can use page sequences to alternate page number for even/odd pages in a book, for example. Each such element refers to a page master such as the one you've already created and uses that page master to format its pages.
These are the properties you can use with the <fo:page-sequence> element:
- country
- format
- language
- letter-value
- grouping-separator
- grouping-size
- id
- initial-page-number
- force-page-count
- master-name
- master-reference
You specify the name of the page master you want to use in a page sequence by using the master-reference attribute. In today's example, the page master is named mainPage, so here's how the page sequence starts out:
<?xml version='1.0'? > <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" version='1.0'> <xsl:template match="states"> <fo:root> <fo:layout-master-set> <fo:simple-page-master master-name="mainPage" page-height="300mm" page-width="200mm" margin-top="20mm" margin-bottom="20mm" margin-left="20mm" margin-right="20mm"> <fo:region-body margin-top="0mm" margin-bottom="10mm" margin-left="0mm" margin-right="0mm"/> <fo:region-after extent="20mm"/> </fo:simple-page-master> </fo:layout-master-set> <fo:page-sequence master-reference="mainPage"> . . .
The next step is to specify the content of the page sequence, and you do that with the <fo:flow> element, as described in the next section.
Using <fo:flow>
You use <fo:flow> to create a text flow, much like the flow in browsers, where the browser decides how to arrange elements. To create a flow, you specify a region, and the document content flows into that region.
You don't need to let text flow as the XSL-FO processor chooses. Besides <fo:flow>, you can also use <fo:static-content> in page sequences. The content in <fo:static-content> is static, which means it stays as you've arranged it. Static content is often used for headers and footers, where you want to dictate the text content and position, such as page numbers that you might always want centered or on the right.
The <fo:flow> element supports a property named flow-name, and you can use that property to indicate that you want text content to flow into the body region of pages like this, finishing off the <fo:root> element:
<?xml version='1.0'?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" version='1.0'> <xsl:template match="states"> <fo:root> . . . <fo:page-sequence master-reference="mainPage"> <fo:flow flow-name="xsl-region-body"> <xsl:apply-templates/> </fo:flow> </fo:page-sequence> </fo:root> </xsl:template> . . .
Note that the <fo:flow> element, which handles the XML document's content, has an <xsl:apply-templates/> element in it. This means that the XML elements in the XML document will be matched with templates in the XSLT style sheet. For example, to match the <name> elements, you use this template:
<xsl:template match="state/name"> . . .
Now you've matched a <name> element. You want to display its text in bold, underlined text, as shown in Figure 10.1. How do you do that? You start by using <fo:block> to create a block element.
Using <fo:block>
In XSL-FO you can use the <fo:block> element to create block-level elements, much like the ones you created on Day 6, when working with CSS. You use this element to create your own rectangular region that appears on its own line. You can use it to give each data item from ch10_01.xml its own line in the output document.
You can use these properties with <fo:block> elements:
- Accessibility properties: source-document and role
- Aural properties: azimuth, cue-after, cue-before, elevation, pause-after, pause-before, pitch, pitch-range, play-during, richness, speak, speak-header, speak-numeral, speak-punctuation, speech-rate, stress, voice-family, and volume
- Border, padding, and background properties: background-attachment, background-color, background-image, background-repeat, background-position-horizontal, background-position-vertical, border-before-color, border-before-style, border-before-width, border-after-color, border-after-style, border-after-width, border-start-color, border-start-style, border-start-width, border-end-color, border-end-style, border-end-width, border-top-color, border-top-style, border-top-width, border-bottom-color, border-bottom-style, border-bottom-width, border-left-color, border-left-style, border-left-width, border-right-color, border-right-style, border-right-width, padding-before, padding-after, padding-start, padding-end, padding-top, padding-bottom, padding-left, and padding-right
- Font properties: font-family, font-size, font-stretch, font-size-adjust, font-style, font-variant, and font-weight
- Hyphenation properties: country, language, script, hyphenate, hyphenation-character, hyphenation-push-character-count, and hyphenation-remain-character-count
- Block margin properties: margin-top, margin-bottom, margin-left, margin-right, space-before, space-after, start-indent, and end-indent
- break-after
- break-before
- color
- font-height-override-after
- font-height-override-before
- hyphenation-keep
- hyphenation-ladder-count
- id
- keep-together
- keep-with-next
- keep-with-previous
- last-line-end-indent
- linefeed-treatment
- line-height
- line-height-shift-adjustment
- line-stacking-strategy
- orphans
- relative-position
- space-treatment
- span
- text-align
- text-align-last
- text-indent
- visibility
- white-space-collapse
- widows
- wrap-option
- z-index
To handle the data in the <name>, <population>, <capital>, and other elements in the XML document, you can use the following templates in ch10_02.xsl, which uses <fo:block> to create blocks displaying that data:
<xsl:template match="state/name"> <fo:block font-weight="bold" font-size="18pt" line-height="24pt" font-family="sans-serif" text-decoration="underline"> Name: <xsl:value-of select="."/> </fo:block> </xsl:template> <xsl:template match="state/population"> <fo:block font-size="18pt" line-height="24pt" font-family="sans-serif"> Population (people): <xsl:value-of select="."/> </fo:block> </xsl:template> <xsl:template match="state/capital"> <fo:block font-size="18pt" line-height="24pt" font-family="sans-serif"> Capital: <xsl:value-of select="."/> </fo:block> </xsl:template> <xsl:template match="state/bird"> <fo:block font-size="18pt" line-height="24pt" font-family="sans-serif"> Bird: <xsl:value-of select="."/> </fo:block> </xsl:template> <xsl:template match="state/flower"> <fo:block font-size="18pt" line-height="24pt" font-family="sans-serif"> Flower: <xsl:value-of select="."/> </fo:block> </xsl:template> <xsl:template match="state/area"> <fo:block font-size="18pt" line-height="24pt" font-family="sans-serif"> Area (square miles): <xsl:value-of select="."/> </fo:block> </xsl:template>
Now you have created the XSLT style sheet, ch10_02.xsl, which converts ch10_01.xml into the XSL-FO document ch10_03.fo. Congratulations! You've created an XSLT style sheet that transforms an XML document into XSL-FO form, which the FOP XSL-FO processor can now translate into a PDF file.