XSL Extensible Stylesheet Language of XSL is a language for expressing style sheets. I always hate it when a term is defined with its own terms so let's backtrack just a minute to find out what a style sheet is. To understand a style sheet, we can look into our HTML vocabulary and pull out the same definition HTML uses for a style sheet. Style sheets are used to define a specific look or presentation attributes including font attributes such as type size, special effects (bold, italic, underline) color, and alignment for how HTML is displayed in the browser. Well, we already discussed that XML is not a markup language used for presentation. XSL is XML's presentation language. In XSL, you define how you want an XML document to display. The problem begins because XML, as already discussed, does not have any pre-defined tags. The document author defines every tag used in the XML document. For example, in HTML, the browser knows how to display the table tag, but not so in XML. The table could refer to a formatted display or it could represent a real table where you eat dinner. Since XML does not define how to present its elements graphically, we need a language that can. More explicitly, we can make the analogy that CSS is to HTML what XSL is to XML. Again, a complete tutorial about the intricacies of XSL is beyond the scope of this course, but the purpose of this course is to introduce you to the technology so that you can understand its role in Web Services. Introduction to XSL XSL is a standard recommended by the W3C. Detailed information about XSL, including the standard can be found at http://www.w3.org. XSL consists of 3 languages.
XPath XPath is a set of rules for defining parts of an XML document. It allows you to select specific elements or nodes in an XML document. You can locate the data you need access to in an XML file using XPath statements. XPath is key to XSL because it allows you to select the parts of the XML document you want output or transformed to another format. For example, let's say you have a very long XML document that is a catalog of books. You want to find the book called "Teaching XPath to Beginners." You would need to parse through the entire file to find the tile of your book. With XPath, you can choose to select only the part of the document you are looking for without having to parse the entire file, which proves to be more efficient and quicker. XPath defines paths and functions to access specified elements within an XML file. For example, you can explicitly call a particular element by its name and find its attribute if necessary. Check out the following example. <catalog> In the above XML file, we can explicitly select the <title> element with the following XPath statement: /catalog/book/title We can even use an XPath function to compare the price of the book with this statement. /catalog/book/[price > 25.00] XPath defines a library of functions and statements to provide great flexibility in selecting data from an XML document. XSLT Extensible Stylesheet Language Transformations is a language based on XML that can transform an XML document into another text-based document. The most common use for XSLT is to transform an XML document into an HTML or XHTML, which is the newest HTML standard based on XML syntax. In order to write an XSLT document, you select XML elements using an XPath statement. This allows the document writer to customize the output of the transformed file into something different than the input XML data file. The document could leave out elements in the transformation, create new elements, sort elements, rearrange elements, and even perform conditional checks. An XSL file typically accompanies an XML file as a separate file. The XML file contains the data that will be transformed to the resulting format by the XSL file. For example, when a user requests an XML file from a web server through a browser, the request is sent through the web server to the file, the file is parsed by the XML parser, which finds the accompanying XSL file, which is also parsed by the XML parser. As the XSL document is read, the statements of the XSL file select the parts of the XML file to display and apply to defined styles to the elements in the XML document. This is knows as the transformation process.
Figure 1: Transformation Process. The transformation process occurs using XPath statements and XSLT elements and functions which match elements in the source document. Knowing how to use XPath statements to return specific elements is key to writing a good XSL transformation. The source document is compared to templates that are defined using XSLT in the result document, which is then displayed to the browser, for example. In the spirit of "beginning programming," let's look at a "Hello World" example that transforms XML elements into HTML.
XSL Formatting Objects XSL formatting objects are not as commonly used as XPath and XSLT, but here we will define what the purpose of XSL formatting objects. XSL formatting objects can provide an XML document transformation into other formats such as Adobe PDF, Microsoft Word, or others. Objects to help with the transformation into other formats can be downloaded from several vendors. Apache has created Apache FOP which can be downloaded from http://www.apache.org. We can contrast formatting objects from XSLT by saying that XSLT provides a mechanism for transforming XML documents into web form, but we can use XSL formatting objects to transform a document into a print form. For more information on XSL technologies, see http://www.w3.org.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||