You are on page 1of 3

5/15/2019 XML Video Tutorial 2

001 <?xml version="1.0" encoding="UTF-8"?>


002 <xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
003
004 <!-- XSLT: eXtensible Style Language Used for styling XML -->
005
006 <!-- This sets the output type (Not Needed for HTML) -->
007
008 <xsl:output method="html" />
009
010 <!-- xsl:template : States that we want to match the whole document -
->
011 <!-- All of the rules that follow will apply to the root node -->
012
013 <xsl:template match="/">
014
015 <html>
016 <head><title>TV Shows</title></head>
017 <body>
018 <!-- Return the XSLT Version used -->
019
020 Version: <xsl:value-of select="system-
property('xsl:version')" /><br />
021
022 <!-- Return the XSLT Vendor used -->
023 <!-- libxml2 : Toolkit developed for the Gnome project --
>
024
025 Vendor: <xsl:value-of select="system-
property('xsl:vendor')" /><br />
026
027 Vendor URL: <xsl:value-of select="system-
property('xsl:vendor-url')" /><br />
028
029 <!-- Cycles through each node in the XML file -->
030
031 <xsl:for-each select="tvshow/show">
032
033 <!-- generate-id() returns a unique node identifier -
->
034
035 <a href="#{generate-id(name)}">
036
037 <!-- Get the value of name for the node -->
038
039 <xsl:value-of select="name" /></a><br />
040 </xsl:for-each>
041
042 <br />
043
044 <!-- Cycles through each node in the XML file -->
045
046 <xsl:for-each select="tvshow/show">
047
048 <!-- xsl:sort sorts based on your rules
049 select: node to base sort on
050 order: ascending (default) or descending
051 data-type: text or number -->
052
053 <xsl:sort select="name" order="ascending" data-
type="text" />
www.newthinktank.com/2012/05/xml-video-tutorial-2/ 1/3
5/15/2019 XML Video Tutorial 2

054
055 <!-- Print show name with a link to the list above --
>
056
057 <h3><a name="{generate-id(name)}">
058 <xsl:value-of select="name" /></a></h3>
059
060 <!-- Here I create an img tag and add attributes to
it -->
061
062 <img>
063
064 <!-- Select the attribute to change -->
065
066 <xsl:attribute name="src">
067
068 <!-- Set the value for the attribute -->
069 <!-- You grab the attribute of a node with @ -->
070
071 <xsl:value-of select="poster/@href" />
072 </xsl:attribute>
073 </img><br />
074
075 <!-- Returns the current node information -->
076
077 <xsl:value-of select="current()"/>
078
079 <p>The show <xsl:value-of select="name"/> was
released in
080 <xsl:value-of select="release"/> by
081 <xsl:value-of select="network"/>. It had an average
082 viewership of <xsl:value-of select="viewers"/>
million people.
083 It was cancelled in <xsl:value-of select="end_date"/>
084 </p><br />
085
086 </xsl:for-each>
087
088 <!-- Output Information in a table -->
089
090 <table border="2">
091 <tr><th>Name</th><th>Network</th><th>Viewers</th></tr>
092 <xsl:for-each select="tvshow/show">
093
094 <!-- Checks if the attribute of network is not equal to
UK -->
095
096 <!-- This will only allow non-UK shows on the list -->
097
098 <!-- You can use: = != > &lt; -->
099
100 <!-- <xsl:if test="network[@country='UK']"> -->
101
102 <!-- Shows results when release < 2006 -->
103
104 <xsl:if test="release &lt; 2006">
105
106 <tr>
107 <td><xsl:value-of select="name"/></td>
108 <td><xsl:value-of select="network"/></td>
109 <td><xsl:value-of select="viewers"/></td>
110 </tr>
www.newthinktank.com/2012/05/xml-video-tutorial-2/ 2/3
5/15/2019 XML Video Tutorial 2

111
112 </xsl:if>
113
114 <!-- xsl:choose is used to cover many conditions -->
115
116 <xsl:choose>
117
118 <!-- xsl:when defines under what condition to execute
-->
119
120 <xsl:when test="release > 2006">
121
122 <tr bgcolor="yellow">
123 <td><xsl:value-of select="name"/></td>
124 <td><xsl:value-of select="network"/></td>
125 <td><xsl:value-of select="viewers"/></td>
126 </tr>
127
128 </xsl:when>
129
130 <xsl:when test="release = 2006">
131
132 <tr bgcolor="orange">
133 <td><xsl:value-of select="name"/></td>
134 <td><xsl:value-of select="network"/></td>
135 <td><xsl:value-of select="viewers"/></td>
136 </tr>
137
138 </xsl:when>
139
140 <!-- If xsl:if was removed this would be the default
action for xsl:choose -->
141
142 <xsl:otherwise>
143
144 <tr bgcolor="pink">
145 <td><xsl:value-of select="name"/></td>
146 <td><xsl:value-of select="network"/></td>
147 <td><xsl:value-of select="viewers"/></td>
148 </tr>
149
150 </xsl:otherwise>
151
152 </xsl:choose>
153
154 </xsl:for-each>
155 </table>
156
157 </body>
158 </html>
159
160 </xsl:template>
161
162 </xsl:stylesheet>

www.newthinktank.com/2012/05/xml-video-tutorial-2/ 3/3

You might also like