<?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>MacLochlainns Weblog &#187; xsl:query</title>
	<atom:link href="http://blog.mclaughlinsoftware.com/tag/xslquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.mclaughlinsoftware.com</link>
	<description>Michael McLaughlin's Technical Blog</description>
	<lastBuildDate>Mon, 06 Sep 2010 21:21:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Querying Oracle directly from XML</title>
		<link>http://blog.mclaughlinsoftware.com/2008/08/29/querying-oracle-directly-from-xml/</link>
		<comments>http://blog.mclaughlinsoftware.com/2008/08/29/querying-oracle-directly-from-xml/#comments</comments>
		<pubDate>Fri, 29 Aug 2008 22:56:21 +0000</pubDate>
		<dc:creator>maclochlainn</dc:creator>
				<category><![CDATA[Oracle]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[XML query to Oracle]]></category>
		<category><![CDATA[xsl:query]]></category>

		<guid isPermaLink="false">http://maclochlainn.wordpress.com/?p=289</guid>
		<description><![CDATA[I&#8217;d gone through Doug Tidwell&#8217;s XSLT (2nd Edition), I was disappointed that there wasn&#8217;t any coverage in Chapter 9 about how to connect through an XSLT Processor to an Oracle database. Overall, the book is improved. I&#8217;d recommend it as a reference or a detailed read for the lost truth of XML. Seriously, it&#8217;s a [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;d gone through Doug Tidwell&#8217;s XSLT (2nd Edition), I was disappointed that there wasn&#8217;t any coverage in Chapter 9 about how to connect through an XSLT Processor to an Oracle database. Overall, the book is improved. I&#8217;d recommend it as a reference or a detailed read for the lost truth of XML. Seriously, it&#8217;s a very readable book.</p>
<p>I didn&#8217;t find anything through a quick search of the web, and I figured a little example might be nice. I used Xalan-Java of the example.  Xalan-Java is a convenient open source XSLT Processor, covered in an <a title="How to convert XML to CSV and upload into Oracle" href="http://blog.mclaughlinsoftware.com/how-to-convert-xml-to-csv-and-upload-into-oracle/" target="_blank">earlier blog</a>. You can refer to that blog page for the download location and general configuration of Xalan-Java. There is one more configuration step after those in the other blog entry. You need to put the Oracle JDBC driver file in the class path. That&#8217;s where the Oracle class files are located. This example uses ojdbc5.jar from an Oracle Database 11g home on Windows:</p>

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;">C:\&gt;SET <span style="color: #448844;">CLASSPATH</span>=<span style="color: #33cc33;">%</span><span style="color: #448888;">CLASSPATH</span><span style="color: #33cc33;">%</span>;C:\oracle\11.1.0\db_1\jdbc\lib\ojdbc5.jar</pre></div></div>

<p>You can write this sample file, substituting a user, password, table and column (or two) from your own database:</p>
<p><a href="http://blog.mclaughlinsoftware.com/wp-content/uploads/2008/08/xmlquery1.jpg"><img class="aligncenter size-medium wp-image-290" src="http://blog.mclaughlinsoftware.com/wp-content/uploads/2008/08/xmlquery1.jpg?w=300" alt="" style="border:none" width="300" height="195" /></a></p>
<p>The hidden rows didn&#8217;t fit nicely in the other image, but they&#8217;re required to make it work. There in the next image. They are the templates for handling columns and rows.</p>
<p><a href="http://blog.mclaughlinsoftware.com/wp-content/uploads/2008/08/xmlquery2.jpg"><img class="aligncenter size-medium wp-image-291" src="http://blog.mclaughlinsoftware.com/wp-content/uploads/2008/08/xmlquery2.jpg?w=300" alt="" style="border:none" width="300" height="116" /></a></p>
<p>The command line generates an HTML page for you and redirects it to a file:</p>

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;">C:\&gt; java org.apache.xalan.xslt.Process -XSL oracle_xml_query.xsl &gt; sample.htm</pre></div></div>

<p>or, the preferred syntax:</p>

<div class="wp_syntax"><div class="code"><pre class="dos" style="font-family:monospace;">C:\&gt; java org.apache.xalan.xslt.Process -XSL oracle_xml_query.xsl -OUT sample.htm</pre></div></div>

<p>As per the request, here&#8217;s the native code for an XML direct query against an Oracle database.</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;xsl:stylesheet</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span></span>
<span style="color: #009900;"> <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;">xmlns:sql</span>=<span style="color: #ff0000;">&quot;org.apache.xalan.lib.sql.XConnection&quot;</span></span>
<span style="color: #009900;"> <span style="color: #000066;">extension-element-prefixes</span>=<span style="color: #ff0000;">&quot;sql&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:output</span> <span style="color: #000066;">method</span>=<span style="color: #ff0000;">&quot;html&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;xsl:variable</span></span>
<span style="color: #009900;">  <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;movies&quot;</span></span>
<span style="color: #009900;">  <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;sql:new('oracle.jdbc.driver.OracleDriver'</span>
<span style="color: #009900;">        ,'jdbc:oracle:thin:@mclaughlin11g:1521:orcl','plsql','plsql')&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:variable</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;streaming&quot;</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;sql:disableStreamingMode($movies)&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:variable</span></span>
<span style="color: #009900;">  <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;queryResults&quot;</span></span>
<span style="color: #009900;">  <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;sql:query($movies,'SELECT item_title, item_subtitle FROM 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;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 style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Oracle Result Set<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span><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> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;font-family: sans-serif;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;table</span> <span style="color: #000066;">border</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">cellpadding</span>=<span style="color: #ff0000;">&quot;5&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tr<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;$queryResults/sql/metadata/column-header&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;th<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;@column-label&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/th<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;/tr<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;$queryResults/sql/row-set/row&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/table<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:value-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;sql:close($movies)&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>
<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;row&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;tr<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;xsl:apply-templates</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;col&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/tr<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:template</span> <span style="color: #000066;">match</span>=<span style="color: #ff0000;">&quot;col&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;td<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;text()&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/td<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></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.mclaughlinsoftware.com/2008/08/29/querying-oracle-directly-from-xml/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
