Jump to content

XML - Managing Data Exchange/C.4

From Wikibooks, open books for an open world

XML Style Sheet

[edit | edit source]
<?xml version="1.0" encoding="UTF-8" ?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">

<HTML>
<HEAD>
<TITLE>Tour Guide</TITLE>
<STYLE TYPE="text/css">
H2 {TEXT-ALIGN:CENTER;}
.greenBackground {BACKGROUND-COLOR:LIGHTGREEN; TEXT-ALIGN:CENTER;}
.yellowBackground {BACKGROUND-COLOR:YELLOW; TEXT-ALIGN:CENTER; FONT-WEIGHT:BOLD; FONT-SIZE:14pt;}
.salmonBackground {BACKGROUND-COLOR:LIGHTSALMON; TEXT-ALIGN:CENTER; FONT-SIZE:12pt;}
</STYLE>
</HEAD>
<BODY>
<H2>Top Tourist Destinations</H2>
<xsl:apply-templates select="tourGuide"/>
</BODY>
</HTML>

</xsl:template>
<xsl:template match="tourGuide">

<TABLE BORDER="1" WIDTH="100%">
<xsl:for-each select="city/country">
<xsl:sort select="countryName"/>
<xsl:if test="population &gt; 10000">
<TR>
<TD CLASS="greenBackground"><BR/>
<xsl:text>Country: </xsl:text><xsl:value-of select="countryName"/></TD><BR/>
<TD CLASS="greenBackground"><BR/>
<xsl:text>Population: </xsl:text><xsl:value-of select="population"/>

</TD>
<xsl:for-each select="topDestination">
<TD CLASS="yellowBackground"><BR/>
<xsl:text>Top Destination: </xsl:text><xsl:value-of select="destinationName"/>

</TD>
</xsl:for-each>
<xsl:for-each select="destination">
<TD CLASS="yellowBackground"><BR/>
<xsl:text>Destination: </xsl:text><xsl:value-of select="destinationName"/>
</TD>
</xsl:for-each>
</TR>
</xsl:if>
</xsl:for-each>
</TABLE>
</xsl:template>

</xsl:stylesheet>


Table 4-3: XML style sheet for a one-to-one relationship – country_dest.xsl

The XML style sheet in Table 4-3 introduces the if statement as well as the ability to sort elements.
The if statement executes code based on the evaluation of the condition given in the "test" attribute,e.g.

<xsl:if test="population &gt;= 10000">

Big Town

</xsl:if>
<xsl:if test="population &lt; 10000">

Small Town

</xsl:if>

Possible expressions for the condition are

Operator

Meaning

=equality of two values
!=inequality of two values
&lt;less than
&gt;greater than
&lt;=less than or equal
&gt;=greater than or equal
andlogical and-conjunction
orlogical or-conjunction


Output can be sorted based on the value of the select attribute of the <xsl:sort> element.