title: "Execution of the 'document()' function was prohibited." where EnableDocumentFunction set to true?
body: <p>I am getting an intermittent System.Xml.Xsl.XslTransformException exception in our production environment when attempting an xslt transform, unfortunately I cannot replicate this in the development environment.</p>
  
  <p>The exception spits out further details:</p>
  
  <blockquote>
    <p>Execution of the 'document()' function was prohibited. Use the
    XsltSettings.EnableDocumentFunction property to enable it. An error
    occurred at C:\path\to\file\CDS.xsl(16,3).</p>
  </blockquote>
  
  <p>However the EnableDocumentFunction property <em>is</em> set to true in the processing code:</p>
  
  <pre><code>private void Transform()
  {
      var keepTrying = true;
      var tryCount = 0;
      const int maxRetrys = 3;
  
      while (keepTrying)
      {
          try
          {
              var xmlResolver = new XmlUrlResolver();
  
              using (var xmlFile = new XmlNodeReader(_xDoc))
              {
                  var settings = new XmlReaderSettings
                                     {
                                         XmlResolver = xmlResolver,
                                         ProhibitDtd = false,
                                         DtdProcessing = DtdProcessing.Ignore
                                     };
  
                  using (var xsl = XmlReader.Create(_xslPath, settings))
                  {
                      var xslt = new XslCompiledTransform(true);
                      xslt.Load(xsl, new XsltSettings { EnableDocumentFunction = true }, xmlResolver);
  
                      var sb = new StringBuilder();
                      using (var writer = XmlWriter.Create(sb, xslt.OutputSettings))
                      {
                          xslt.Transform(xmlFile, null, writer, xmlResolver); // errors out here.
                      }
  
                      var xhtml = sb.ToString();
                      _transformedXml = xhtml;
                      _isTransformed = true;
  
                      xsl.Close();
                  }
              }
  
              keepTrying = false;
          }
          catch (System.Xml.Xsl.XsltException ex)
          {
              ExceptionPolicy.HandleException(ex, "ExceptionLogging");
  
              tryCount++;
              if (tryCount &gt; maxRetrys)
              {
                  keepTrying = false;
                  throw;
              }
          }
      }
  }
  </code></pre>
  
  <p>The xslt file is provided by a third party and automatically updated, so rewriting it is not an option.  Here is the top of it, slightly modified for privacy reasons:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  
  &lt;!--
       Interaction_550.xsl : 20110916
  --&gt;
  &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:example="http://www.example.com" version="1.0"&gt;
    &lt;xsl:param name="D2DSeverityFilter"&gt;&lt;/xsl:param&gt;
    &lt;xsl:param name="D2HSeverityFilter"&gt;&lt;/xsl:param&gt;
    &lt;xsl:param name="DocumentationFilter"&gt;&lt;/xsl:param&gt;
    &lt;xsl:output method="html"/&gt;
    &lt;xsl:key name="d2d_sev_level-lookup" match="example:d2d_sev_level" use="@name"/&gt;
    &lt;xsl:key name="d2h_sev_level-lookup" match="example:d2h_sev_level" use="@name"/&gt;
        &lt;xsl:key name="d2l_sev_level-lookup" match="example:d2l_sev_level" use="@name"/&gt;
        &lt;xsl:key name="preg_cat-lookup" match="example:preg_cat" use="@cat"/&gt;
    &lt;xsl:key name="doc_level-lookup" match="example:doc_level" use="@name"/&gt;
    &lt;xsl:variable name="d2d_sev_level-top" select="document('')/*/example:d2d_sev_levels"/&gt;
    &lt;xsl:variable name="d2h_sev_level-top" select="document('')/*/example:d2h_sev_levels"/&gt;
        &lt;xsl:variable name="d2l_sev_level-top" select="document('')/*/example:d2l_sev_levels"/&gt;
  
    &lt;xsl:variable name="doc_level-top" select="document('')/*/example:doc_levels"/&gt;
        &lt;xsl:variable name="preg_cat-top" select="document('')/*/example:preg_cats"/&gt;
    &lt;xsl:template match="/"&gt;
      &lt;head&gt;
        &lt;style type="text/css"&gt;
  body {
  font-family : arial,sans serif,helvetica;
  }
  ...
  </code></pre>
  
  <p>How can I:</p>
  
  <ul>
  <li>fix this so that it does not happen at all? </li>
  <li>failing that, how could I go about replicating this in dev?</li>
  </ul>