<?xml version="1.0" encoding="UTF-8"?>
<screenbook><title realName="A2 An XSLT Example">An XSLT Example</title><bookCopyright>copyright</bookCopyright><bookCopyrightYear>2007</bookCopyrightYear><bookCopyrightOwner>NYCircuits Inc</bookCopyrightOwner><soundFile></soundFile><slideSeconds>7</slideSeconds><category realName="SXSW 2007">SXSW 2007</category><subcategory realName="C2 XSLT">XSLT</subcategory><format realName="Book">Book</format><description></description><keywords></keywords><ldate>3/11/2007</ldate><version>draft</version><pages><page><text><span id="pagetext"><![CDATA[<P>This tutorial will present a very simple XSLT stylesheet example. </P>
<P>This tutorial is being created with a program called Screenbook Maker. Screenbook Maker creates an xml file for each tutorial. The tutorial will use this xml file as an example. </P>
<P>The xml file is <A href="sbook.xml" target=_blank>here</A>.</P>]]></span></text><picture>[Empty]</picture><title>The sbook.xml file</title><author>Joe Orr</author><picLabel>[Empty]</picLabel><keywords>[Empty]</keywords><sauthor>Joe Orr</sauthor><cp>All Rights Reserved</cp><authoremail>jorr@nycircuits.com</authoremail></page><page><text><span id="pagetext"><![CDATA[<P>First we create the the stylesheet element and the root element. </P>
<P>&lt;?xml version="1.0"?&gt;<BR/><BR/><FONT face="Courier New"><STRONG>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;<BR/><BR/>&#160; &lt;xsl:template match="/"&gt;<BR/>&#160;&#160;&#160; Hello World<BR/>&#160; &lt;/xsl:template&gt;</STRONG></FONT></P>
<P><FONT face="Courier New"><STRONG>&lt;/xsl:stylesheet&gt;</STRONG></FONT></P>
<P>&#160;</P>]]></span></text><picture>[Empty]</picture><title>Root element</title><author>Joe Orr</author><picLabel>[Empty]</picLabel><keywords>[Empty]</keywords><sauthor>Joe Orr</sauthor><cp>All Rights Reserved</cp><authoremail>jorr@nycircuits.com</authoremail></page><page><text><span id="pagetext"><![CDATA[<P>We save the preceding stylesheet into a file named example.xsl, and use the Xalan XSLT processor to transform the input sbook.xml file into an output file named sample.html</P>
<P>We will use the command line version of the Xalan XSLT processor, available from xml.apache.org.</P>
<P>The syntax for the transformation is </P>
<P><CODE><FONT face="courier, monospaced">java org.apache.xalan.xslt.Process -in <I>xmlSource</I></FONT></CODE><BR/><CODE><FONT face="courier, monospaced">&#160;&#160;&#160;&#160;-xsl <I>stylesheet</I> -out <I>outputfile</I></FONT></CODE></P>]]></span></text><picture width="640" height="480" ldate="" id="pagepic" dwidth="800" dheight="600">pic03.png</picture><title>Save and transform</title><author>Joe Orr</author><picLabel>[Empty]</picLabel><keywords>[Empty]</keywords><sauthor>Joe Orr</sauthor><cp>All Rights Reserved</cp><authoremail>jorr@nycircuits.com</authoremail></page><page><text><span id="pagetext"><![CDATA[Here's what our output file looks like in Internet Explorer:]]></span></text><picture width="640" height="480" ldate="" id="pagepic" dwidth="800" dheight="600">pic04.png</picture><title>The output</title><author>Joe Orr</author><picLabel>[Empty]</picLabel><keywords>[Empty]</keywords><sauthor>Joe Orr</sauthor><cp>All Rights Reserved</cp><authoremail>jorr@nycircuits.com</authoremail></page><page><text><span id="pagetext"><![CDATA[<P>The xslt processor starts at the root of the xml document and proceeds down left to right. It visits each element of the xml input and then checks for a match in the stylesheet. If it finds a match, it prints out the text inside the matching template. </P>
<P>So, the processor found a matching template for the root. It then printed out the contents of that template and was done.</P>]]></span></text><picture>[Empty]</picture><title>The processor</title><author>Joe Orr</author><picLabel>[Empty]</picLabel><keywords>[Empty]</keywords><sauthor>Joe Orr</sauthor><cp>All Rights Reserved</cp><authoremail>jorr@nycircuits.com</authoremail></page><page><text><span id="pagetext"><![CDATA[<P>Let's change the root template to go back to the input file and keep looking for matches:</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="Courier New"><STRONG>&lt;xsl:template match="/"&gt;<BR/>&#160; &lt;xsl:apply-templates/&gt;<BR/>&lt;/xsl:template&gt;</STRONG></FONT></P></BLOCKQUOTE>]]></span></text><picture>[Empty]</picture><title>Apply templates</title><author>Joe Orr</author><picLabel>[Empty]</picLabel><keywords>[Empty]</keywords><sauthor>Joe Orr</sauthor><cp>All Rights Reserved</cp><authoremail>jorr@nycircuits.com</authoremail></page><page><text><span id="pagetext"><![CDATA[<P>Now let's add a template to match the page element in the sbook.xml file.</P>
<BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px">
<P><FONT face="Courier New"><STRONG>&lt;xsl:template match="page"&gt;<BR/>&#160; &lt;xsl:value-of select="text" disable-output-escaping="yes"/&gt;<BR/>&#160; &lt;hr/&gt;<BR/>&lt;/xsl:template&gt;</STRONG></FONT></P></BLOCKQUOTE>]]></span></text><picture>[Empty]</picture><title>Template for page element</title><author>Joe Orr</author><picLabel>[Empty]</picLabel><keywords>[Empty]</keywords><sauthor>Joe Orr</sauthor><cp>All Rights Reserved</cp><authoremail>jorr@nycircuits.com</authoremail></page><page><text><span id="pagetext"><![CDATA[Here is what the generated HTML file looks like now. The XSLT processor found a match for the page element each time it encountered it in the input file. So each page element has been echoed to the output HTML file.]]></span></text><picture width="640" height="480" ldate="" id="pagepic" dwidth="800" dheight="600">pic08.png</picture><title>The new output</title><author>Joe Orr</author><picLabel>[Empty]</picLabel><keywords>[Empty]</keywords><sauthor>Joe Orr</sauthor><cp>All Rights Reserved</cp><authoremail>jorr@nycircuits.com</authoremail></page><page><text><span id="pagetext"><![CDATA[The complete sample xslt file is <a href="example.xsl" target="_blank">here</a>.]]></span></text><picture>[Empty]</picture><title>Complete sample xslt</title><author>Joe Orr</author><picLabel>[Empty]</picLabel><keywords>[Empty]</keywords><sauthor>Joe Orr</sauthor><cp>All Rights Reserved</cp><authoremail>jorr@nycircuits.com</authoremail></page></pages></screenbook>

