- XML Reference Guide
- Overview
- What Is XML?
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Table of Contents
- The Document Object Model
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- DOM and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- Implementations
- DOM and JavaScript
- Using a Repeater
- Repeaters and XML
- Repeater Resources
- DOM and .NET
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Downloads
- DOM and C++
- DOM and C++ Resources
- DOM and Perl
- DOM and Perl Resources
- DOM and PHP
- DOM and PHP Resources
- DOM Level 3
- DOM Level 3 Core
- DOM Level 3 Load and Save
- DOM Level 3 XPath
- DOM Level 3 Validation
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Implementations
- The Simple API for XML (SAX)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- SAX and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- SAX and .NET
- Informit Articles and Sample Chapters
- SAX and Perl
- SAX and Perl Resources
- SAX and PHP
- SAX and PHP Resources
- Validation
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Document Type Definitions (DTDs)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XML Schemas
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- RELAX NG
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Schematron
- Official Documentation and Implementations
- Validation in Applications
- Informit Articles and Sample Chapters
- Books and e-Books
- XSL Transformations (XSLT)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XSLT in Java
- Java in XSLT Resources
- XSLT and RSS in .NET
- XSLT and RSS in .NET Resources
- XSL-FO
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XPath
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XML Base
- Informit Articles and Sample Chapters
- Official Documentation
- XHTML
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XHTML 2.0
- Documentation
- Cascading Style Sheets
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XUL
- XUL References
- XML Events
- XML Events Resources
- XML Data Binding
- Informit Articles and Sample Chapters
- Books and e-Books
- Specifications
- Implementations
- XML and Databases
- Informit Articles and Sample Chapters
- Books and e-Books
- Online Resources
- Official Documentation
- SQL Server and FOR XML
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Implementations
- Service Oriented Architecture
- Web Services
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Creating a Perl Web Service Client
- SOAP::Lite
- Amazon Web Services
- Creating the Movable Type Plug-in
- Perl, Amazon, and Movable Type Resources
- Apache Axis2
- REST
- REST Resources
- SOAP
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- SOAP and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- WSDL
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- UDDI
- UDDI Resources
- XML-RPC
- XML-RPC in PHP
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Ajax
- Asynchronous Javascript
- Client-side XSLT
- SAJAX and PHP
- Ajax Resources
- JSON
- Ruby on Rails
- Creating Objects
- Ruby Basics: Arrays and Other Sundry Bits
- Ruby Basics: Iterators and Persistence
- Starting on the Rails
- Rails and Databases
- Rails: Ajax and Partials
- Rails Resources
- Web Services Security
- Web Services Security Resources
- SAML
- Informit Articles and Sample Chapters
- Books and e-Books
- Specification and Implementation
- XML Digital Signatures
- XML Digital Signatures Resources
- XML Key Management Services
- Resources for XML Key Management Services
- Internationalization
- Resources
- Grid Computing
- Grid Resources
- Web Services Resource Framework
- Web Services Resource Framework Resources
- WS-Addressing
- WS-Addressing Resources
- WS-Notifications
- New Languages: XML in Use
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Google Web Toolkit
- GWT Basic Interactivity
- Google Sitemaps
- Google Sitemaps Resources
- Accessibility
- Web Accessibility
- XML Accessibility
- Accessibility Resources
- The Semantic Web
- Defining a New Ontology
- OWL: Web Ontology Language
- Semantic Web Resources
- Google Base
- Microformats
- StructuredBlogging
- Live Clipboard
- WML
- XHTML-MP
- WML Resources
- Google Web Services
- Google Web Services API
- Google Web Services Resources
- The Yahoo! Web Services Interface
- Yahoo! Web Services and PHP
- Yahoo! Web Services Resources
- eBay REST API
- WordML
- WordML Part 2: Lists
- WordML Part 3: Tables
- WordML Resources
- DocBook
- Articles
- Books and e-Books
- Official Documentation and Implementations
- XML Query
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XForms
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Resource Description Framework (RDF)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Topic Maps
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation, Implementations, and Other Resources
- Rich Site Summary (RSS)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Simple Sharing Extensions (SSE)
- Atom
- Podcasting
- Podcasting Resources
- Scalable Vector Graphics (SVG)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- OPML
- OPML Resources
- Summary
- Projects
- JavaScript TimeTracker: JSON and PHP
- The Javascript Timetracker
- Refactoring to Javascript Objects
- Creating the Yahoo! Widget
- Web Mashup
- Google Maps
- Indeed Mashup
- Mashup Part 3: Putting It All Together
- Additional Resources
- Frequently Asked Questions About XML
- What's XML, and why should I use it?
- What's a well-formed document?
- What's the difference between XML and HTML?
- What's the difference between HTML and XHTML?
- Can I use XML in a browser?
- Should I use elements or attributes for my document?
- What's a namespace?
- Where can I get an XML parser?
- What's the difference between a well-formed document and a valid document?
- What's a validating parser?
- Should I use DOM or SAX for my application?
- How can I stop a SAX parser before it has parsed the entire document?
- 2005 Predictions
- 2006 Predictions
- Nick's Book Picks
HTML is terrific for what it's designed for: creating pages that adjust to the browser or other environment in which the content is to be displayed. But what if you wanted more control over the presentation of your content? Yes, you can use Cascading Style Sheets to perform all sorts of formatting miracles, but there comes a point at which you really need to move on to a technology more suited for the job.
In the XML realm, that technology is XSL-FO, or Extensible Stylesheet Language Formatting Objects. (Current versions refer to it simply as XSL, as opposed to XSLT, for Extensible Stylesheet Language Transformations, but XSL-FO seems to have stuck, no matter what the W3C says.) XSL-FO provides a methodology and a number of elements that enable you to design a document that not only controls the presentation of content, but also enables you easily convert your content to formats such as paged documents (perhaps the most common use of XSL-FO) in such a way that content not only flows smoothly from page to page, but also adapts to situations such as odd-and-even page differences.
For example, we can create a short, center-bound book of the "What is XML?" section of this guide:
<?xml version="1.0" encoding="iso-8859-1"?> <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> <fo:layout-master-set> <fo:simple-page-master master-name="my-page" page-height="4in"> <fo:region-body margin=".5in" /> </fo:simple-page-master> </fo:layout-master-set> <fo:page-sequence master-reference="my-page"> <fo:flow flow-name="xsl-region-body"> <fo:block>A cousin of Hypertext Markup Language (HTML), Extensible Markup Language (XML) provides a human-readable, platform-independent way to represent data, whether it's content destined for a web page or brochure, or data destined for an enterprise database. Like HTML and their mutual parent, Standard Generalized Markup Language (SGML), XML works on the principle of tags, which delineate elements and content. Consider, for example, the following XML document: <?xml version="1.0"?> <candy> <product>Chocolate Bar</product> </candy> This simple document has two elements: candy and product. These elements are indicated by tags, or constructions that start with the less-than sign (<) and ... </fo:flow> </fo:page-sequence> </fo:root>
This formatting objects file has two major sections. First, we define the page layout master, which includes the size of the page itself and margin information. Second, we have the actual page sequence, which specifies the master to follow. The data automatically flows between pages, as shown in Figure 1.
Of course, this is the equivalent of putting HTML on the page without breaking it up using <p></p> tags. We can break the content up into blocks, and we can specify style information on inline sections using the inline element:
<?xml version="1.0" encoding="iso-8859-1"?> <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> <fo:layout-master-set> <fo:simple-page-master master-name="my-page" page-height="4in"> <fo:region-body margin=".5in" /> </fo:simple-page-master> </fo:layout-master-set> <fo:page-sequence master-reference="my-page"> <fo:flow flow-name="xsl-region-body"> <fo:block padding-top="10px" padding-bottom="10px">A cousin of Hypertext Markup Language (HTML), Extensible Markup Language (XML) provides a human-readable, platform-independent way to represent data, whether it's content destined for a web page or brochure, or data destined for an enterprise database.</fo:block> <fo:block padding-top="10px" padding-bottom="10px">Like HTML and their mutual parent, Standard Generalized Markup Language (SGML), XML works on the principle of tags, which delineate elements and content. Consider, for example, the following XML document:</fo:block> <fo:block font-family="monospace"> <?xml version="1.0"?></fo:block> <fo:block font-family="monospace"><candy></fo:block> <fo:block font-family="monospace"> <product>Chocolate Bar</product> </fo:block> <fo:block font-family="monospace"></fo:block> <fo:block font-family="monospace"></candy></fo:block> <fo:block padding-top="10px" padding-bottom="10px">This simple document has two elements: <fo:inline font-family="monospace">candy</fo:inline> and <fo:inline font-family="monospace">product</fo:inline>. These elements are indicated by tags, or constructions that start with the less-than sign (<) and end with the greater-than sign (>) surrounding the name of the element. In other words, the <fo:inline font-family="monospace">candy</fo:inline> element starts with the <fo:inline font-family="monospace">candy</fo:inline> opening tag:</fo:block> <fo:block font-family="monospace"><candy></fo:block> ... </fo:flow> </fo:page-sequence> </fo:root>
The content can be broken into block elements, just as it is in HTML, with style information added directly to the element as attributes, rather than as part of the style attribute. Similarly, the inline element enables you to add style information without having to create a new block, as shown in Figure 2.
You can also use XSL-FO to create "conditional" page masters. For example, say we wanted to bind this booklet. We might want to leave extra margin in the center by creating different page layouts for the odd and even pages:
<?xml version="1.0" encoding="iso-8859-1"?> <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> <fo:layout-master-set> <fo:simple-page-master master-name="odd-page" page-height="4in"> <fo:region-body margin=".5in" margin-left="1.5in" /> </fo:simple-page-master> <fo:simple-page-master master-name="even-page" page-height="4in"> <fo:region-body margin=".5in" margin-right="1.5in" /> </fo:simple-page-master> <fo:simple-page-master master-name="first-page" page-height="4in"> <fo:region-body margin="2in" margin-right="1.5in" /> </fo:simple-page-master> <fo:page-sequence-master master-name="booklet-sequence"> <fo:repeatable-page-master-alternatives> <fo:conditional-page-master-reference page-position="first" master-reference="first-page"/> <fo:conditional-page-master-reference odd-or-even="odd" master-reference="odd-page"/> <fo:conditional-page-master-reference odd-or-even="even" master-reference="even-page"/> </fo:repeatable-page-master-alternatives> </fo:page-sequence-master> </fo:layout-master-set> <fo:page-sequence master-reference="booklet-sequence"> <fo:flow flow-name="xsl-region-body"> <fo:block padding-top="10px" padding-bottom="10px"> A cousin of Hypertext Markup Language (HTML), Extensible Markup Language (XML) provides a human-readable, platform-independent way to represent data, whether it's content destined for a web page or brochure, or data destined for an enterprise database.</fo:block> ... </fo:flow> </fo:page-sequence> </fo:root>
In this case, rather than following a single page master, the content flows into the conditional page master, which chooses the appropriate page master based on which page it is, as shown in Figure 3. You can also specify page positions such as "first" to include a title page.
You can also add static content to the page using a specific region. If the content flow takes up more than one page, this static content is automatically be added to each appropriate page:
<?xml version="1.0" encoding="iso-8859-1"?> <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format"> <fo:layout-master-set> <fo:simple-page-master master-name="odd-page" page-height="4in"> <fo:region-body margin=".5in" margin-left="1.5in" /> <fo:region-start background-color="#EEEEEE" extent="1.25in" /> </fo:simple-page-master> <fo:simple-page-master master-name="even-page" page-height="4in"> <fo:region-body margin=".5in" margin-right="1.5in" /> <fo:region-end background-color="#EEEEEE" extent="1.25in" /> </fo:simple-page-master> <fo:page-sequence-master master-name="booklet-sequence"> <fo:repeatable-page-master-alternatives> <fo:conditional-page-master-reference odd-or-even="odd" master-reference="odd-page"/> <fo:conditional-page-master-reference odd-or-even="even" master-reference="even-page"/> </fo:repeatable-page-master-alternatives> </fo:page-sequence-master> </fo:layout-master-set> <fo:page-sequence master-reference="booklet-sequence"> <fo:static-content flow-name="xsl-region-end"> <fo:block padding=".25in" margin=".1in">NOTES</fo:block> </fo:static-content> <fo:static-content flow-name="xsl-region-start"> <fo:block padding=".25in" margin=".1in">NOTES</fo:block> </fo:static-content> <fo:flow flow-name="xsl-region-body"> <fo:block padding-top="10px" padding-bottom="10px">A cousin of Hypertext Markup Language (HTML), Extensible Markup Language (XML) provides a human-readable, platform-independent way to represent data, whether it's content destined for a web page or brochure, or data destined for an enterprise database.</fo:block> ... </fo:flow> </fo:page-sequence> </fo:root>
Static comment can be added to a region and will appear on each page that accompanies the flow, as shown in Figure 4.
Notice that I've added a region-start, which appears to the left of the body (in a left-to-right language) to the even pages, but not to the odd pages, and vice versa for the region-end. This way, I can specify static content for those particular regions, and they'll only appear on the pages for which that region has been specified.
This is, of course, just a quick look at what you can do with XSL Formatting Objects. If you take its power and combine it with both the ability to generate standard files such as PDFs and the fact that you can easily generate XSL-FO with XSL Transformations, you have an essential tool in your presentational toolbox.