Overview of XSL


As discussed in the previous section, XSLT was originally part of the effort to create a master presentation language for XML documents. While the ability to rearrange document content turned out to have many applications, it is still a necessary component of document presentation. Extensible Stylesheet Language ( XSL ) uses the XSLT syntax for modifying the document tree and adds its own syntax for attaching presentation properties to the nodes.

XSL's presentation properties focus on the requirements of paginated output, where the notion of a page has important formatting ramifications . While the most obvious form of paginated output is printing, electronic renditions of pages such as PDF or electronic books also clearly fall in the paginated category. You could even imagine pagination applying in some sense to aural output for books on tape. As you can probably guess, XSL is therefore of primary interest to publishing professionals.

The goal of XSL is to specify a language that allows people to apply paginated formatting to XML documents without contaminating logical document content. There are three primary requirements for a solution.

  1. Applying formatting rules to elements. Authors must be able to specify complete formatting rules for each type of element in a document. These rules include font format, indentation, line spacing, leading and trailing space, table formatting, and so on. Applying formatting rules to elements makes it very efficient to create stylesheets in conjunction with DTDs. Because all documents using a DTD have the same structure, it makes sense that they should have the same formatting rules.

  2. Applying formatting rules to pages. In addition to formatting rules for elements, paginated output requires applying formatting rules to each page. Changing the content of elements in a document changes the position of page breaks. It would be highly cumbersome for authors to figure out the position of these page breaks and insert special elements to provide features such as margins, headers, and footers. Therefore, style sheets for paginated output need general rules for specifying these page-level formatting concepts.

  3. Usable with different display technologies. One of the big advantages of a stylesheet approach is that an organization could render the same document content in different formats by changing stylesheets. A publisher could render the same book in a printing press format, an electronic book format, and a books on tape format. Therefore, the formatting rules must be flexible enough to accommodate the special needs of these different presentation platforms.

How It Works

The fundamental idea behind XSL is to use the same content to generate different paginated presentations. As just discussed, different presentations can accommodate different platforms. They could also accommodate different conventions, such as those used to format product press releases versus product data sheets, and users' physical differences, such as sight impairment .

People may also have logical differences. In Figure 3-5, employees in the Accounting department and Fulfillment department may want to print the same Order Document differently. Accounting employees want emphasis on the billing address for printing invoices, and Fulfillment employees want emphasis on the shipping address for printing packing lists. Each department can each define its own stylesheet, and the XSL processor can generate different page layouts for the same document.

Figure 3-5. Applying Different Stylesheets for Different Users

graphics/03fig05.jpg

Generating output occurs in two steps. First, an XSL stylesheet transforms a content document into a presentation document with the same mechanisms as using XSLT to transform one data document into another data document. The resulting presentation document contains XML content elements decorated with XSL formatting objects and is called a formatting object tree . Actually generating the final output requires the use of a formatter as a second step. As Figure 3-5 shows, a given presentation document may be rendered into many different outputs. In this case, each department generates both a Postscript file for printing and a PDF file for electronic storage.

Example 3-11 shows a fragment of a stylesheet you could apply to the "Order" document in Example 2-6 that emphasizes the billing address over the shipping address. With XSL, you define formatting templates that apply to an element and its subelements. This simple example has two templates. Note that instructions for selecting the particular elements are the same as for XSLT and use the "xsl" namespace, whereas instruction for formatting output use the "fo" namespace.

Example 3-11
 <xsl:template match="address" [@addType="bill" or    @addType="billship"]>  <fo:block   fo:font-size="large"   fo:font-weight="bold"   fo:font-family="Arial"   fo:line-height="2"   <xsl:apply-templates/>  </fo:block> </xsl:template> <xsl:template match="address[@addType="ship"]"> <fo:block   fo:font-size="small"   fo:font-weight="normal"   fo:font-family="Times"   fo:line-height="1"   <xsl:apply-templates/>  </fo:block> </xsl:template> 

The first template selects billing address elements. For each child element, it starts a new paragraph, in large, bold, Arial type with double line spacing. The "apply-templates" syntax applies this formatting to all children of the "address" element, including the " firstname ," "lastname," "street," "city," "state," "postal," and "country" elements. The second rule selects shipping address elements. For each child element, it starts a new paragraph in small, normal, Times type with single line spacing. So addresses for billing appear in large, bold type. Addresses for shipping appear only in small, lightface type.

As mentioned previously, XSL also includes formatting rules for pages as well as elements. These rules enable you to specify common page- related concepts such as margins, headers, and footers. Example 3-12 shows an XSL fragment that provides a header and a footer for orders.

Example 3-12
 <fo:layout-master-set>  <fo:simple-page-master master-name="Order"    page-width="8.5in" page-height="11in"    margin-top="0.5in" margin-bottom="0.5in"    margin-left="0.5in" margin-right="0.5in">   <fo:region-before extent="1.0in"/>   <fo:region-body margin="1.0in"/>   <fo:region-after extent="1.0in"/>  </fo:simple-page-master> </fo:layout-master-set> <fo:page-sequence master-name="Order">  <fo:static-content flow-name="xsl-region-before">   <fo:block>  Foo Company Order  </fo:block>  </fo:static-content>  <fo:static-content flow-name="xsl-region-after">   <fo:block>  p. <fo:page-number/>  </fo:block>  </fo:static-content> </fo:page-sequence> 

The first part of Example 3-12 defines a page master named "Order" and gives it half-inch margins. Besides the main body of the page, it also defines a header with the "fo:region-before" element and a footer with the "fo:region-after" element. The second part assigns content to this header and footer. The header gets the title "Foo Company Order" and the footer gets a page number identifier.

Practical Usage

XSL achieved W3C Recommendation status relatively recently, so there is not much practical experience with its application. At the time of this writing, there were only a handful of XSL processors and formatters for a very few output formats available. Most of the work with XSL so far has been performed by contributors to the standard and other industry experts using it on their own projects, such as book publishing.

However, given the history of XSL, we can make some fairly confident predictions about where it will be most useful. As discussed in the coverage of XSLT, applications that dynamically generate XML content as part of a server-side application are using XSLT to generate markup-oriented presentations such as HTML, WML, and VoiceXML directly. Combine this usage pattern with the focus of XSL on paginated output, and it becomes clear that the primary use of XSL will be for publishing.

Within publishing, XSL has the potential to affect both companies dedicated to the business of publishing and internal publishing groups within companies. For publishing companies, XSL enables them to target different delivery platforms such as printed pages, electronic books, and books on tape efficiently . For internal groups, XSL enables them to repurpose the same content for different uses such as press releases, data sheets, and technical specifications. There is also the possibility that XSL could greatly expand the scope of internal publishing to include capturing application-generated XML content, collating it, and dispersing it in a published form. All of these uses for XSL depend on the adoption by vendors of traditional publishing and content management systems.



XML. A Manager's Guide
XML: A Managers Guide (2nd Edition) (Addison-Wesley Information Technology Series)
ISBN: 0201770067
EAN: 2147483647
Year: 2002
Pages: 75
Authors: Kevin Dick

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net