<?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>out &#62;&#62; m_Conscientia; &#187; sql</title>
	<atom:link href="http://blog.hypercomplex.co.uk/index.php/tag/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.hypercomplex.co.uk</link>
	<description>a multidimensional braindump</description>
	<lastBuildDate>Sat, 27 Aug 2011 19:16:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Filtering sqlmetal output using XSLT</title>
		<link>http://blog.hypercomplex.co.uk/index.php/2010/03/filtering-sqlmetal-output-using-xslt/</link>
		<comments>http://blog.hypercomplex.co.uk/index.php/2010/03/filtering-sqlmetal-output-using-xslt/#comments</comments>
		<pubDate>Thu, 04 Mar 2010 21:50:41 +0000</pubDate>
		<dc:creator>Alex Peck</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[build target]]></category>
		<category><![CDATA[linq]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[sqlmetal]]></category>
		<category><![CDATA[visual studio]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[xslt]]></category>

		<guid isPermaLink="false">http://blog.hypercomplex.co.uk/?p=799</guid>
		<description><![CDATA[I needed to generate LINQ to SQL O/RM classes for a subset of the objects in a large database. Unfortunately, sqlmetal doesn&#8217;t provide a mechanism to filter it&#8217;s output, so I made my own using XSLT, a batch file and a custom build target. This post explains how. Overview When you invoke a build, a [...]]]></description>
			<content:encoded><![CDATA[<p>I needed to generate <a href="http://msdn.microsoft.com/en-us/library/bb425822.aspx">LINQ to SQL</a> O/RM classes for a subset of the objects in a large database. Unfortunately, <a href="http://msdn.microsoft.com/en-us/library/bb386987.aspx">sqlmetal</a> doesn&#8217;t provide a mechanism to filter it&#8217;s output, so I made my own using XSLT, a batch file and a custom build target. This post explains how. </p>
<p>
<strong>Overview</strong></p>
<p>When you invoke a build, a custom &#8220;BeforeBuild&#8221; build target runs to do the code generation. This code generation is driven by a batch file, this is what it does:</p>
<ol>
<li> Run sqlmetal to output a dbml file for the entire database schema</li>
<li> Using an XML configuration file and an XSLT transform, generate a test .cs file containing a reference to each expected table. When included in our project, this gives us a compile time test that we generated the expected classes (we make this internal).</li>
<li> Run a second XSLT transform based on the configuration file and the output a dbml file from step 1. In this step we prune the xml according to the config file. The result is a dbml file containing only the tables we specify in our configuration.</li>
<li> Run sqlmetal again using the result of step 3 as input. This time sqlmetal outputs our CSharp code for our Linq to Sql classes. </li>
</ol>
<p>The XSLT transforms are run from the command line using MSXSL.exe, which is available <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=2FB55371-C94E-4373-B0E9-DB4816552E41&#038;displaylang=en">here</a>.</p>
<p>The generated CSharp files are part of my Visual Studio project. After the BeforeBuild target is run (which does the code generation), the generated code is compiled into an Assembly.</p>
<p>
<strong>Custom build target</strong></p>
<p>These are the pertinent parts of my project file. You can see that inside my project directory, I created a directory called Prebuild where the work happens. This is where the Configuration.xml, DbmlPruner.xslt, GenerateTestTypes.xslt and generate.bat files live.</p>

<div class="wp_codebox"><table><tr id="p7996"><td class="code" id="p799code6"><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;Project</span> <span style="color: #000066;">ToolsVersion</span>=<span style="color: #ff0000;">&quot;3.5&quot;</span> <span style="color: #000066;">DefaultTargets</span>=<span style="color: #ff0000;">&quot;Build&quot;</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/developer/msbuild/2003&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #808080; font-style: italic;">&lt;!-- most of the project file omitted for brevity --&gt;</span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Target</span> <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;BeforeBuild&quot;</span> <span style="color: #000066;">DependsOnTargets</span>=<span style="color: #ff0000;">&quot;GenerateDbClasses;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span> <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Target<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Target</span> <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;GenerateDbClasses&quot;</span> <span style="color: #000066;">Inputs</span>=<span style="color: #ff0000;">&quot;Prebuild\Configuration.xml;Prebuild\DbmlPruner.xslt;Prebuild\GenerateTestTypes.xslt&quot;</span> <span style="color: #000066;">Outputs</span>=<span style="color: #ff0000;">&quot;MasterDb.Generated.cs;TestMasterDbTypes.cs&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Exec</span> <span style="color: #000066;">Command</span>=<span style="color: #ff0000;">&quot;$(ProjectDir)Prebuild\generate.bat $(ProjectDir) $(TargetName) master.dbml prunedmaster.dbml TestMasterDbTypes.cs MasterDb Configuration.xml master&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Target<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Project<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>
<strong>Configuration.xml</strong></p>

<div class="wp_codebox"><table><tr id="p7997"><td class="code" id="p799code7"><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: #808080; font-style: italic;">&lt;!-- This configuration is used to specify which tables should generate linq to SQL classes, </span>
<span style="color: #808080; font-style: italic;">  and to generate a sanity check class which verifies all the expected types exist in the </span>
<span style="color: #808080; font-style: italic;">  generated code.</span>
<span style="color: #808080; font-style: italic;">  --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Configuration</span> <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;Master Tables&quot;</span> <span style="color: #000066;">SourceXml</span>=<span style="color: #ff0000;">&quot;master.dbml&quot;</span> <span style="color: #000066;">Namespace</span>=<span style="color: #ff0000;">&quot;Master.Data.Linq&quot;</span> <span style="color: #000066;">TestClassName</span>=<span style="color: #ff0000;">&quot;TestMasterTypes&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;">SqlName</span>=<span style="color: #ff0000;">&quot;dbo.TestTable&quot;</span> <span style="color: #000066;">DataContextPropertyName</span>=<span style="color: #ff0000;">&quot;TestTable&quot;</span> <span style="color: #000066;">ClassName</span>=<span style="color: #ff0000;">&quot;TestTableRow&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>
<strong>GenerateTestTypes.xslt</strong></p>

<div class="wp_codebox"><table><tr id="p7998"><td class="code" id="p799code8"><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: #808080; font-style: italic;">&lt;!-- ===========================================================</span>
<span style="color: #808080; font-style: italic;">  Generate a C# class with members corresponding to all the linq</span>
<span style="color: #808080; font-style: italic;">  to SQL tables specified in the input file.</span>
<span style="color: #808080; font-style: italic;">================================================================ --&gt;</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:output</span> <span style="color: #000066;">method</span>=<span style="color: #ff0000;">&quot;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> <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: #339933;">&lt;![CDATA[&lt;auto-generated&gt;]]&gt;</span>
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost 
//     if the code is regenerated.
// <span style="color: #339933;">&lt;![CDATA[&lt;/auto-generated&gt;]]&gt;</span>
//-----------------------------------------------------------------------
&nbsp;
// Disable warning CS0169: The private field 'foo' is never used. This is 
// by design.
#pragma warning disable 0169
&nbsp;
namespace <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;/Configuration/@Namespace&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
{
    /// <span style="color: #339933;">&lt;![CDATA[&lt;summary&gt;]]&gt;</span>
    /// This class is provided as a compile time test for the linq to SQL
    /// classes specified in Prebuild/configuration.xml. It will fail to
    /// compile if one of the dependent classes is not generated (or not
    /// generated with the expected name). This is by design.
    /// <span style="color: #339933;">&lt;![CDATA[&lt;/summary&gt;]]&gt;</span>
    internal class <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;/Configuration/@TestClassName&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> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;/Configuration/Table&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;xsl:text<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><span style="color: #ddbb00;">&amp;#9;</span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:text<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;xsl:text<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><span style="color: #ddbb00;">&amp;#9;</span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:text<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>private <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;@ClassName&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:value-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:value-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;concat(@ClassName, 'Member')&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:text<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><span style="color: #ddbb00;">&amp;#xa;</span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:text<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/xsl:for-each<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;xsl:text<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><span style="color: #ddbb00;">&amp;#9;</span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsl:text<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>}
}
&nbsp;
#pragma warning restore 0169
  <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>
<strong>DbmlPruner.xslt</strong></p>

<div class="wp_codebox"><table><tr id="p7999"><td class="code" id="p799code9"><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: #808080; font-style: italic;">&lt;!-- ===========================================================</span>
<span style="color: #808080; font-style: italic;">  Replicate a .dbml file based on the tables specified in a </span>
<span style="color: #808080; font-style: italic;">  configuration.</span>
<span style="color: #808080; font-style: italic;">================================================================ --&gt;</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>
<span style="color: #009900;">	 <span style="color: #000066;">xmlns:sql</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/linqtosql/dbml/2007&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: #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;xml&quot;</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: #000066;">indent</span>=<span style="color: #ff0000;">&quot;yes&quot;</span></span>
<span style="color: #009900;">  	  omit-xml-declaration = <span style="color: #ff0000;">&quot;no&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;Configuration&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
		<span style="color: #808080; font-style: italic;">&lt;!-- load sql metal output as $database --&gt;</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;database&quot;</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;document(@SourceXml)&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;namespace&quot;</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;namespace-uri($database/sql:Database)&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:comment<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;@Name&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span> generated from database <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;$database/sql:Database/@Name&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:value-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;@SourceXml&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:comment<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
		<span style="color: #808080; font-style: italic;">&lt;!-- Output a tree which replicates @SourceXml but contains only the table nodes in the configuration --&gt;</span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Database&quot;</span> <span style="color: #000066;">namespace</span>=<span style="color: #ff0000;">&quot;{$namespace}&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:attribute</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Name&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:value-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;$database/sql:Database/@Name&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:attribute<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:for-each</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;/Configuration/Table&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:variable</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;sqlName&quot;</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;@SqlName&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
				<span style="color: #808080; font-style: italic;">&lt;!-- only output a table element when the source table exists --&gt;</span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:if</span> <span style="color: #000066;">test</span>=<span style="color: #ff0000;">&quot;$database/sql:Database/sql:Table[@Name=$sqlName]&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
					<span style="color: #808080; font-style: italic;">&lt;!-- Output the table substituting Member and Type for the ClassName in the configuration --&gt;</span>
					<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Table&quot;</span> <span style="color: #000066;">namespace</span>=<span style="color: #ff0000;">&quot;{$namespace}&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:attribute</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Name&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:value-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;@SqlName&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:attribute<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
						<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsl:attribute</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Member&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:value-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;@DataContextPropertyName&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:attribute<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:element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Type&quot;</span> <span style="color: #000066;">namespace</span>=<span style="color: #ff0000;">&quot;{$namespace}&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:attribute</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Name&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:value-of</span> <span style="color: #000066;">select</span>=<span style="color: #ff0000;">&quot;@ClassName&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:attribute<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
							<span style="color: #808080; font-style: italic;">&lt;!-- Copy the children (Columns etc) --&gt;</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;$database/sql:Database/sql:Table[@Name=$sqlName]/sql:Type/*&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:element<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:element<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:if<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;/xsl:element<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></td></tr></table></div>

<p>
<strong>generate.bat</strong></p>
<p>This is my entire generate.bat file, you can glean the input arguments from the custom build target above.</p>

<div class="wp_codebox"><table><tr id="p79910"><td class="code" id="p799code10"><pre class="bat" style="font-family:monospace;">::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Generate LINQ to SQL classes based on tables defined in an xml config
::
&nbsp;
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
&nbsp;
setlocal 
&nbsp;
set SQLMETAL=&lt;put your path here!&gt;\sdk\Win2008\Bin\sqlmetal.exe
set MSXSL=&lt;put your path here!&gt;\msxsl.exe
&nbsp;
set PRJDIR=%1%
set PREDIR=%PRJDIR%Prebuild\
&nbsp;
set NAMESPACE=%2
&nbsp;
set METALOUT=%PREDIR%%3
set METALIN=%PREDIR%%4
&nbsp;
set COMPILETESTCLASS=%PRJDIR%%5
set CONTEXTCLASS=%6
set CONTEXTCLASSFILE=%PRJDIR%%CONTEXTCLASS%.Generated.cs
&nbsp;
set CONFIGPATH=%PREDIR%%7
set DATABASE=%8
&nbsp;
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
&nbsp;
echo ================ Generation of LINQ to SQL classes started ================
echo Using %CONFIGPATH%
&nbsp;
echo 1. Building %METALOUT% for entire %DATABASE% database 
%SQLMETAL% /conn:&quot;server=localhost; database=%DATABASE%;Integrated Security=SSPI&quot; /dbml:%METALOUT%
if errorlevel 1 goto :Failed
&nbsp;
echo 2. Generating %COMPILETESTCLASS% to test linq to sql types were generated correctly at compile time
%MSXSL% %CONFIGPATH% %PREDIR%GenerateTestTypes.xslt -o %COMPILETESTCLASS%
if errorlevel 1 goto :Failed
&nbsp;
echo 3. Building %METALIN% from %CONFIGPATH%
%MSXSL% %CONFIGPATH% %PREDIR%DbmlPruner.xslt -o %METALIN%
if errorlevel 1 goto :Failed
&nbsp;
echo 4. Generating %CONTEXTCLASSFILE% using %METALIN%
%SQLMETAL% %METALIN% /code:%CONTEXTCLASSFILE% /language:csharp /context:%CONTEXTCLASS% /namespace:%NAMESPACE% /serialization:Unidirectional
if errorlevel 1 goto :Failed
&nbsp;
echo ======== Generation of LINQ to SQL classes completed successfully =========
endlocal
goto :EOF
&nbsp;
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:Failed
&nbsp;
echo Configuration %CONFIG% FAILED
endlocal
exit /B 1</pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.hypercomplex.co.uk/index.php/2010/03/filtering-sqlmetal-output-using-xslt/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dynamic search conditions in SQL Server</title>
		<link>http://blog.hypercomplex.co.uk/index.php/2009/07/dynamic-search-conditions-in-sql-server/</link>
		<comments>http://blog.hypercomplex.co.uk/index.php/2009/07/dynamic-search-conditions-in-sql-server/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 23:19:39 +0000</pubDate>
		<dc:creator>Alex Peck</dc:creator>
				<category><![CDATA[Software Engineering]]></category>
		<category><![CDATA[sql]]></category>

		<guid isPermaLink="false">http://blog.hypercomplex.co.uk/?p=186</guid>
		<description><![CDATA[Browsing stackoverflow I found someone had linked up this excellent reference. This is a handy reference for passing arrays into stored procedures.]]></description>
			<content:encoded><![CDATA[<p>Browsing <a href="http://stackoverflow.com/">stackoverflow</a> I found someone had linked up <a href="http://www.sommarskog.se/dyn-search-2005.html">this</a> excellent reference.</p>
<p><a href="http://www.sommarskog.se/arrays-in-sql.html">This</a> is a handy reference for passing arrays into stored procedures.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hypercomplex.co.uk/index.php/2009/07/dynamic-search-conditions-in-sql-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

