title: BizTalk 2009 XSLT and Attribute Value Templates
body: <p>I'm trying to make use of the <a href="http://www.w3.org/TR/xslt#dt-attribute-value-template" rel="nofollow noreferrer">attribute value</a> type in a <em>BizTalk</em> XSL transformation to dynamically set attributes or other element names.</p>
  
  <p>The following code is an example of an XSL template to add an attribute optionally:</p>
  
  <pre><code>&lt;xsl:template name="AttributeOptional"&gt;
    &lt;xsl:param name="value"/&gt;
    &lt;xsl:param name="attr"/&gt;
    &lt;xsl:if test="$value != ''"&gt;
      &lt;xsl:attribute name="{$attr}"&gt;
        &lt;xsl:value-of select="$value"/&gt;
      &lt;/xsl:attribute&gt;
    &lt;/xsl:if&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p><strong>Problem:</strong></p>
  
  <p>Running this script in BizTalk causes the following exception:</p>
  
  <blockquote>
    <p>"Exception from HRESULT: 0x80070002)"</p>
  </blockquote>
  
  <p>One alternative I thought about was to call a <code>msxsl:script</code> function to do the same thing, but I can not handle the XSL output context from inside the function.</p>
  
  <p>An ideas?</p>