The following example will be referred to, and added to, throughout this section. The Finished XSLT Example Using Variables appears at the end of the section.
XML File (weatherlog.xml)
The following XML file contains weather information for various locations, over a period of time. The locations are initially assigned some generic names ("location1", "location2", etc.). Later, we will use XSLT variables to replace these names with more specific locations, such as "Airport" or "Midtown".
<?xml version="1.0" ?> <?xml-stylesheet type="text/xsl" href="weatherlogv1.xsl"?> <weather temp_scale="F"> <day date="2000-11-01"> <locale place="location1"> <temp> <low>47</low> <high>62</high> </temp> <wind_dir>NNE</wind_dir> <wind_speed units="mph">5</wind_speed> <barom units="mb">1013.2</barom> <rel_humid>.74</rel_humid> <precip units="in">0</precip> </locale> <locale place="location2"> <temp> <low>50</low> <high>64</high> </temp> <wind_dir>NNE</wind_dir> <wind_speed units="mph">4</wind_speed> <barom units="mb">1015.7</barom> <rel_humid>.70</rel_humid> <precip units="in">0</precip> </locale> <locale place="location3"> <temp> <low>48</low> <high>61</high> </temp> <wind_dir>NNE</wind_dir> <wind_speed units="mph">8</wind_speed> <barom units="mb">1010.0</barom> <rel_humid>.68</rel_humid> <precip units="in">.1</precip> </locale> </day> <day date="2000-11-02"> <locale place="location1"> <temp> <low>43</low> <high>58</high> </temp> <wind_dir>NE</wind_dir> <wind_speed units="mph">1</wind_speed> <barom units="mb">1009.1</barom> <rel_humid>.80</rel_humid> <precip units="in">2.3</precip> </locale> <locale place="location3"> <temp> <low>40</low> <high>61</high> </temp> <wind_dir>N</wind_dir> <wind_speed units="mph">12</wind_speed> <barom units="mb">1011.0</barom> <rel_humid>.73</rel_humid> <precip units="in">2.0</precip> </locale> </day> </weather>
XSLT File (weatherlogv1.xsl)
<?xml version='1.0'?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/TR/REC-html40"> <xsl:template match="/"> <HTML> <HEAD> <TITLE>Weather Readings</TITLE> </HEAD> <BODY> <xsl:apply-templates/> </BODY> </HTML> </xsl:template> <!-- Override built-in template rule for text nodes. --> <xsl:template match="text()"/> <xsl:template match="weather"> <H1>Weather Readings</H1> <xsl:for-each select="day"> <H2>As of <xsl:value-of select="@date"/></H2> <xsl:for-each select="locale"> <H3><xsl:value-of select="@place"/></H3> </xsl:for-each> </xsl:for-each> </xsl:template> </xsl:stylesheet>
Formatted Output
The resulting table shows the date and the locations where readings were taken.
Processor Output
Line breaks have been added for clarity.
<?xml version="1.0" encoding="UTF-16"?> <HTML xmlns="http://www.w3.org/TR/REC-html40"> <HEAD><TITLE>Weather Readings</TITLE></HEAD> <BODY> <H1>Weather Readings</H1> <H2>As of 2000-11-01</H2> <H3>location1</H3> <H3>location2</H3> <H3>location3</H3> <H2>As of 2000-11-02</H2> <H3>location1</H3> <H3>location3</H3> </BODY></HTML>
Finished XSLT Example Using Variables