The XSL standards

The title of this book refers to the acronym XSL, which stands for eXtensible Stylesheet Language. In the early stages of the development of this stylesheet language, this name was chosen to describe the specification. Since that time, things have become a little more complicated, and the final specification is actually an amalgamation of three standards: XSL, XSLT and XPath. The respective roles of these three standards can be explained in isolation. It is then possible to show how they work together to perform the task of formatting XML documents.

XSL

The original term 'XSL' is now used in a more limited way, to describe a way to specify output styles. The XSL standard defines an application of XML that deliberately breaks the golden rule for XML document structures. In this application, an element is used to describe the appearance of its content, instead of its meaning. In the following example, the element named 'block' contains a text block, such as a paragraph, and 'wrapper' contains a range of text to be styled in a specific way:

<block>This is an <wrapper style="bold">XSL</wrapper>
document.</block>

This is an XSL document.

XML documents should be self-describing, and so should not be created in this form. After all, an XML document that uses the XSL DTD is no more descriptive of its content than an HTML document. It is indeed analogous to an HTML page. The HTML equivalent of the example above is simply briefer (though HTML is a more limited format):

<P>This is an <B>HTML</B> document.</P>

This is an HTML document.

XSLT

XSLT (XSL Transformations) is an XML transformation language. Using XSLT, it is possible to convert XML documents into other data formats, including many of the formatting languages that are used to control the rendering of material onto a chosen medium. Such a language was originally part of the XSL standard but has now been extracted into a standard of its own. The XSLT standard specifies the format of the stylesheet document and includes definitions for the mapping rules. An XSLT document is actually an XML document that conforms to the XSLT DTD.

At the present time, the most popular use of XSLT is to convert XML documents into the data format recognized by Web browsers. The HTML format is an application of SGML, sharing ancestry with XML, and can therefore (with one or two caveats) be considered to be an XML format. This is convenient because XSLT is particularly good at converting one XML document into another XML document. The example below formats an XML fragment, containing a paragraph with an emphasized term, into equivalent HTML constructs:

						<paragraph>A <emphasis>Highlighted</emphasis>
   term</paragraph>


<template match="paragraph">
  <html:P>
    <apply-templates/>
  </html:P>
</template>

<template match="emphasis">
  <html:B>
    <apply-templates/>
  </html:B>
</template>


   <P>A <B>Highlighted</B> term</P>
					

However, XSLT can be used for purposes other than formatting. XML documents can simply be transformed into new XML documents that conform to a different structure (perhaps conforming to a different DTD). Element content can be re-used, repositioned, sorted, split or merged with other content, and even transformed into attribute values.

A number of interesting benefits arise from the fact that XSLT stylesheets are XML documents. First, it is possible to use a DTD both to validate a stylesheet and, along with an XML-sensitive editor, to guide stylesheet authors. Second, a stylesheet can be stored in an XML repository, where it can be manipulated and versioned, down to the element level. Third, because XSLT stylesheets process XML documents, it is possible to write stylesheets that convert other XML/SGML-based stylesheets into XSLT format, and to create stylesheet variants from another stylesheet. Finally, a stylesheet can be produced to format other stylesheets for presentation or printout.

XPath

XSLT can transform an XML document that conforms to one DTD into another XML document that conforms to a different DTD. At the simplest level, using techniques shown above, tags can be renamed to conform to element definitions in the target DTD. For example, a 'Paragraph' element can be output as a 'Para' element or 'P' element. But XSLT has much more powerful features that allow material to be moved or copied. Element content can become attribute values, and attribute values can become element content. Also, specific formatting can be applied to elements when they appear at a significant place within the document structure. This kind of advanced analysis and manipulation of data is performed using an expression language.

The original XSL proposal included such a language, but the XSLT standard now references a separate standard called XPath. This standard defines a general-purpose language for interrogating XML document structures. It is also used as a searching/query language and for advanced hypertext linking schemes. This language is not XML-based, but it has a compact syntax that has been designed to be suitable in a number of different circumstances. The following expression identifies 'Paragraph' elements within 'Chapter' elements that have a 'Type' attribute value of 'Important':

chapter//paragraph[@type='Important']

Once this expression language is understood, the skills gained can be utilized for querying XML structures (using XQL) and linking structures (using Xpointer).

Putting XSL, XSLT and XPath together

It can be seen that XSLT, with the assistance of XPath expressions, offers an ideal way to transform arbitrary XML documents into XSL documents, ready to be formatted for presentation. The following example shows an XSLT rule that maps a 'Paragraph' element in the source document to a bold paragraph in XSL format, but only when the paragraph is inside a 'Chapter' element and has a 'Type' attribute value of 'Important', as expressed using XPath:

<template match="Chapter//Paragraph[@type='Important']">
  <fo:block font-style='bold'>
    <apply-templates/>
  </fo:block>
</template>

Note that the focus of this book is on the XSLT standard for transforming XML documents. This standard makes significant use of the XPath expression language. The XSL standard itself is less vital to the whole package, as XSLT and XPath can be used together for many other purposes than to create XSL documents.

Naming confusion

The term 'XSL' is currently used to describe three different things, in one case more accurately than the others. First, it is correctly used to name the XSL standard itself. Second, more commonly but far less accurately, it is used to describe the standard properly named XSLT. Third, for historical reasons, it is used to describe the combination of XSLT (including XPath) and XSL, where an XSLT stylesheet is used to create XSL documents. Within this book, the third definition is used as well as the first, because no other term has emerged to describe the whole package (the context of the discussion should indicate which meaning applies).

The term 'XSL stylesheet' should perhaps refer to an XSLT document that outputs an XSL document, but is commonly understood to mean any XSLT document, no matter what output it generates.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset