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 independent 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 all 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. For example the following defines XML for Employees. 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 examine this in detail later in the
course.
<?xml version="1.0"?>
<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 version="1.0"?>
<?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="/">
<HTML>
<BODY>
<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>
</BODY>
</HTML>
</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. And does not give any debugging information. The msxsl program will display the resulting HTML by:
|
Employees.xml file defines a 4 level hierarchy of elements:
Employees.xml
<?xml version="1.0"?>
<?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="/">
<HTML>
<BODY>
<TABLE BORDER="1">
<TR>
<TD>First</TD>
<TD>Last</TD>
</TR>
<xsl:apply-templates/>
</TABLE>
</BODY>
</HTML>
</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 is to visit the tree nodes in depth-first order, although the results are not apparent in this example. However, for the Family tree, 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="/"> <HTML> <BODY> <TABLE BORDER="1"> <TR> <TD>First</TD> <TD>Last</TD> </TR> <xsl:apply-templates/> </TABLE> </BODY> </HTML> </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.
XSL can generate any HTML from XML but it takes a little effort with the < and > characters. Suppose we want to have XML elements define the names of images to generate the following:
The XML would define an IMAGEFILE1 element with a value of "test1.jpg". In XSL we would like to write selects for IMAGEFILE1, something as below:
<img src='<xsl:value-of select="IMAGEFILE1" />'>
Unfortunately < and > have special meaning in XSL, the parser gets lost at the second < of: <img src='<xsl.
What is needed is a way to insert text such as < and > which is ignored by XSL. One way might be 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'>
The solution is the <xsl:text> </xsl:text> tag which states that the between text should be ignored by XSL, and using the < and > and telling XSL to convert the < to < and > to >. The <xsl:text disable-output-escaping="yes">'></xsl:text> converts the > to >. Whew!
The following shows the correct XSL to produce the above from the XML.
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 version="1.0"?>
<?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
|