1 Alphabet Soup: XSL Stylesheets Overview The first tutorial in this series introduced the core Extensible Markup Language (XML) technologies. The second tutorial described the construction of a well-formed XML document. The third tutorial discussed the role of the XML schema, the primary elements of a schema, and the relationship between an XML document and an XML schema. In this tutorial, we describe the roles of XSL stylesheets and introduce the mark-up and transformation languages used in stylesheets. Presentation and Transformation We started this series by discussing the primary role of XML. XML is a set of technologies that simplify and enhance the distribution of data and information (Figure 1). XML does this by structuring data hierarchically using tags to convey the meaning of each unit of data (called an element in XML terminology). Figure 1: Information Sharing Using XML The core repository for data in an XML-based system is the XML document. An XML document is a text document that contains elements delimited using tags (Figure 2). XML Schema XML Document
17
Embed
Alphabet Soup: XSL Stylesheetsocean.otr.usm.edu/~w300778/is-doctor/pubpdf/xmlxsl.pdfAlphabet Soup: XSL Stylesheets Overview The first tutorial in this series introduced the core Extensible
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
1
Alphabet Soup: XSL Stylesheets
Overview
The first tutorial in this series introduced the core Extensible Markup Language (XML)
technologies. The second tutorial described the construction of a well- formed XML document.
The third tutorial discussed the role of the XML schema, the primary elements of a schema, and
the relationship between an XML document and an XML schema. In this tutorial, we describe
the roles of XSL stylesheets and introduce the mark-up and transformation languages used in
stylesheets.
Presentation and Transformation
We started this series by discussing the primary role of XML. XML is a set of technologies that
simplify and enhance the distribution of data and information (Figure 1). XML does this by
structuring data hierarchically using tags to convey the meaning of each unit of data (called an
element in XML terminology).
Figure 1: Information Sharing Using XML
The core repository for data in an XML-based system is the XML document. An XML
document is a text document that contains elements delimited using tags (Figure 2).
XML Schema
XML Document
2
Figure 2: BankAcct3.xml
Presentation: Presentation of information on the web is one common application of XML. By
default, web browsers capable of processing XML display the XML document in a hierarchical,
text format (Figure 3).
Figure 3: BankAcct3.xml in Internet Explorer
3
Although this format may be acceptable for simple XML documents, this format is not
appropriate for complex XML documents. XSL provides a means of formatting the XML data
for easier use. With XSL, the web browser displays a more meaningful view of the data (Figure
4).
Figure 4: BankAcct3.xml Rendered Using a XSL Stylesheet
Data Transformation: In addition to presenting information on the web, the XSL stylesheet
provides a means to transform data as needed so that different applications can examine the same
data in whatever way they need the data. One XML document may have many XSL stylesheets,
one stylesheet for each user (Figure 5). The user can be a person or an application.
4
Figure 5: Multiple XSL Stylesheets for One XML Document
As a simple illustration, a person interested in information about his or her bank account may
prefer the view in Figure 4; however, a computer program would have a great deal of difficulty
understanding this format. Instead, the computer program might prefer the data in a comma-
separated format like Figure 6.
Figure 6: Comma-separated BankAcct3.xml Data
A Simple XSL Stylesheet
XSL uses HyperText Markup Language (HTML) to format data for display by web browsers and
other applications that understand HTML, XPath for navigating through the XML hierarchy, and
XML Document
5
XSL Transformations (XSLT) for manipulating the data. Figure 7 is an example of a basic XSL
stylesheet.
Figure 7: A Simple XSL Stylesheet (BankAcct3.xsl)
To understand XSL, we will dissect this basic XSL stylesheet.
Line 1 declares the contents of the file as an XSL stylesheet based on the 1999 XSL standard.
Line 3 establishes the initial point in the XML document for processing nodes. The slash at the
start of match="/BankAccount" establishes the root node as the starting point for processing the
XML document. The remainder of the match property indicates this template manipulates the
top user-defined node in the XML document tree (bank account). Generally, the match property
for the initial template used to process the XML document starts with a reference to the root
node (/) followed by the name of the top user-defined node.
Lines 5 and 6 integrate HTML and XSLT to display the values of the account id element and the
balance element, respectively. Both lines start with standard HTML to display labels for the data
values. Each “ ” inserts a blank space in the stylesheet’s output; this is needed when
forcing space characters outside of HTML elements. The value-of transformation instruction
XPath
HTML
XSLT
6
displays the value of the element identified by the select property. The value-of instruction
self-terminates to ensure a well- formed stylesheet. The <br/> at the end of each line is “XML”
notation for an HTML line break; in general, single tag HTML elements require a trailing slash
to self terminate.
Line 8 starts repetitively processing each account holder node. Line 9 specifies the sort order for
account holder nodes, in this case sorting by the account holder’s name. Lines 11 through 17
display the account holder information using HTML and XSLT. Line 19 terminates this
repetitive processing.
Line 21 closes the template XPath pointer established by line 3.
Line 23 terminates the XSL stylesheet.
Open MyAccount.html in Internet Explorer. MyAccount.html processes the BankAcct3.xml
document using the BankAcct3.xsl stylesheet to produce data formatted using HTML (Figure 8).
The resulting page looks similar to Figure 4.
Figure 8: XML Document/XSL Stylesheet Relationship
Modifications: Now we will modify the stylesheet to display the information in different ways.
Open BankAcct3.xsl in Crimson Editor. Line 6 currently displays the value of the balance
element without any formatting. Modify line 6 as follows: