<?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/"
	>

<channel>
	<title>Code Sleuth's Blog &#187; XQuery</title>
	<atom:link href="http://blog.codesleuth.co.uk/index.php/tag/xquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.codesleuth.co.uk</link>
	<description>The .NET Pimp</description>
	<lastBuildDate>Wed, 24 Mar 2010 09:21:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>XSLT Optimisation</title>
		<link>http://blog.codesleuth.co.uk/index.php/2008/03/06/xslt-optimisation/</link>
		<comments>http://blog.codesleuth.co.uk/index.php/2008/03/06/xslt-optimisation/#comments</comments>
		<pubDate>Thu, 06 Mar 2008 13:00:45 +0000</pubDate>
		<dc:creator>Sleuth</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[XQuery]]></category>
		<category><![CDATA[XSLT]]></category>

		<guid isPermaLink="false">http://blog.codesleuth.co.uk/index.php/2008/03/06/xslt-optimisation/</guid>
		<description><![CDATA[As far as I remember, the first time I had to use XSLT was nearly 2 years ago, where we were required to use XSLT to produce HTML/XHTML reports from XML data returned and modified by our client. Since I was new to the language (also new to XQuery as you can imagine) I used [...]]]></description>
			<content:encoded><![CDATA[<p>As far as I remember, the first time I had to use XSLT was nearly 2 years ago, where we were required to use XSLT to produce HTML/XHTML reports from XML data returned and modified by our client. Since I was new to the language (also new to XQuery as you can imagine) I used a lot of <em>styles</em> from a co-worker of mine who had certain bad coding habits that he probably wasn&#8217;t aware of also.</p>
<p>Recently I had to revisit the coding I did on the XSLT reporting module, to solve a problem with speed and file sizes when the client was run on a lot of data (when I say a lot of data, I mean a full system-wide report). It wasn&#8217;t unreasonable that the output was very large, as this is just how the report will end up no matter what we do, besides shaving the odd unnecessary tag off here and there, but taking 20minutes to produce a report, using 1.5GB of virtual memory, is the main problem.</p>
<p>Since time is always of the essence I had to attempt to fix whatever I could, and I came up with a few pointers which successfully decreased the transformation processing time. Take this slightly modified <a title="Developer Fusion Bookstore XML Example" href="http://quickstart.developerfusion.co.uk/QuickStart/util/srcview.aspx?path=~/aspnet/samples/ctrlref/navigation/TreeView/TreeView14.src&amp;file=App_Data\Bookstore.xml" target="_blank">example XML file</a>:</p>
<pre><code>&lt;Bookstore&gt;
&lt;genre name="Business"&gt;
&lt;book ISBN="BU1032" Title="The Busy Executive's Database Guide" Price="19.99"&gt;
&lt;chapter num="1" name="Introduction"&gt;
Abstract...
&lt;/chapter&gt;
&lt;chapter num="2" name="Body"&gt;
Abstract...
&lt;/chapter&gt;
&lt;/book&gt;
&lt;book ISBN="BU2075" Title="You Can Combat Computer Stress!" Price="2.99"&gt;
&lt;chapter num="1" name="Introduction"&gt;
Abstract...
&lt;/chapter&gt;
&lt;chapter num="2" name="Body"&gt;
Abstract...
&lt;/chapter&gt;
&lt;chapter num="3" name="Conclusion"&gt;
Abstract...
&lt;/chapter&gt;
&lt;/book&gt;
&lt;book ISBN="BU7832" Title="Straight Talk About Computers" Price="19.99"&gt;
&lt;chapter num="1" name="Introduction"&gt;
Abstract...
&lt;/chapter&gt;
&lt;chapter num="2" name="Body"&gt;
Abstract...
&lt;/chapter&gt;
&lt;/book&gt;
&lt;/genre&gt;
&lt;/Bookstore&gt;
</code></pre>
<p>If we want to get a list of all books with at least 3 chapters, no matter which genre it is, we may use XQuery in the form of <code>//chapter[@num='3']/../@Title</code> which should return the book &#8220;You Can Combat Computer Stress!&#8221;, and indeed it does:</p>
<pre><code>A: Title = You Can Combat Computer Stress!</pre>
<p></code><br />
Looking at this code we can see that it begins with the global search specifier <code>//</code> without a specified parent, which will enumerate every node in the document in order to find a <code>chapter</code> node.</p>
<p>To optimise this, there is a simple change we can make to the XQuery that will simply change the global search to a fully qualified path name. Change the <code>//</code> to <code>/Bookstore/genre/book/</code> which will now be:</p>
<pre><code>/Bookstore/genre/book/chapter[@num='3']/../@Title
</pre>
<p></code><br />
This returns:</p>
<pre><code>A: Title = You Can Combat Computer Stress!
</pre>
<p></code><br />
The result is the same, but the XSLT processor no longer has to look around for the node we want to select.</p>
<p>In this particular scenario, there would be virtually no benefit in optimising this, but optimising this XQuery to be run on a very large XML database where it may be run many times over would reduce the total processing time by quite an amount.</p>
<p>NOTE: this will only work in XQuery code that doesn't look for nodes that are defined in more than one place. If the element you are searching for will always be in a fixed hierarchy, this method should work and help your transformations.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.codesleuth.co.uk/index.php/2008/03/06/xslt-optimisation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
