title: Using the 'current()' function in a pattern to apply custom templates to Nodes
body: <p>I am implementing a system in with a user can generate a document on a collection of <em>Items</em>. An item contains a <em>Name</em> and a <em>ItemTypeId</em>. A user can select what template to apply to what item of a certain type. This is stored in a <em>composition</em>-object which contains a mapping of itemTypeId's to TemplateId's.</p>
  
  <p>So the XML-data used to generate the document looks like this:  </p>
  
  <p><strong>XML:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;data&gt;
    &lt;composition&gt;
      &lt;ItemType&gt;
        &lt;ItemTypeId&gt;IT1&lt;/ItemTypeId&gt;
        &lt;TemplateId&gt;T1&lt;/TemplateId&gt;
      &lt;/ItemType&gt;
      &lt;ItemType&gt;
        &lt;ItemTypeId&gt;IT2&lt;/ItemTypeId&gt;
        &lt;TemplateId&gt;T3&lt;/TemplateId&gt;
      &lt;/ItemType&gt;
      &lt;ItemType&gt;
        &lt;ItemTypeId&gt;IT3&lt;/ItemTypeId&gt;
        &lt;TemplateId&gt;T2&lt;/TemplateId&gt;
      &lt;/ItemType&gt;
      &lt;ItemType&gt;
        &lt;ItemTypeId&gt;IT4&lt;/ItemTypeId&gt;
        &lt;TemplateId&gt;T1&lt;/TemplateId&gt;
      &lt;/ItemType&gt;
    &lt;/composition&gt;
    &lt;items&gt;
      &lt;item&gt;
        &lt;ItemTypeId&gt;IT1&lt;/ItemTypeId&gt;
        &lt;Name&gt;A&lt;/Name&gt;
      &lt;/item&gt;
      &lt;item&gt;
        &lt;ItemTypeId&gt;IT2&lt;/ItemTypeId&gt;
        &lt;Name&gt;B&lt;/Name&gt;
      &lt;/item&gt;
      &lt;item&gt;
        &lt;ItemTypeId&gt;IT3&lt;/ItemTypeId&gt;
        &lt;Name&gt;C&lt;/Name&gt;
      &lt;/item&gt;
      &lt;item&gt;
        &lt;ItemTypeId&gt;IT2&lt;/ItemTypeId&gt;
        &lt;Name&gt;D&lt;/Name&gt;
      &lt;/item&gt;
    &lt;/items&gt;
  &lt;/data&gt;
  </code></pre>
  
  <p>To generate the document I want to use an XSLT containing 4 templates.</p>
  
  <p><strong>XSLT:</strong>
    
    </p>
  
  <pre><code>&lt;xsl:template match="/"&gt;
    &lt;html&gt;
    &lt;body&gt;
    &lt;h2&gt;Document&lt;/h2&gt;
  &lt;xsl:for-each select="/data/items/item"&gt;
  &lt;xsl:variable name="itemTypeId" select="current()/ItemTypeId" /&gt;
    &lt;xsl:apply-templates select="current()"/&gt;
  &lt;/xsl:for-each&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="item[/data/composition/ItemType[ItemTypeId = current()/ItemTypeId]/TemplateId='T1']"&gt;
   &lt;h1&gt;T1&lt;/h1&gt;
   Item &lt;xsl:value-of select="current()/Name"/&gt; 
   with type &lt;xsl:value-of select="current()/ItemTypeId"/&gt;&lt;br/&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="item[/data/composition/ItemType[ItemTypeId = current()/ItemTypeId]/TemplateId='T2']"&gt;
   &lt;h1&gt;T2&lt;/h1&gt;
   Item &lt;xsl:value-of select="current()/Name"/&gt; 
   with type &lt;xsl:value-of select="current()/ItemTypeId"/&gt;&lt;br/&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="item[/data/composition/ItemType[ItemTypeId = current()/ItemTypeId]/TemplateId='T3']"&gt;
   &lt;h1&gt;T3&lt;/h1&gt;
   Item &lt;xsl:value-of select="current()/Name"/&gt; 
   with type &lt;xsl:value-of select="current()/ItemTypeId"/&gt;&lt;br/&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="item[/data/composition/ItemType[ItemTypeId = current()/ItemTypeId]/TemplateId='T4']"&gt;
   &lt;h1&gt;T4&lt;/h1&gt;
   Item &lt;xsl:value-of select="current()/Name"/&gt; 
   with type &lt;xsl:value-of select="current()/ItemTypeId"/&gt;&lt;br/&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="item"&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p></p>
  
  <p>I use the current() function in the match pattern to look in the composition and see if the template needs to be applied or not. This works perfectly in online XSLT editor of <a href="http://www.w3schools.com/" rel="nofollow">W3Schools</a> and generates the following document:</p>
  
  <p><strong>Output (On W3Schools xslt editor):</strong></p>
  
  <blockquote>
    <p><h2>Document</h2><h1>T1</h1>  Item A with type
    IT1<h1>T3</h1>Item B with type IT2<h1>T2</h1>  Item C
    with type IT3 <h1>T3</h1>  Item D with type IT2
    </p>
  </blockquote>
  
  <p>However, when I try to implement this in a .Net application I get the following error:</p>
  
  <blockquote>
    <p>The 'current()' function cannot be used in a pattern.</p>
  </blockquote>
  
  <p>It seems to me that .NET does not allow the use of <em>current()</em> in match patterns. Does anybody know a way around this problem? Or can somebody point out a  flaw in my logic?</p>
  
  <p><strong>Edit</strong>
  I apply the xslt using the standard .NET methods in <em>System.Xml.Xsl</em> like so:</p>
  
  <pre><code>  XElement data = XElement.Parse("xml is entered here");
    XElement stylesheet = XElement.Parse("xslt is entered here");
    XslCompiledTransform compiledTransform = new XslCompiledTransform();
    compiledTransform.Load(stylesheet.CreateReader());
    // Create an xml writer to write to the result stream.
    using (XmlWriter writer = XmlWriter.Create(result, compiledTransform.OutputSettings))
    {
        // Apply the transformation.
        compiledTransform.Transform(data.CreateReader(), null, writer);
    }
    // Return the result.
    return result;
  </code></pre>