<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:series="http://unfoldingneurons.com/"
	>

<channel>
	<title>Eddie Welker.com &#187; xslt</title>
	<atom:link href="http://www.eddiewelker.com/category/xslt/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.eddiewelker.com</link>
	<description>Hahaha.</description>
	<lastBuildDate>Thu, 01 Apr 2010 01:53:52 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>My XSLT Toolbox &#8211; 5 Favorite XSLT Books</title>
		<link>http://www.eddiewelker.com/2009/01/08/top-5-xslt-books/</link>
		<comments>http://www.eddiewelker.com/2009/01/08/top-5-xslt-books/#comments</comments>
		<pubDate>Fri, 09 Jan 2009 04:42:00 +0000</pubDate>
		<dc:creator>Eddie</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[xslt]]></category>
		<category><![CDATA[books]]></category>
		<category><![CDATA[xpath]]></category>

		<guid isPermaLink="false">http://www.eddiewelker.com/?p=214</guid>
		<description><![CDATA[I love reading programming books, especially to learn a new programming language.  Learning XSLT, I read a large number of books, as there are quite a few available.  The quality of the XSLT books struck me as particularly all over the place, some were quite good while others weren&#8217;t even worth the time to skim.  [...]]]></description>
			<content:encoded><![CDATA[<p>I love reading programming books, especially to learn a new programming language.  Learning XSLT, I read a large number of books, as there are quite a few available.  The quality of the XSLT books struck me as particularly all over the place, some were quite good while others weren&#8217;t even worth the time to skim.  So I&#8217;m throwing together a simple list of my current collection of XSLT references, which happened to be my favorites of the bunch.   These books are all geared towards specific audiences&#8230; beginners, advanced, etc, so I included their audiences.</p>
<ul>
<li><strong><a href="http://www.amazon.com/gp/product/0596000537?ie=UTF8&amp;tag=eddwelsblo-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0596000537">XSLT &#8211; Mastering XML Transformations</a>, Doug Tidwell</strong><br />
This is my favorite XSLT book.  Mr. Tidwell did a great job of combining an introduction to the language, a tutorial on how to write XSLTs, and a reference all into one book.  On top of that, I found it to be written in the clearest, most conversational style I&#8217;ve found in many a programming book.  I find this book covers 90% of my day-to-day needs, and when I forget how something works, this book usually answers my questions.  (Plus, hey, you can get the 1.0 version for about $3 used.)</li>
<li><strong><a href="http://www.amazon.com/gp/product/0470192747?ie=UTF8&amp;tag=eddwelsblo-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0470192747">XSLT: Programmer&#8217;s Reference</a>, Michael Kay</strong><br />
If Mr. Tidwell&#8217;s book covers 90%, this book covers all 100%, and then some.  Mr. Kay (who wrote the Saxon processor, if you weren&#8217;t aware) presents what amounts to an annotated specification in book form.  One of my co-workers calls this book the XSLT dictionary, and I can&#8217;t argue with that.  This book is probably best for advanced programmers.</li>
<li><strong><a href="http://www.amazon.com/gp/product/0764547763?ie=UTF8&amp;tag=eddwelsblo-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0764547763">XSLT and XPATH on the Edge</a>, Jeni Tennison</strong><br />
Once you&#8217;ve got the basics of the language down, you&#8217;ve got to use it to write real-world code.  I found this book helps to smooth down the rough edges of working with the language.  This book requires a mid-level familiarity with the language.</li>
<li><strong><a href="http://www.amazon.com/gp/product/0596009747?ie=UTF8&amp;tag=eddwelsblo-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0596009747">XSLT Cookbook, Second Edition</a>, Salvatore Mangano</strong><br />
I reach for this book whenever I&#8217;ve got to do something weird.  I use it to find the solution to some odd edge case, or for my &#8220;can I do this with XSLT&#8221; questions.  The book covers everything from faking regular expressions, to set operations on different node-sets, to functional programming with XSLT.  I don&#8217;t use it often, but it&#8217;s like gold when I do.  This book is mostly for advanced users.</li>
<li><strong><a href="http://www.amazon.com/gp/product/0596002912?ie=UTF8&amp;tag=eddwelsblo-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0596002912">XPath and XPointer</a>, John E. Simpson</strong><br />
The content in this book is totally covered in each of the other books, and it isn&#8217;t really XSLT, because it only covers XPath.  But this book is my simple reference to 90% of the XPath questions I have.  It is a nice little book that I could live without, but it certainly makes my life easier having it around.  I think new users will likely get the most out of this book. (Another book that can be had for about $3 used.)</li>
</ul>
<p>(For disclosure, I did make the links amazon referrals. I feel kinda weird, but figured why not. I don&#8217;t expect any results, but if I got some, it&#8217;d go straight to buying a new book.)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.eddiewelker.com/2009/01/08/top-5-xslt-books/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>My XSLT Toolbox &#8211; Recursive XSLT templates</title>
		<link>http://www.eddiewelker.com/2008/12/28/recursive-xslt-templates/</link>
		<comments>http://www.eddiewelker.com/2008/12/28/recursive-xslt-templates/#comments</comments>
		<pubDate>Sun, 28 Dec 2008 21:21:35 +0000</pubDate>
		<dc:creator>Eddie</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[xslt]]></category>
		<category><![CDATA[exslt]]></category>
		<category><![CDATA[recursive]]></category>
		<category><![CDATA[templates]]></category>
		<category><![CDATA[tokenize]]></category>
		<category><![CDATA[xpath]]></category>

		<guid isPermaLink="false">http://www.eddiewelker.com/?p=199</guid>
		<description><![CDATA[Recursion is one of the core concepts in programming.  It&#8217;s valuable not only as a technique for writing programs, but as a general concept for solving problems.  XSLT provides many useful elements such as for-each (and apply-templates), but occasionally you will run into a problem which must be solved with recursion.  Let&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>Recursion is one of the core concepts in programming.  It&#8217;s valuable not only as a technique for writing programs, but as a general concept for solving problems.  XSLT provides many useful elements such as for-each (and apply-templates), but occasionally you will run into a problem which must be solved with recursion.  Let&#8217;s take a look at a real-world (no Fibonacci!!) example, where we have to operate on a simple string of numbers separated by commas.  We&#8217;ll take a step-by-step approach to writing a recursive template. </p>
<p>Let&#8217;s say we have the following source document, short and sweet.  We want to take each number, and wrap it with an element.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;comma<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1,2,3,4,5,6,7,88,99,100<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/comma<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The easy way to do this is to use the <a href="http://www.exslt.org/str/functions/tokenize/">EXSLT str:tokenize function</a>, which takes a string and some delimiters and splits the string based on those delimiters. All we do is add the xmlns:str and extension-element-prefixes attributes to our xsl:stylesheet declaration, and then call the str:tokenize function.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:stylesheet</span> <span style="color: #000066;">xmlns:xsl</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/1999/XSL/Transform&quot;</span> </span>
<span style="color: #009900;"><span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">xmlns:str</span>=<span style="color: #ff0000;">&quot;http://exslt.org/strings&quot;</span> </span>
<span style="color: #009900;"><span style="color: #000066;">extension-element-prefixes</span>=<span style="color: #ff0000;">&quot;str&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">match</span>=<span style="color: #ff0000;">&quot;/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:for-each</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;str:tokenize( comma, ',')&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:copy-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;.&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:for-each<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:stylesheet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The result is, (new-lines added for readability):</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;token<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/token<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;token<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/token<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;token<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>3<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/token<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;token<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>4<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/token<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;token<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>5<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/token<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;token<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>6<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/token<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;token<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>7<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/token<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;token<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>88<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/token<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;token<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>99<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/token<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;token<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>100<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/token<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Excellent. But let&#8217;s say that we don&#8217;t have access to the EXSLT functions, and we have to write a template to perform the same thing.  </p>
<p>So now we think up a recursive algorithm.  Let&#8217;s look at a simplified list with three numbers, such as &#8220;1,2,3&#8243;.  First, we print the &#8220;1&#8243;, the value before the first comma, and then we discard the first comma.  At that point, our list will be &#8220;2,3&#8243; and we repeat, printing the new first value, and discarding the new first comma. Finally, the list becomes only &#8220;3&#8243;.  There is no comma, so we simply print out the rest of the list, &#8220;3&#8243;.  So we will be recursing over the string printing the first number, and then popping off the first number and first comma.  This technique will work with a three number list, or a million-number list (though your processor will probably run out of memory before that).</p>
<p>XPath&#8217;s &#8220;substring-before&#8221;, &#8220;substring-after&#8221;, and &#8220;contains&#8221; functions are all of the tools that we&#8217;ll need to implement our algorithm.  &#8220;substring-before&#8221; lets us obtain the number before the first comma. &#8220;substring-after&#8221; lets us discard the first number and first comma, and &#8220;contains&#8221; allows us figure out the last, comma-less case.</p>
<p>Our function starts in the same manner as all recursive functions, dealing with the last case, and then all of the cases before it.  The last case will be the comma-less case from our algorithm. So here&#8217;s our template skeleton.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;splitcommas&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:choose<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:when</span> <span style="color: #000066;">test</span>=<span style="color: #ff0000;">&quot; not(contains($comma, ','))&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                <span style="color: #808080; font-style: italic;">&lt;!-- do something --&gt;</span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:when<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:otherwise<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #808080; font-style: italic;">&lt;!-- do something --&gt;</span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:otherwise<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:choose<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>We use the XPath &#8220;not(contains($comma, &#8216;,&#8217;))&#8221; to, obviously, test whether our list contains a comma.  As you probably noticed, we don&#8217;t actually have a $comma variable defined, so we&#8217;ll add our xsl:param to the template.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;splitcommas&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;comma&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:choose<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:when</span> <span style="color: #000066;">test</span>=<span style="color: #ff0000;">&quot; not(contains($comma, ','))&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                <span style="color: #808080; font-style: italic;">&lt;!-- do something --&gt;</span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:when<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:otherwise<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #808080; font-style: italic;">&lt;!-- do something --&gt;</span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:otherwise<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:choose<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Now we fill in the holes in the template&#8217;s framework.  First, we&#8217;ll handle the comma-less case, simply outputting the value wrapped in a &#8220;value&#8221; element.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;xsl:value-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;$comma&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Then we will deal with the otherwise case.  We&#8217;ll first print out the number before the first comma in the same manner that we printed the comma-less case.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;xsl:value-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;substring-before($comma, ',')&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Then we&#8217;ll perform the same operation on the list after discarding the first number and first comma.  We&#8217;ll call the template again, passing everything after the first comma (thus excluding both the first number and the first comma).</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:call-template</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;splitcommas&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:with-param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;comma&quot;</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;substring-after($comma, ',')&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:call-template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Finally, we&#8217;ll put all of these parts together, as well as adding a root template that will call our &#8220;splitcommas&#8221; template.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:stylesheet</span> <span style="color: #000066;">xmlns:xsl</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/1999/XSL/Transform&quot;</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">match</span>=<span style="color: #ff0000;">&quot;/&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:call-template</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;splitcommas&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:with-param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;comma&quot;</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;comma&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:call-template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;splitcommas&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;comma&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:choose<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:when</span> <span style="color: #000066;">test</span>=<span style="color: #ff0000;">&quot; not(contains($comma, ','))&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;xsl:value-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;$comma&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:when<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:otherwise<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;xsl:value-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;substring-before($comma, ',')&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:call-template</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;splitcommas&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:with-param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;comma&quot;</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;substring-after($comma, ',')&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:call-template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:otherwise<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:choose<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:stylesheet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>When applied to our source document, we get exactly what we wanted, each number of the string, enclosed in a <value> element (I added new-lines for readability).</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>3<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>4<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>5<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>6<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>7<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>88<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>99<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>100<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>So we accomplished what we were looking to accomplish by writing a recursive template, however our template only deals with commas.  What if our list was separated by spaces instead of commas?  We&#8217;d have to either write a new template that deals with spaces, or, better yet, modify our template to be more generic and handle many cases.  I invite you to take a look at the <a href="http://www.exslt.org/str/functions/tokenize/str.tokenize.template.xsl">actual implementation of the str:tokenize  function</a>.  It isn&#8217;t that much more complicated, but does contain a few interesting wrinkles.</p>
<p>If you&#8217;re interested in the basics of recursion in general, I highly recommend <a href="http://www.amazon.com/gp/product/0262560992?ie=UTF8&#038;tag=eddwelsblo-20&#038;linkCode=as2&#038;camp=1789&#038;creative=390957&#038;creativeASIN=0262560992"><i>The Little Schemer</i></a> which is the most outstanding book I&#8217;ve read on the subject.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.eddiewelker.com/2008/12/28/recursive-xslt-templates/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>My XSLT Toolbox &#8211; copy and copy-of</title>
		<link>http://www.eddiewelker.com/2008/12/27/xslt-copy-copy-of/</link>
		<comments>http://www.eddiewelker.com/2008/12/27/xslt-copy-copy-of/#comments</comments>
		<pubDate>Sat, 27 Dec 2008 20:05:09 +0000</pubDate>
		<dc:creator>Eddie</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[xslt]]></category>
		<category><![CDATA[copy]]></category>
		<category><![CDATA[copy-of]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.eddiewelker.com/?p=184</guid>
		<description><![CDATA[Using XSLT to copy elements is extremely common when you&#8217;re transforming a source document of a certain type (XML, HTML, etc.) to the same type.  Often, you need an exact copy of an element verbatim, but other times you need to selectively choose certain elements to copy and others to discard.  XSLT makes [...]]]></description>
			<content:encoded><![CDATA[<p>Using XSLT to copy elements is extremely common when you&#8217;re transforming a source document of a certain type (XML, HTML, etc.) to the same type.  Often, you need an exact copy of an element verbatim, but other times you need to selectively choose certain elements to copy and others to discard.  XSLT makes this process quite elegant using it&#8217;s xsl:copy-of and xsl:copy elements.  The following is a setp-by-step tutorial on how these elements are used.</p>
<p>When you need an exact copy of an element and it&#8217;s children, you use the xsl:copy-of element, which makes an exact copy of the selected element and it&#8217;s children.  Given the following XML data, which represents a (trivial) inventory of a store, let&#8217;s say you want an exact copy of any items with the name &#8220;XSLT&#8221;.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;inventory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;1&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>The Little Schemer<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>book<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Friedman<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Felleisen<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;list-price<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>29.95<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/list-price<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;sell-price<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>26.99<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/sell-price<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cost<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>17.92<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/cost<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;2&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>XSLT<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>book<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Tidwell<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;list-price<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>49.95<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/list-price<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;sell-price<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>34.99<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/sell-price<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cost<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>22.92<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/cost<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;3&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Romeo and Juliet<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>compact disc<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;conductor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Rostropovich<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/conductor<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;list-price<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>18.98<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/list-price<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;sell-price<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>13.99<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/sell-price<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cost<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>9.92<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/cost<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/inventory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>You simply apply the following XSLT stylesheet to your source document:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:stylesheet</span> <span style="color: #000066;">xmlns:xsl</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/1999/XSL/Transform&quot;</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">match</span>=<span style="color: #ff0000;">&quot;/&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:copy-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;inventory/item[name = 'XSLT']&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:stylesheet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Which gives you exactly what you were looking for, the &#8220;item&#8221; with the name &#8220;XSLT&#8221;.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;2&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>XSLT<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>book<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Tidwell<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;list-price<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>49.95<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/list-price<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;sell-price<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>34.99<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/sell-price<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cost<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>22.92<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/cost<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>That was easy, so now let&#8217;s say you want to do a little more with your inventory document.  Your boss wants a copy of it to look at the numbers and do some accounting.  She doesn&#8217;t care about the authors or conductors, so she&#8217;d like that information left out.  Also, she would like an additional piece of information for each item, the amount of profit off each item sold, the difference between the sell-price and the cost.  </p>
<p>Because we are adding a piece of information and getting rid of elements that don&#8217;t affect the accounting, we can&#8217;t use a xsl:copy-of, because that would output an exact copy of the item element, it&#8217;s attribute nodes, and it&#8217;s child nodes. This exact copy is called a <i>deep copy</i>, because it not only copies the element, but all of it&#8217;s children as well.  The solution is to use xsl:copy which performs a <i>shallow copy</i>, which means it <b>only</b> copies the current node, and ignores all children or attribute nodes.</p>
<p>Since xsl:copy only copies one element at a time, you need to explicitly specify that you want to continue copying attribute nodes and child nodes.  xsl:apply-templates gives us the leverage to write a template that accomplishes that.  The following template starts by matching attribute and children nodes, then copies the node, and recursively applies itself to any attribute or child nodes found in the source tree.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!-- @* matches any attribute node on the current element,</span>
<span style="color: #808080; font-style: italic;">       node() matches any child nodes of the current element --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">match</span>=<span style="color: #ff0000;">&quot;@*|node()&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #808080; font-style: italic;">&lt;!-- shallow copy... </span>
<span style="color: #808080; font-style: italic;">          only copy the node you're on (be that attribute or child node) --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:copy<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #808080; font-style: italic;">&lt;!-- apply this template to any other attribute or child nodes found --&gt;</span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:apply-templates</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;@*|node()&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>  
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:copy<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Using this template, I can write a XSLT stylesheet that will copy the entire source document without any changes.  This isn&#8217;t quite what we were looking for, because we wanted to add and remove child elements of item, but this is the first step.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:stylesheet</span> <span style="color: #000066;">xmlns:xsl</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/1999/XSL/Transform&quot;</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">match</span>=<span style="color: #ff0000;">&quot;/&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:apply-templates</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- copy all attribute or child nodes in place --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">match</span>=<span style="color: #ff0000;">&quot;@*|node()&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:copy<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:apply-templates</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;@*|node()&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>  
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:copy<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:stylesheet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>So how do we add the profit made from each item and remove the unnecessary information?  We use the fact that our xsl:match=&#8221;@*|node()&#8221; template has a very low priority.  Determination of default XSLT priorities are an advanced topic I won&#8217;t go into right now, but feel free to <a href="http://www.w3.org/TR/xslt#conflict">explore the topic</a> if <a href="http://www.dpawson.co.uk/xsl/sect2/N7654.html#d11689e235">you are interested</a>.  Our template is essentially given a priority of -.5.  Templates such as xsl:template match=&#8221;foo&#8221;, however, are given a default priority of 0.  Because templates matching element names are higher priority, we can easily figure out how to remove the &#8220;author&#8221; and &#8220;conductor&#8221; elements, just declare templates without outputs!</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">match</span>=<span style="color: #ff0000;">&quot;author&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">match</span>=<span style="color: #ff0000;">&quot;conductor&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p>We use the same technique to add an element to our &#8220;item&#8221; elements.  First we use xsl:copy to copy the item node itself.  Then we apply-templates to any attribute or children nodes found.  When an author or conductor element is found, it will match our explicit rules and produce no output, therefore they will not be copied into our result.  Finally, we create a new element named &#8220;profit&#8221; which will contain the difference between the sell-price and the cost.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">match</span>=<span style="color: #ff0000;">&quot;item&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:copy<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:apply-templates</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;@*|node()&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;profit<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;xsl:value-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;(sell-price - cost)&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/profit<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:copy<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>So we arrive at our final XSLT stylesheet, which looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:stylesheet</span> <span style="color: #000066;">xmlns:xsl</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/1999/XSL/Transform&quot;</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">match</span>=<span style="color: #ff0000;">&quot;/&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:apply-templates</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- for each item, copy the &lt;item&gt; element, and apply-templates</span>
<span style="color: #808080; font-style: italic;">         to it's attributes and children. Finally, create a &lt;profit&gt; </span>
<span style="color: #808080; font-style: italic;">         element, the difference between the sell-price and the cost.  --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">match</span>=<span style="color: #ff0000;">&quot;item&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:copy<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:apply-templates</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;@*|node()&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;profit<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;xsl:value-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;(sell-price - cost)&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/profit<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:copy<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- don't copy these, when they're found, there is no output --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">match</span>=<span style="color: #ff0000;">&quot;author&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">match</span>=<span style="color: #ff0000;">&quot;conductor&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- copy all attribute or child nodes in place --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">match</span>=<span style="color: #ff0000;">&quot;@*|node()&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:copy<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:apply-templates</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;@*|node()&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>  
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:copy<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:stylesheet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>When applied to our source document, we get the result our boss wanted, it excludes any &#8220;author&#8221; or &#8220;composer&#8221; elements, and includes a &#8220;profit&#8221; element.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;inventory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;1&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>The Little Schemer<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>book<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;list-price<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>29.95<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/list-price<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;sell-price<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>26.99<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/sell-price<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cost<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>17.92<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/cost<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;profit<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>9.07<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/profit<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;2&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>XSLT<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>book<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;list-price<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>49.95<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/list-price<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;sell-price<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>34.99<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/sell-price<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cost<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>22.92<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/cost<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;profit<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>12.07<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/profit<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;3&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Romeo and Juliet<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>compact disc<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;list-price<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>18.98<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/list-price<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;sell-price<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>13.99<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/sell-price<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;cost<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>9.92<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/cost<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;profit<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>4.07<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/profit<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/inventory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Using this technique, we can then easily prepare another XSLT stylesheet to generate an inventory list for the customer, which will exclude the &#8220;cost&#8221; element, since we don&#8217;t want them knowing it!  All we need is to match all attribute and children nodes and copy them as normal, while providing no output when the XSLT processor encounters a &#8220;cost&#8221; element.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:stylesheet</span> <span style="color: #000066;">xmlns:xsl</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/1999/XSL/Transform&quot;</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">match</span>=<span style="color: #ff0000;">&quot;/&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:apply-templates</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- don't copy the cost, the customer doesn't need to know! --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">match</span>=<span style="color: #ff0000;">&quot;cost&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
&nbsp;
    <span style="color: #808080; font-style: italic;">&lt;!-- copy all attribute or child nodes in place --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">match</span>=<span style="color: #ff0000;">&quot;@*|node()&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:copy<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:apply-templates</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;@*|node()&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>  
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:copy<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:stylesheet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Personally, because I end up transforming XML sources to XML output, I end up using xsl:copy-of and xsl:template match=&#8221;@*|node()&#8221; all the time.  In fact, xsl:template match=&#8221;@*|node()&#8221; just happens to be the first piece of code in my XSLT toolbox.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.eddiewelker.com/2008/12/27/xslt-copy-copy-of/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Advantages of push-style XSLT over pull-style</title>
		<link>http://www.eddiewelker.com/2008/11/25/push-style-xslt-vs-pull-style/</link>
		<comments>http://www.eddiewelker.com/2008/11/25/push-style-xslt-vs-pull-style/#comments</comments>
		<pubDate>Tue, 25 Nov 2008 04:56:24 +0000</pubDate>
		<dc:creator>Eddie</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[xslt]]></category>
		<category><![CDATA[functional]]></category>
		<category><![CDATA[pull]]></category>
		<category><![CDATA[push]]></category>
		<category><![CDATA[scheme]]></category>
		<category><![CDATA[style]]></category>
		<category><![CDATA[templates]]></category>

		<guid isPermaLink="false">http://www.eddiewelker.com/?p=155</guid>
		<description><![CDATA[Working with more than a few new-hires over the last few weeks, I&#8217;ve noticed that new XSLT developers often write pull-style XSLTs by default.  However, this tends to defy XSLT&#8217;s functional heritage, and is not as useful as the opposite form, push-style XSLTs.
Pull-style XSLTs reach into the source document and pull out the data [...]]]></description>
			<content:encoded><![CDATA[<p>Working with more than a few new-hires over the last few weeks, I&#8217;ve noticed that new XSLT developers often write pull-style XSLTs by default.  However, this tends to defy XSLT&#8217;s functional heritage, and is not as useful as the opposite form, push-style XSLTs.</p>
<p>Pull-style XSLTs reach into the source document and pull out the data they need to transform.  The pull-style is similar to template systems like those found in Rails or Django, or inserting PHP commands between HTML elements. For example, given the trivial input:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;books<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;book<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>The Scheme Programming Language<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>R. Kent Dybvig<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/book<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;book<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Essentials of Programming Languages<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Daniel P. Friedman<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/book<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;book<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>An Introduction to Information Theory<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>John R. Pierce<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/author<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/book<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/books<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>an XSLT novice will produce a stylesheet like the following (note lines 11 and 12 which reach into the source and grab the data):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:stylesheet</span> <span style="color: #000066;">xmlns:xsl</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/1999/XSL/Transform&quot;</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">match</span>=<span style="color: #ff0000;">&quot;/&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>books<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dl<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:for-each</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;books/book&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dt<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;xsl:value-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;title&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/dt<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dd<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;xsl:value-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;author&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/dd<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:for-each<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dl<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:stylesheet<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>which transforms into:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;meta</span> <span style="color: #000066;">http-equiv</span>=<span style="color: #ff0000;">&quot;Content-Type&quot;</span> <span style="color: #000066;">content</span>=<span style="color: #ff0000;">&quot;text/html; charset=utf-8&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>books<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dl<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dt<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>The Scheme Programming Language<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dt<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dd<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>R. Kent Dybvig<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dd<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dt<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Essentials of Programming Languages<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dt<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dd<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Daniel P. Friedman<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dd<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dt<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>An Introduction to Information Theory<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dt<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dd<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>John R. Pierce<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dd<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dl<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<hr/>
<p>The real power of XSLT, however, is defining templates for the elements found within the source document. These are push-style XSLTs.  They have two main advantages.  First, push-style gracefully handles complex source structures, including recursively nested elements.  It would be near impossible to handle the following source document using pull-style,</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;div<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>a<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>if you didn&#8217;t know how deep the recursive divs would go. A push-style solution, though, is incredibly simple.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">match</span>=<span style="color: #ff0000;">&quot;div&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
     * <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:apply-templates</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span> *
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Will transform the previous source into the following.</p>
<pre>* * * a * * *</pre>
<p>In addition to handling complex source structures, push-style allows code reuse.  This is of course an ideal of any programming language.  Push-style XSLTs have a greater ability to be reused, because the individual templates can be reused.  When you only have one template, it is quite difficult to make it general without resorting to numerous choose-when statements.  Here is an example of code reuse, where we extend a previously written template with the xsl:apply-imports rule.</p>
<p>Given the input,</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;images<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;image<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://www.filmjunkie.com/drinks/blixa/blixa.jpg<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;alt<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Blixa!<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/alt<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/image<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/images<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>and the XSLTs,</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:import</span> <span style="color: #000066;">href</span>=<span style="color: #ff0000;">&quot;imageformat.xsl&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">match</span>=<span style="color: #ff0000;">&quot;image&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;wrapper&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:apply-imports</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>and the rule in &#8220;imageformat.xsl&#8221; (the template being extended in this case),</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:template</span> <span style="color: #000066;">match</span>=<span style="color: #ff0000;">&quot;image&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;img</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;{url}&quot;</span> <span style="color: #000066;">alt</span>=<span style="color: #ff0000;">&quot;{alt_text}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:template<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>the processor will apply the higher-precedence template first, and then apply the imported (and lower-precedence) template to yield the following output.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;wrapper&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;img</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;http://www.filmjunkie.com/drinks/blixa/blixa.jpg&quot;</span> <span style="color: #000066;">alt</span>=<span style="color: #ff0000;">&quot;Blixa!&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Push-style XSLTs are not the most obvious thing to pick up unless you&#8217;ve been exposed to a functional programming language like Lisp or Scheme. However, considering their great value, they should be among the first disciplines studied when learning XSLT. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.eddiewelker.com/2008/11/25/push-style-xslt-vs-pull-style/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Funny thing about bugs&#8230;</title>
		<link>http://www.eddiewelker.com/2008/09/18/funny-thing-about-bugs/</link>
		<comments>http://www.eddiewelker.com/2008/09/18/funny-thing-about-bugs/#comments</comments>
		<pubDate>Fri, 19 Sep 2008 01:17:12 +0000</pubDate>
		<dc:creator>Eddie</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[xslt]]></category>

		<guid isPermaLink="false">http://www.eddiewelker.com/?p=113</guid>
		<description><![CDATA[They just keep popping up.
I just scrolled down my own blog, and saw a post titled &#8220;Down to three bugs.&#8221;  Funny enough, three months later, we&#8217;re still trying to push the website out. (Wow, those were some really big bugs!!!  Haha, jk&#8230;)  In fact, I&#8217;ve been pressing for the last 2 days, trying to expedite [...]]]></description>
			<content:encoded><![CDATA[<p>They just keep popping up.</p>
<p>I just scrolled down my own blog, and saw a post titled &#8220;Down to three bugs.&#8221;  Funny enough, three months later, we&#8217;re still trying to push the website out. (Wow, those were some really big bugs!!!  Haha, jk&#8230;)  In fact, I&#8217;ve been <em>pressing</em> for the last 2 days, trying to expedite a few last-minute bugs so that QA testing could begin (for about the 18th time, now in snapshot 21).  Kinda odd, when QA finds bugs in round 20 that they didn&#8217;t find in round 10&#8230; despite no code changes in the meantime&#8230;</p>
<p>But I&#8217;m not going to complain.  Programming is still a lot of fun <em>when you&#8217;re actually doing it</em>.  It ends up being almost therapeutic when you&#8217;ve got other stuff going on that you&#8217;re trying to ignore.  Not only that, but there&#8217;s always room to improve.  A co-worker came to my desk yesterday and schooled me on how to write code.  When you&#8217;re young, you&#8217;re stupid.  He had lots of experience, and was&#8230; well, right&#8230; most of the time.  I was boxed into a corner at some points, but also made a few bad decisions.  The good thing is that I always learn from my mistakes, and next time will be <em>much</em> better.</p>
<p>I remember writing 3.5 websites before I really got common code working the way I wanted.  It was similar to the way I learned how to write CSS.  Excerpting common code so that I would only edit once, vs. editing 6 files just to make one update.  It was easy when I was using CSS, SSI&#8217;s, or PHP includes to simply import exactly what I needed, but I find it is far more difficult when using XSLT&#8217;s.  Why?  Because in XSLT you need to know not only the structure, but the <em>context</em>.  That&#8217;s what makes it difficult&#8230; writing context dependent generic code is difficult.</p>
<p>[Of course, dirty secret (I know you're skimming at this point, not really reading) is that I'm almost on only my 2 year anniversary (Sept. 20) of doing programming/development work.  So my progress is still quite good.]</p>
<p>Alright, I admit that this post was far more stream-of-conciousness than actual post, but it was good for me.  It wasn&#8217;t intended to be a &#8220;I haven&#8217;t written in a month, here&#8217;s what I&#8217;ve been doing&#8221; post, but more a &#8220;I&#8217;m busy, I&#8217;ll be back soon, but I don&#8217;t know when&#8221; type post.  So on that note&#8230;</p>
<p>[I think it's <a href="http://www.vamp.org/Siouxsie/Images/">Siouxsie</a> that's making me intentionally ambiguous. ]</p>
<p>Ta ta!</p>
<p>-e</p>
<p>[this post is like an email I never wrote to anyone...]</p>
]]></content:encoded>
			<wfw:commentRss>http://www.eddiewelker.com/2008/09/18/funny-thing-about-bugs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP and Me</title>
		<link>http://www.eddiewelker.com/2008/04/01/php-and-me/</link>
		<comments>http://www.eddiewelker.com/2008/04/01/php-and-me/#comments</comments>
		<pubDate>Wed, 02 Apr 2008 04:02:38 +0000</pubDate>
		<dc:creator>Eddie</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[xslt]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[programming language]]></category>
		<category><![CDATA[scheme]]></category>

		<guid isPermaLink="false">http://edwardwelker.com/2008/04/01/php-and-me/</guid>
		<description><![CDATA[I just finished writing a not-so-simple, not-so-complex PHP script.  First, lemme say that I had a good time with it, since it isn&#8217;t either XSLT or Portal (my workplace&#8217;s homebrew) code.  It&#8217;s kinda refreshing to look outside of my multi-month project.  Exciting, in fact&#8230; despite how insignificant (in the grand scheme of things) it is.
I&#8217;ve [...]]]></description>
			<content:encoded><![CDATA[<p>I just finished writing a not-so-simple, not-so-complex PHP script.  First, lemme say that I had a good time with it, since it isn&#8217;t either XSLT or Portal (my workplace&#8217;s homebrew) code.  It&#8217;s kinda refreshing to look outside of my multi-month project.  Exciting, in fact&#8230; despite how insignificant (in the grand scheme of things) it is.</p>
<p>I&#8217;ve got to admit, thought, that I don&#8217;t really like PHP.  I&#8217;m not going to bash PHP outright (as I&#8217;ve heard MANY people do).  It is a programming language, and it certainly has it&#8217;s place.  I&#8217;ve used many things written in PHP.   In fact, I feel bad about the way people dismiss PHP.  That said, I still don&#8217;t like it.   My problem is that I feel PHP is simply too verbose.  There seems to be a function for everything.  It is the total opposite of Scheme, where every function is a based on a handful of core functions.  I have had to deal with a number of languages in my life, and I certainly prefer languages with as little syntax as possible.</p>
<p>I spend a lot of time googling the functions.  And then the parameters.  And then the return types.  And then I run across another function that is kinda like the first one that I saw, but slightly different (maybe even described as more or less efficient!)  And then I have to research the return types.   I&#8217;m mostly bothered by the related functions.  Just give me something I can use in different ways.  Don&#8217;t give me the kitchen sink and tell me to choose the faucet type.</p>
<p>This is the same problem that I have with XSLTs.  It is a rather verbose language (which happens to usually be interspersed in XML/HTML).  This makes it hard to pick-up in a hurry, and makes it unintuitive.  This is important.  Unintuitive.  I spend a major part of my work day making sure that all of the webpages, all of the components, all of the behavior&#8230; even the URLs of my work are intuitive.  So I don&#8217;t like to accept much less from the tools I work with.</p>
<p>To extend the analogy slightly further&#8230; I have seen that when websites are unintuitive, that said websites can fail.  Is the same thing the case for programming languages?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.eddiewelker.com/2008/04/01/php-and-me/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Random February Thoughts</title>
		<link>http://www.eddiewelker.com/2008/03/01/random-february-thoughts/</link>
		<comments>http://www.eddiewelker.com/2008/03/01/random-february-thoughts/#comments</comments>
		<pubDate>Sat, 01 Mar 2008 18:02:29 +0000</pubDate>
		<dc:creator>Eddie</dc:creator>
				<category><![CDATA[classical music]]></category>
		<category><![CDATA[xslt]]></category>
		<category><![CDATA[columbia orchestra]]></category>

		<guid isPermaLink="false">http://edwardwelker.com/2008/03/01/random-february-thoughts/</guid>
		<description><![CDATA[In an effort to inspire me to write something else, I am throwing a few thoughts out from the last month.
My orchestra, The Columbia Orchestra, is playing Beethoven&#8217;s Ninth Symphony for our next concert. The orchestra is trying to make at least a small cultural event out of it.  There is a website (embracingthemillions.com) [...]]]></description>
			<content:encoded><![CDATA[<p>In an effort to inspire me to write something else, I am throwing a few thoughts out from the last month.</p>
<p>My orchestra, <a href="http://columbiaorchestra.org">The Columbia Orchestra</a>, is playing Beethoven&#8217;s Ninth Symphony for our next concert. The orchestra is trying to make at least a small cultural event out of it.  There is a website (<a href="http://embracingthemillions.com">embracingthemillions.com</a>) a flickr discussion group (<a href="http://flickr.com/groups/embracingthemillions">flickr.com/groups/embracingthemillions</a>), and a number of artists (not just musicians) participating in the event.  Since I threw together the website (no comments please, I&#8217;ve been very busy), I know the statistics of people visiting it.  And there are quite a few people visiting.  What I can&#8217;t figure out is with 30 or so people visiting per-day, why hasn&#8217;t anyone a) commented, b) written something on the discussion board?  I made a post in the discussion group.  It took me a few minutes at best.  No one else has posted.   Are people&#8217;s lives really so busy that they can&#8217;t be bothered?  I&#8217;m confused by this.  Technology aside, I am delighted to be playing Beethoven, he was simply a genius.</p>
<p>I went to see the Ingmar Bergman film, <em>The Magician</em> the other day at the <a href="http://afi.com/silver">AFI Silver Theater</a>.  I liked the quirky nature of the film, and found it to be totally engaging (I&#8217;ve yet to see one of his films that isn&#8217;t).  I regret not being able to see more movies from the <a href="http://www.afi.com/silver/new/nowplaying/2008/v5i1/bergman.aspx">first part of the Bergman Retrospective</a> they are doing now, as I have been ridiculously busy of late.  However, I do own most of them on DVD (I don&#8217;t own the Magician).  I look forward to the next few parts of this retrospective, as I don&#8217;t know as many of his later works.</p>
<p>I have been working extremely hard on the next version of My NCBI, the preferences section of the <a href="http://www.ncbi.nlm.nih.gov/">NCBI website</a>.  This is where you can set preferences for PubMed, Blast, and all of the rest of the NCBI sites/databases.  It is hard, writing everything in a internally-created language, which is slightly buggy and tends to make easy things easy, but hard things very hard, with XSLTs completing the system.  I haven&#8217;t had to write a ton of recursive XSLT functions or anything, (mostly because of the inclusion of <a href="http://www.exslt.org/">EXSLT extensions</a>), but development time is still very slow.  It also seems that with every step I take forward, I discover 4 new things I have to do.  Throw in the fact I made time estimates without knowing the language, and the NCBI announcement that the budget fell flat (and actually decreased) this year and people were dismissed, and that makes my life particularly fun. I knew what I was getting into when I signed up, and I wanted to do a LOT of work, but that doesn&#8217;t mean I feel that way every second of every day.</p>
<p>Additionally, I&#8217;ve had to meet a bunch of deadlines for the legal aspects of my dad&#8217;s passing.  Trips/talks to lawyers, a visit to DC Vital Records, Orphan&#8217;s Court (yes, they call it that!) and NASA have really been a&#8230; treat, let&#8217;s say.</p>
<p>Also, some of the better (or at least unexpected) and smaller moments of the month were:</p>
<ul>
<li>Re-discovering IRC.  I hadn&#8217;t even thought of IRC until I saw it mentioned in a twitter post.  I found, however, that the groups in irc.freenode.net are going strong.  I&#8217;ve read some of the #scheme discussion, answered a considerable number of questions in the #html and #css groups, and just lurked watching #microformats and #javascript.  Interesting stuff.</li>
<li>I&#8217;ve been taking <a href="http://flickr.com/people/ed_welker">a number of photos</a>.  I like holding on to moments, which is probably why I like taking pictures so much.  Hopefully I will continue.  I keep telling myself that if I do, I&#8217;ll upgrade to a D-SLR when I finally get all of my money issues (paying my father&#8217;s bills) worked out.</li>
<li>I went on a kick of listening to nothing but David Bowie (thanks <a href="http://twitter.com/ashleylynn">@ashleylynn</a>) which was nice. I&#8217;ve been floundering around, listening to whatever CD was closest for a while now.  I love finally regaining some direction.  I also feel a Prokofiev kick coming on.  Maybe, maybe not.</li>
<li>I got a new monitor. A Dell 24&#8243; flatscreen, which I really like.  I knew they were coming out with new models (which have since arrived on their website), and luckily the only real difference was HDMI inputs, which I don&#8217;t need since I don&#8217;t plan on watching HD movies or TV on it (unless from my computer).  My PC now has the 24&#8243; as the main, and my 20&#8243; rotated 90 deg. as a secondary.  It is an extremely useful setup.  I love the multiple inputs, since the press of a button on the front will switch me from PC to Mac and back again</li>
<li>The Giants won the Superbowl.  That was teriffic.  So my Red Sox took the World Series, my Giants have the Superbowl, who will win the NBA Championship?  (Ha, not the Knicks!) But I would love to see Detroit or Boston make the finals.  Especially Detroit, I think.</li>
<li>I ordered a new laptop hard-drive from <a href="http://newegg.com">Newegg</a>, which should arrive either today or Monday. I installed Leopard on it last month, and during that time got tired of squeezing everything onto my tiny 60gb drive.  So 250gb will be here soon, and I&#8217;ll transfer.  I&#8217;ve found the only thing I really like about Leopard is Spaces, the virtual window manager.  On the otherhand, it works in ways I don&#8217;t expect (or think it should work).  Eventually, I will look to see if there are some command line settings I can use to change behavior.  For now, it will just occasionally annoy me.</li>
</ul>
<p>Finally, I am quite tired of the weather.  Continual cold with occasional teases of warmer temperatures have made this, in my mind, one of the longest winters ever.  I can&#8217;t wait for even slightly warmer temperatures, so I can convince myself to get back outside on my bike.  I haven&#8217;t rode, again, in forever.  I look forward to changing that.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.eddiewelker.com/2008/03/01/random-february-thoughts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>XSLT Class</title>
		<link>http://www.eddiewelker.com/2007/10/23/xslt-class/</link>
		<comments>http://www.eddiewelker.com/2007/10/23/xslt-class/#comments</comments>
		<pubDate>Wed, 24 Oct 2007 03:07:13 +0000</pubDate>
		<dc:creator>Eddie</dc:creator>
				<category><![CDATA[xslt]]></category>
		<category><![CDATA[beginning xslt]]></category>
		<category><![CDATA[doug tidwell]]></category>
		<category><![CDATA[functional]]></category>
		<category><![CDATA[functional programming language]]></category>
		<category><![CDATA[jeni tennison]]></category>
		<category><![CDATA[mastering xml transformations]]></category>
		<category><![CDATA[mulberry technologies]]></category>
		<category><![CDATA[wendell piez]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[xslt 1.0]]></category>
		<category><![CDATA[xslt class]]></category>

		<guid isPermaLink="false">http://edwardwelker.com/blog/2007/10/23/xslt-class/</guid>
		<description><![CDATA[Last Monday through last Wednesday I attended Mulberry Technologies&#8217; XSLT course.  I had a fun time, and learned a bunch.
To prepare, I looked through a few books.  I looked through Jeni Tennison&#8217;s Beginning XSLT 2.0 book, but it wasn&#8217;t for me.  I didn&#8217;t like how wordy it was&#8230; I got lost in [...]]]></description>
			<content:encoded><![CDATA[<p>Last Monday through last Wednesday I attended <a href="http://mulberrytech.com/">Mulberry Technologies&#8217; XSLT course</a>.  I had a fun time, and learned a bunch.</p>
<p>To prepare, I looked through a few books.  I looked through Jeni Tennison&#8217;s <em>Beginning XSLT 2.0</em> book, but it wasn&#8217;t for me.  I didn&#8217;t like how wordy it was&#8230; I got lost in the dialogue, and it distracted me from the points and the examples.  On the otherhand, <a href="http://www.oreillynet.com/pub/au/749">Doug Tidwell</a>&#8217;s book, <em>XSLT-Mastering XML Tranformations</em> was precisely what I was looking for.  It was concise and organized very methodically.  PLUS, it only cost me $7 with shipping from Amazon.  Heh, gotta love that!</p>
<p>[note: because we only use 1.0 on the job, older books were better, as I didn't have to figure out what was a 2.0 feature and what was a 1.0 feature... and again, the books were cheaper]</p>
<p>Fun stuff aside, XSLT is a pretty cool language.  I&#8217;m too new to it to debate if it is a functional programming language or simply based on those attributes, but I&#8217;m happy to get to play with its functional aspects.  I am kinda thrilled to dust off my recursive-function-writing skills.  I hope to use it early and often, so I can get a good amount of practice in.</p>
<p>The class was useful because it showed me things I didn&#8217;t pick up from the books.  First was the concept that the root node is placed above what I called the XML root node.  The class called the XML root node the &#8220;document node.&#8221;  Solved one problem that I had been having. The other really enlightening aspect was the axes.  When I read the book and saw that there 13, I simply skimmed it.  It was good to sit down with , as the following:: and preceding:: axes were not what I thought.</p>
<p>Debbie Lapeyre and Wendell Piez, the instructors, were great and worked with us to answer all of our questions&#8230; even my weird set theory questions.  Wendell, in particular (because he led the third day) was helpful, showing me some of the common &#8220;hacks&#8221; people use to make 1.0 do things it wasn&#8217;t intended to do [despite my incredibly poorly phrased question!].  Now that I can understand XPath a little better, I can break down the pieces of the hacks&#8230; so things like the Muenchian method actually make sense now.</p>
<p>Hopefully I will get to keep working with XSLT&#8217;s, I&#8217;m definitely not an expert from three days.  But since they&#8217;re core to the portal system I work with, I expect to get a lot more practice in.  If I get good enough, I&#8217;d love to post some information that may be useful to others.  Stay tuned!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.eddiewelker.com/2007/10/23/xslt-class/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Class over</title>
		<link>http://www.eddiewelker.com/2007/10/18/class-over/</link>
		<comments>http://www.eddiewelker.com/2007/10/18/class-over/#comments</comments>
		<pubDate>Thu, 18 Oct 2007 12:36:00 +0000</pubDate>
		<dc:creator>Eddie</dc:creator>
				<category><![CDATA[xslt]]></category>
		<category><![CDATA[functional programming]]></category>
		<category><![CDATA[scheme]]></category>

		<guid isPermaLink="false">http://edwardwelker.com/blog/2007/10/18/class-over/</guid>
		<description><![CDATA[I am finished my XSLT class, and learned some very interesting things.  Very cool the way some of the features of the language fit within the context of Scheme (and Lisp, for that matter).   More to come.
]]></description>
			<content:encoded><![CDATA[<p>I am finished my XSLT class, and learned some very interesting things.  Very cool the way some of the features of the language fit within the context of Scheme (and Lisp, for that matter).   More to come.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.eddiewelker.com/2007/10/18/class-over/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>catching up</title>
		<link>http://www.eddiewelker.com/2007/10/03/catching-up/</link>
		<comments>http://www.eddiewelker.com/2007/10/03/catching-up/#comments</comments>
		<pubDate>Thu, 04 Oct 2007 03:16:00 +0000</pubDate>
		<dc:creator>Eddie</dc:creator>
				<category><![CDATA[Boston]]></category>
		<category><![CDATA[baseball]]></category>
		<category><![CDATA[bethesda]]></category>
		<category><![CDATA[classical music]]></category>
		<category><![CDATA[red sox]]></category>
		<category><![CDATA[xslt]]></category>
		<category><![CDATA[Advanced DOM Scripting]]></category>
		<category><![CDATA[kensington]]></category>
		<category><![CDATA[moving]]></category>
		<category><![CDATA[random thoughts]]></category>
		<category><![CDATA[riding my bike]]></category>
		<category><![CDATA[tchaikovsky]]></category>

		<guid isPermaLink="false">http://edwardwelker.com/blog/2007/10/03/catching-up/</guid>
		<description><![CDATA[This is my appropriately named &#8220;catching up&#8221; post.  As most know, I&#8217;ve moved over the last month.  Despite my early action, I still had to do a lot of work at the end of the month.  C&#8217;est la vie, that&#8217;s what moving is.  Aside that, I&#8217;ve also been focused on various work related things (css, [...]]]></description>
			<content:encoded><![CDATA[<p>This is my appropriately named &#8220;catching up&#8221; post.  As most know, I&#8217;ve moved over the last month.  Despite my early action, I still had to do a lot of work at the end of the month.  C&#8217;est la vie, that&#8217;s what moving is.  Aside that, I&#8217;ve also been focused on various work related things (css, xslt, and the &#8220;portal&#8221; system), as well as the end of baseball&#8217;s regular season and the beginning of the post season.</p>
<p>On my list of things to touch in full&#8230; 1) Advanced DOM Scripting.  I finished the book (a while ago) but haven&#8217;t had a chance to review it yet.  At the risk of ruining the surprise for my one (hopefully) reader, it was teriffic, and I highly recommend it.   I will do a full write-up later.  2) My comments on The Little Schemer.  This is simply an amazing book that&#8217;s opened up a number of paths in my brain.  I&#8217;m afraid that any review wouldn&#8217;t do it justice.  Maybe some thoughts on the book everyone should read. 3) Random thoughts on Bethesda, Kensington, Rockville (my new home!), riding my bike again, the Tchaikovsky concert coming up, my xslt class coming up, my recent css exploits&#8230; as you can see, I&#8217;ve got a lot of things that I am thinking about, but nothing to put down on paper&#8230; yet.  (Yes, I know, really, there is no paper&#8230; shut up!)</p>
<p>Contrast that with the time that I MUST spend with friends, and the potential Boston trip coming up&#8230; and I&#8217;ve got a lot to do!!  Better get started.  Washing my clothes now&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.eddiewelker.com/2007/10/03/catching-up/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
