XML and XSL |
Modified: |
Web systems minimally consist of two distinct parts:
Differences - The skills needed to design and implement a user interface on the client are very different than those for dynamically generating pages on the server.
Separation - Ideally the user interface and server page generation implementations are separated or decoupled, allowing changes in one to be made independently of changes in the other.
For example, suppose you maintain the NYSE stock pages. You have two basic options for constructing the pages seen by the user:
This section is about implementing the user interface separately from the logic of the Web site.
Throughout the course, XML will be used to represent the data and combined HTML/XSL to implement the user interface.
A language for defining and representing languages.
A simple example of XML use is to supply structured data to an
application. Information for shipping a package from one Zip code to another can
be specified in XML with a hierarchy tree as:
| <package> <To>47150</To> <From>47165</From> <Weight>17.0</Weight> <Rate>27.50</Rate> </package> |
![]() |
Many programming languages and applications such as the IE Web browser can parse XML to build a hierarchical tree that corresponds to the XML. The parser reads the XML and constructs the corresponding tree. A program such as IE would use a parser to first construct the tree, then reference nodes on the tree to access each element of the XML. We'll see that XML parsing into a DOM (Document Object Model) tree is key, allowing programs (and therefore us) complete control of the tree and its contents; we will examine this in detail later in the course.
For example the following defines XML for Employees,
which is automatically parsed by IE and some other browsers into a DOM tree.
<Employees>
<Employee>
<First>James</First> <Last>Borg</Last>
</Employee>
<Employee>
<First>Joyce</First> <Last>English</Last>
</Employee>
</Employees>
|
|
Parse tree for above XML |
Exercise 1 - XML
|
Style sheets provide a consistent way to display a page.
XSL works to transform the data structure of the XML or HTML tree for display in a browser. The advantage is illustrated below where the XML file references the XSL file when loaded to display a table based upon the data structure defined in XML. This supports maintaining a standard look and feel across all applications that would generate a table that appeared the same though with different entries for the different source XML data recordsets.
How it works
|
![]() |
Employees.xml
<?xml-stylesheet type="text/xsl"
href="Employees.xsl"?>
<Employees>
<Employee>
<First>James</First>
<Last>Borg</Last>
</Employee>
<Employee>
<First>Joyce</First>
<Last>English</Last>
</Employee>
</Employees>
HTML and Table produced
|
Employees.xsl
<xsl:stylesheet version="2.0"
xmlns:xsl=
"http://www.w3.org/1999/XSL/Transform" >
<xsl:template match="/">
<TABLE BORDER="1">
<TR> <TD>First</TD> <TD>Last</TD> </TR>
<xsl:for-each select="Employees/Employee">
<TR>
<TD><xsl:value-of select="First"/></TD>
<TD><xsl:value-of select="Last"/></TD>
</TR>
</xsl:for-each>
</TABLE>
</xsl:template>
</xsl:stylesheet>
|
Exercise 3 - XSL
|
Exercise 4 - HTML from XML and XSL
IE displays the results of the XSL transformation of XML to HTML but does not let you examine the raw HTML nor give any debugging information. The msxsl program will display the resulting HTML and any error messages by:
|
The example below produces the same results as that above but use apply-templates rather than for-each to process XML elements. apply-templates specifies to apply the template defined for the next lower-level of the heirarchy.
Employees.xml file defines a 4-level hierarchy of elements, the Employees.xsl defines a corresponding hierarchy to process the XML elements.
Employees.xml Employees.xsl 1. "/" or root <xsl:template match="/"> 2. <Employees> <xsl:template match="Employees"> 3. <Employee> <xsl:template match="Employee"> 4. <First> and <Last> <xsl:value-of select="First"/>
<xsl:value-of select="Last"/>
| Employees.xml
<?xml-stylesheet type="text/xsl" href="Employees.xsl"?> <Employees> <Employee> <First>James</First> <Last>Borg</Last> </Employee> <Employee> <First>Joyce</First> <Last>English</Last> </Employee> </Employees> ![]() |
Employees.xsl
<xsl:stylesheet version="2.0"
xmlns:xsl=
"http://www.w3.org/1999/XSL/Transform" >
<xsl:template match="/">
<TABLE BORDER="1">
<TR> <TD>First</TD> <TD>Last</TD> </TR>
<xsl:apply-templates/>
</TABLE>
</xsl:template>
<xsl:template match="Employees">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="Employee">
<TR>
<TD><xsl:value-of select="First"/></TD>
<TD><xsl:value-of select="Last"/></TD>
</TR>
</xsl:template>
</xsl:stylesheet>
|
XSL Templates - XSL can define a template to specify the style for displaying each XML element. In the Employees.xsl file, templates are defined for all elements except <First> and <Last>.
The effect of apply-templates is to visit the tree nodes in depth-first order, although the results are not easily apparent in the above example. However, for the Family tree at right, the order is more obviously depth-first.
The order of depth-first visits to the 14 nodes would be:
Exercise 5 - XSL Templates
|
| <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > <xsl:template match="/"> <TABLE BORDER="1"> <TR> <TD>First</TD> <TD>Last</TD> </TR> <xsl:apply-templates/> </TABLE> </xsl:template> <xsl:template match="Employees"> <xsl:apply-templates/> </xsl:template> <xsl:template match="Employee"> <TR> <xsl:apply-templates/> </TR> </xsl:template> <xsl:template match="First"> <TD><xsl:value-of select="."/></TD> </xsl:template> <xsl:template match="Last"> |

In the tree at right, the children nodes of Employee are First and Last.
The First template could be defined as:
<xsl:template match="First">
<xsl:value-of select="."/>
</xsl:template>
which outputs the value of the First element (i.e. <xsl:value-of select="."/> where the "." is the current node.
From node Employee, the following walks both First and Last child nodes:
<xsl:template match="Employee">
<xsl:apply-templates/>
</xsl:template>
To walk only the First child node templates of Employee but not Last nodes use the select="First" in the apply-templates as follows:
<xsl:template match="Employee">
<xsl:apply-templates select="First"/>
</xsl:template>
To walk only the Employee/First child nodes of Employees:
<xsl:template match="Employees">
<xsl:apply-templates select="Employee/First"/>
</xsl:template>
Exercise 6 - XSL Templates
|
XSL is designed to combine HTML and XML data to construct Web pages dynamically. However, there is a fly-in-the-ointment: because XSL, XML and HTML all use some of the same special characters (e.g. <, >, ', ") the parser can be confused when we attempt to embed one in another.
Suppose we want XML elements to define the names of images that generate the following:
<img src='test1.jpg'>
In XML we could define an element, we'll call IMAGEFILE1, with a value of 'test1.jpg':
<IMAGEFILE1>test1.jpg</IMAGEFILE1>
In XSL we would like to write selects for using the value of IMAGEFILE1, something as below:
<img src='<xsl:value-of select="IMAGEFILE1" />'>
Unfortunately < and > have special meaning in XSL and HTML, the parser gets lost at the second < of: <img src='<xsl.
What is needed is a way to insert the value of the element, IMAGEFILE1, into HTML text.
The simplest approach is to avoid nesting <'s by defining a variable assigned the value of the element. For example:
<xsl:variable name="x" select="IMAGEFILE1"/>
assigns variable x the value test1.jpg (from the above XML
definition). The HTML text to display the image of test1.jpg can then be
generated by:
<img src='{$x}'/>
The rule is:
Example
The following shows XSL to produce valid HTML from XML data with inside and outside <> values.
Special.xsl
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:template match="/">
<table border="1">
<tr>
<td>
<xsl:variable name="x" select="Special/n1"/>
<img src='{$x}'/>
</td>
<td>
<xsl:variable name="z" select="Special/name"/>
<a href="http://www.google.com/search?q={$z}">
<xsl:value-of select="Special/name"/>
</a>
</td>
</tr>
</table>
</xsl:template>
</xsl:stylesheet>
|
Special.xml<?xml-stylesheet
type="text/xsl" href="Special.xsl"?>
<Special>
<n1>test1.jpg</n1>
<n2>test2.jpg</n2>
<email>jborg@ius.edu</email>
<name>John Borg</name>
</Special>
Resulting HTML |
Thanks to Ken Bell and Ryan Hughes for pointing out this method referenced at: http://www.w3.org/TR/xslt20/#attribute-value-templates
XSL is designed to combine HTML and XML data to construct Web pages dynamically. However, there is a fly-in-the-ointment: because XSL, XML and HTML all use some of the same special characters (e.g. <, >, ', ") the parser can be confused when we attempt to embed one in another.
Suppose we want XML elements to define the names of images that generate the following:
<img src='test1.jpg'>
In XML we could define an element, we'll call IMAGEFILE1, with a value of 'test1.jpg':
<IMAGEFILE1>test1.jpg</IMAGEFILE1>
In XSL we would like to write selects for using the value of IMAGEFILE1, something as below:
<img src='<xsl:value-of select="IMAGEFILE1" />'>
Unfortunately < and > have special meaning in XSL and HTML, the parser gets lost at the second < of: <img src='<xsl.
What is needed is a way to write ambigous HTML text such as < and > which is ignored by XSL parsing. We might try to use the < for < and > for >. The XSL would be:
<img src='<xsl:value-of select="IMAGEFILE1" />'>
but the result would be:
<img src='test1.jpg'>
A solution is the <xsl:text> </xsl:text> tag.
Whew!
The following shows XSL to produce valid HTML from XML data.
Special.xsl
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:template match="/">
<table border="1">
<tr>
<td>
<xsl:text disable-output-escaping="yes"><img src='</xsl:text>
<xsl:value-of select="Special/n1"/>
<xsl:text disable-output-escaping="yes">'></xsl:text>
</td>
<td>
<xsl:text disable-output-escaping="yes"><img src='</xsl:text>
<xsl:value-of select="Special/n2"/>
<xsl:text disable-output-escaping="yes">'></xsl:text>
</td>
</tr>
</table>
</xsl:template>
</xsl:stylesheet>
|
Special.xml<?xml-stylesheet
type="text/xsl" href="Special.xsl"?>
<Special>
<n1>test1.jpg</n1>
<n2>test2.jpg</n2>
<email>jborg@ius.edu</email>
<name>John Borg</name>
</Special>
Resulting HTML |
Exercise 7 - XSL Special Text
|