<?xml version="1.0"?>
<?xml-stylesheet type="text/xml" href="presenter.xsl"?>
<slideshow xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<title>XSLT</title>
	<slide>
		<title>XSLT</title>
		<body>
			<p>By Karl Voelker</p>
		</body>
	</slide>
	<slide>
		<title>Agenda</title>
		<body>
			<ol>
				<li class='cur'>Introduction to XSLT and XML</li>
				<li class='after'>Basic XSLT Syntax</li>
				<li class='after'>Clever Algorithms
					<ol>
						<li class='after'>Row Shading</li>
						<li class='after'>Finding Primes</li>
						<li class='after'>Fibonacci Generation</li>
					</ol>
				</li>
				<li class='after'>Practical XSLT</li>
			</ol>
		</body>
	</slide>
	<slide>
		<title>What is XSLT?</title>
		<body>
			<ul>
				<li>A programming language</li>
				<li>A way to convert XML documents to other formats
					<ul>
						<li>Usually, to other XML formats</li>
					</ul>
				</li>
			</ul>
		</body>
	</slide>
	<slide>
		<title>An XML Document is a Tree</title>
		<body>
			<img src="images/custom/xml_tree.png" 
				alt="A graphical representation of an XML tree" />
			<code><include href="include/tree_intro.xml" mode="show"/></code>
		</body>
	</slide>
	<slide>
		<title>XML Elements</title>
		<body>
			<ul>
				<li>"Element" has a specific meaning in XML.</li>
				<li>An element has a <em>name</em>.</li>
				<li>An element may have <em>attributes</em>.</li>
				<li>An element may have child <em>nodes</em>.</li>
				<li>The textual form of an element's name and attributes is 
					a <em>tag</em>.</li>
			</ul>
			<code><show-xml><name attribute-name="attribute-value">
	<child-element/>
</name></show-xml></code>
		</body>
	</slide>
	<slide>
		<title>Other Node Types</title>
		<body>
			<p>Besides elements, nodes may be:</p>
			<dl>
				<dt>Text: </dt>
				<dd><tt><show-xml>A text node</show-xml></tt></dd>
				<dt>Comments (usually ignored): </dt>
				<dd><tt><show-xml><!--comment--></show-xml></tt></dd>
				<dt>Processing instructions (rare): </dt>
				<dd><tt><show-xml><?processing-instruction 1 2 3?></show-xml>
					</tt></dd>
			</dl>
		</body>
	</slide>
	<slide>
		<title>Agenda</title>
		<body>
			<ol>
				<li class='before'>Introduction to XSLT and XML</li>
				<li class='cur'>Basic XSLT Syntax</li>
				<li class='after'>Clever Algorithms
					<ol>
						<li class='after'>Row Shading</li>
						<li class='after'>Finding Primes</li>
						<li class='after'>Fibonacci Generation</li>
					</ol>
				</li>
				<li class='after'>Practical XSLT</li>
			</ol>
		</body>
	</slide>
	<slide>
		<title>How XSLT Works</title>
		<body>
			<ol>
				<li>Evaluate the Document Element, 
					producing the output of the transformation</li>
			</ol>
			<h3>Evaluating a Node</h3>
			<ol>
				<li>Find the template which best matches the node</li>
				<li>Evaluate that template on the node</li>
			</ol>
			<p><em>Why does this work?</em></p>
		</body>
	</slide>
	<slide>
		<title>Example 1: XSLT 1/2</title>
		<body>
			<code><include href="include/ex1.xsl" mode="show" /></code>
		</body>
	</slide>
	<slide>
		<title>Example 1: XSLT 2/2</title>
		<body>
			<code><include href="include/ex1_bbb.xsl" mode="show" /></code>
		</body>
	</slide>
	<slide>
		<title>Example 1: Input</title>
		<body>
			<code><include href="include/ex1_input.xml" mode="show" /></code>
		</body>
	</slide>
	<slide>
		<title>Example 1: Output</title>
		<body>
			<code><include href="include/ex1_output.xml" mode="show" /></code>
			<p><a href="include/ex1_input.xml">See the browser
				generate this!</a></p>
		</body>
	</slide>
	<slide>
		<title>How Templates Work</title>
		<body>
			<ul>
				<li>Everything in a template is evaluated, and turns into 
					part of the template's result.</li>
				<li><tt>xsl:</tt> elements turn into something special</li>
				<li>Anything else (like unknown elements or text) turns 
					into itself</li>
			</ul>
		</body>
	</slide>
	<slide>
		<title><tt>xsl:apply-templates</tt></title>
		<body>
			<ul>
				<li><tt>xsl:apply-templates</tt> evaluates to a concatenation 
					of the results from all the templates that it runs</li>
				<li>What templates does it run?
					<ul>
						<li>By default, it finds a template match for each 
							child node of the current node</li>
						<li>Otherwise, you can specify which nodes for which 
							to find template matches with the <tt>select</tt> 
							attribute</li>
					</ul>
				</li>
			</ul>
		</body>
	</slide>
	<slide>
		<title><tt>xsl:value-of</tt></title>
		<body>
			<ul>
				<li><tt>xsl:value-of</tt> has a <tt>select</tt> attribute</li>
				<li>The value of that attribute is what <tt>xsl:value-of</tt> 
					becomes</li>
			</ul>
			<p>Examples:</p>
			<ul>
				<li>
				<tt><show-xml><xsl:value-of select="3 + 3" /></show-xml></tt>
				</li>
				<li><tt>
				<show-xml><xsl:value-of select="../brother/nephew" /></show-xml>
				</tt></li>
			</ul>
		</body>
	</slide>
	<slide>
		<title>XPath</title>
		<body>
			<ul>
				<li>The expressions in <tt>match</tt> and <tt>select</tt> 
					attributes are XPath expressions</li>
				<li>XPath is a way to identify XML nodes</li>
				<li>Basic XPath is similar to UNIX file paths</li>
				<li>But, XPath can also do more</li>
			</ul>
			<p>Examples:</p>
			<ul>
				<li><tt>.</tt></li>
				<li><tt>../foo/bar</tt></li>
				<li><tt>preceding-sibling::*</tt></li>
				<li><tt>ancestor::*[@x]</tt></li>
			</ul>
		</body>
	</slide>
	<slide>
		<title><tt>xsl:if</tt></title>
		<body>
			<ul>
				<li>The contents of an <tt>xsl:if</tt> are only included if 
					the <tt>test</tt> attribute is a "true" XPath 
					expression</li>
				<li>For XPath, a node reference is true if the node exists</li>
			</ul>
			<p>Example:</p>
			<code><show-xml><xsl:if test="@x">
	<p>This paragraph appears if 
	the current node has an "x" attribute.</p>
</xsl:if></show-xml></code>
		</body>
	</slide>
	<slide>
		<title><tt>xsl:choose</tt></title>
		<body>
			<p><tt>xsl:choose</tt> is like <tt>if/else if/else</tt> 
				statements in many languages</p>
			<p>Example:</p>
			<code><show-xml><xsl:choose>
	<xsl:when test="@x > 9">
		X is big!
	</xsl:when>
	<xsl:when test="@x > 0">
		X is positive!
	</xsl:when>
	<xsl:otherwise>
		Poor X!
	</xsl:otherwise>
</xsl:choose></show-xml></code>
		</body>
	</slide>
	<slide>
		<title><tt>xsl:for-each</tt></title>
		<body>
			<ul>
				<li>Nodes are selected by the <tt>select</tt> attribute</li>
				<li>Contents of the <tt>xsl:for-each</tt> are evaluated 
					once for every node selected</li>
			</ul>
			<code><show-xml><xsl:for-each select="b[@x]">
	<p>
		<!-- Note that the current node 
	is different inside xsl:for-each -->
		<xsl:value-of select="@x" />
	</p>
</xsl:for-each></show-xml></code>
		</body>
	</slide>
	<slide>
		<title><tt>xsl:sort</tt></title>
		<body>
			<p><tt>xsl:sort</tt> changes the order used by 
				<tt>xsl:for-each</tt></p>
			<p>Example:</p>
			<code><show-xml><ul>
	<xsl:for-each select="beings/human">
		<xsl:sort select="@age" />
		<li>
			<xsl:value-of select="@age"/>
			<xsl:text>:</xsl:text>
			<xsl:value-of select="@name"/>
		</li>
	</xsl:for-each>
</ul></show-xml></code>
		</body>
	</slide>
	<slide>
		<title>Agenda</title>
		<body>
			<ol>
				<li class='before'>Introduction to XSLT and XML</li>
				<li class='before'>Basic XSLT Syntax</li>
				<li class='cur'>Clever Algorithms
					<ol>
						<li class='after'>Row Shading</li>
						<li class='after'>Finding Primes</li>
						<li class='after'>Fibonacci Generation</li>
					</ol>
				</li>
				<li class='after'>Practical XSLT</li>
			</ol>
		</body>
	</slide>
	<slide>
		<title>Named Templates</title>
		<body>
			<ul>
				<li>A template may have a <tt>name</tt> attribute</li>
				<li>A named template may be called directly with 
					<tt>xsl:call-template</tt></li>
				<li>Called templates may take arguments</li>
				<li>This allows implementing many clever algorithms!</li>
			</ul>
			<code><show-xml><xsl:call-template name="foo">
	<xsl:with-param name="a" select="@something"/>
	<xsl:with-param name="b" select="'something else'" />
</xsl:call-template></show-xml></code>
		</body>
	</slide>
	<slide>
		<title><tt>xsl:copy</tt></title>
		<body>
			<ul>
				<li><tt>xsl:copy</tt> copies the current node</li>
				<li>This template copies everything:</li>
			</ul>
			<code><show-xml><xsl:template match="node()|@*">
	<xsl:copy>
		<xsl:apply-templates select="node()|@*"/>
	</xsl:copy>
</xsl:template></show-xml></code>
		</body>
	</slide>
	<slide>
		<title>Agenda</title>
		<body>
			<ol>
				<li class='before'>Introduction to XSLT and XML</li>
				<li class='before'>Basic XSLT Syntax</li>
				<li class='cur'>Clever Algorithms
					<ol>
						<li class='cur'>Row Shading</li>
						<li class='after'>Finding Primes</li>
						<li class='after'>Fibonacci Generation</li>
					</ol>
				</li>
				<li class='after'>Practical XSLT</li>
			</ol>
		</body>
	</slide>
	<slide>
		<title>Table Row Shading: Input</title>
		<body>
			<code><include 
				href="include/row_shade_input.xml" mode="show" /></code>
		</body>
	</slide>
	<slide>
		<title>Table Row Shading: Considerations</title>
		<body>
			<ul>
				<li>We want to ignore most of the HTML - but leave it the same
				</li>
				<li>We want to mark all <show-xml><tr/></show-xml> as 
					"even" or "odd"</li>
			</ul>
		</body>
	</slide>
	<slide>
		<title>Table Row Shading: XSLT 1/3</title>
		<body>
			<code><include href="include/row_shade.xsl" mode="show" /></code>
		</body>
	</slide>
	<slide>
		<title>Table Row Shading: XSLT 2/3</title>
		<body>
			<code><include href="include/row_shade_row.xsl" mode="show" />
			</code>
		</body>
	</slide>
	<slide>
		<title>Table Row Shading: XSLT 3/3</title>
		<body>
			<code><include href="include/row_shade_col.xsl" mode="show" />
			</code>
		</body>
	</slide>
	<slide>
		<title>Table Row Shading: Output</title>
		<body>
			<code><include 
				href="include/row_shade_output.xml" mode="show" /></code>
		</body>
	</slide>
	<slide>
		<title>Agenda</title>
		<body>
			<ol>
				<li class='before'>Introduction to XSLT and XML</li>
				<li class='before'>Basic XSLT Syntax</li>
				<li class='cur'>Clever Algorithms
					<ol>
						<li class='before'>Row Shading</li>
						<li class='cur'>Finding Primes</li>
						<li class='after'>Fibonacci Generation</li>
					</ol>
				</li>
				<li class='after'>Practical XSLT</li>
			</ol>
		</body>
	</slide>
	<slide>
		<title>Finding Primes: Input</title>
		<body>
			<code><include 
				href="include/prime_input.xml" mode="show" /></code>
		</body>
	</slide>
	<slide>
		<title>Finding Primes: Considerations</title>
		<body>
			<ul>
				<li>We want to find all the primes in a range.</li>
				<li>We are going to use recursion!</li>
			</ul>
		</body>
	</slide>
	<slide>
		<title>Finding Primes: XSLT 1/3</title>
		<body>
			<code><include href="include/prime.xsl" mode="show" /></code>
		</body>
	</slide>
	<slide>
		<title>Finding Primes: XSLT 2/3</title>
		<body>
			<code><include href="include/prime_main.xsl" mode="show" /></code>
		</body>
	</slide>
	<slide>
		<title>Finding Primes: XSLT 3/3</title>
		<body>
			<code><include href="include/is_prime.xsl" mode="show" /></code>
		</body>
	</slide>
	<slide>
		<title>Finding Primes: Output</title>
		<body>
			<code><include 
				href="include/prime_output.xml" mode="show" /></code>
		</body>
	</slide>
	<slide>
		<title>Agenda</title>
		<body>
			<ol>
				<li class='before'>Introduction to XSLT and XML</li>
				<li class='before'>Basic XSLT Syntax</li>
				<li class='cur'>Clever Algorithms
					<ol>
						<li class='before'>Row Shading</li>
						<li class='before'>Finding Primes</li>
						<li class='cur'>Fibonacci Generation</li>
					</ol>
				</li>
				<li class='after'>Practical XSLT</li>
			</ol>
		</body>
	</slide>
	<slide>
		<title>Fibonacci Generation: Input</title>
		<body>
			<code><include href="include/fib_input.xml" mode="show" /></code>
		</body>
	</slide>
	<slide>
		<title>Fibonacci Generation: XSLT 1/2</title>
		<body>
			<code><include href="include/fib.xsl" mode="show" /></code>
		</body>
	</slide>
	<slide>
		<title>Fibonacci Generation: XSLT 2/2</title>
		<body>
			<code><include href="include/fibs.xsl" mode="show" /></code>
		</body>
	</slide>
	<slide>
		<title>Fibonacci Generation: Output</title>
		<body>
			<code><include href="include/fib_output.xml" mode="show" /></code>
		</body>
	</slide>
	<slide>
		<title>Agenda</title>
		<body>
			<ol>
				<li class='before'>Introduction to XSLT and XML</li>
				<li class='before'>Basic XSLT Syntax</li>
				<li class='before'>Clever Algorithms
					<ol>
						<li class='before'>Row Shading</li>
						<li class='before'>Finding Primes</li>
						<li class='before'>Fibonacci Generation</li>
					</ol>
				</li>
				<li class='cur'>Practical XSLT</li>
			</ol>
		</body>
	</slide>
	<slide>
		<title>Applying XSLT</title>
		<body>
			<p>To note that an XML document should be transformed by a 
				particular stylesheet:</p>
<code><show-xml><?xml-stylesheet type="text/xsl" href="URL"?></show-xml></code>
		</body>
	</slide>
	<slide>
		<title>Getting Other XML Documents</title>
		<body>
			<ol>
				<li>Fetch an XML document from a URL.</li>
				<li>Use it as input!</li>
			</ol>
			<p>This is a powerful technique.</p>
		</body>
	</slide>
	<slide>
		<title>Getting Other XML Documents</title>
		<body>
			<code><show-xml>
				<!-- This is just a snippet! -->
				<xsl:variable name="doc" select="document('foo.xml')" />
				<xsl:apply-templates select="$doc/*" />
			</show-xml></code>
		</body>
	</slide>
	<slide>
		<title><tt>xsl:include</tt></title>
		<body>
			<p>You can include templates from one stylesheet into another.</p>
			<code><show-xml>
				<xsl:include href="other.xsl" />
			</show-xml></code>
		</body>
	</slide>
	<slide>
		<title><tt>xsl:import</tt></title>
		<body>
			<ul>
				<li>Like <tt>xsl:include</tt>, but imported templates have 
					lower priority</li>
				<li>Imported templates will only be used if nothing else 
					matches!</li>
			</ul>
			<code><show-xml>
				<xsl:import href="defaults.xsl" />
			</show-xml></code>
		</body>
	</slide>
	<slide>
		<title>The End</title>
		<body>
			<p>Questions?</p>
		</body>
	</slide>
</slideshow>

