Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Execution of the 'document()' function was prohibited." where EnableDocumentFunction set to true?

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.

The exception spits out further details:

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).

However the EnableDocumentFunction property is set to true in the processing 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 > maxRetrys)
            {
                keepTrying = false;
                throw;
            }
        }
    }
}

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:

<?xml version="1.0"?>

<!--
     Interaction_550.xsl : 20110916
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:example="http://www.example.com" version="1.0">
  <xsl:param name="D2DSeverityFilter"></xsl:param>
  <xsl:param name="D2HSeverityFilter"></xsl:param>
  <xsl:param name="DocumentationFilter"></xsl:param>
  <xsl:output method="html"/>
  <xsl:key name="d2d_sev_level-lookup" match="example:d2d_sev_level" use="@name"/>
  <xsl:key name="d2h_sev_level-lookup" match="example:d2h_sev_level" use="@name"/>
      <xsl:key name="d2l_sev_level-lookup" match="example:d2l_sev_level" use="@name"/>
      <xsl:key name="preg_cat-lookup" match="example:preg_cat" use="@cat"/>
  <xsl:key name="doc_level-lookup" match="example:doc_level" use="@name"/>
  <xsl:variable name="d2d_sev_level-top" select="document('')/*/example:d2d_sev_levels"/>
  <xsl:variable name="d2h_sev_level-top" select="document('')/*/example:d2h_sev_levels"/>
      <xsl:variable name="d2l_sev_level-top" select="document('')/*/example:d2l_sev_levels"/>

  <xsl:variable name="doc_level-top" select="document('')/*/example:doc_levels"/>
      <xsl:variable name="preg_cat-top" select="document('')/*/example:preg_cats"/>
  <xsl:template match="/">
    <head>
      <style type="text/css">
body {
font-family : arial,sans serif,helvetica;
}
...

How can I:

  • fix this so that it does not happen at all?
  • failing that, how could I go about replicating this in dev?
like image 566
CamM Avatar asked Oct 25 '25 16:10

CamM


1 Answers

Alternatively with MS XslCompiledTransform class you can use a XsltSettings class to avoid this error as it described by the error itself. from MSDN

like image 179
Tony Shih Avatar answered Oct 27 '25 04:10

Tony Shih



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!