Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception "stylesheet is too complex" when loading large XSLT in .NET 4.5

Tags:

c#

.net-4.5

xslt

I am getting an exception when attempting to load an XSLT stylesheet. The XSLT stylesheet is very large (almost 8,000 lines). Unfortunately, I do not have any control over this, and I am unable to refactor the stylesheet to make it smaller.

We recently upgraded to .Net Framework 4.5. The following command below worked just fine prior to the upgrade (we were using .Net Framework 4.0). After the upgrade we receive an XsltException that says "The stylesheet is too complex" on thetransform.Load line.

I was hoping there was some new setting that would say "Make this command work the way it did in 4.0", but I could not find anything anywhere.

Does anybody know of some reason why this might suddenly be a problem in the 4.5 version? How can it be resolved?

XslCompiledTransform transform = new XslCompiledTransform();
transform.Load(XmlReader.Create(report), new XsltSettings { EnableScript = true }, new XmlUrlResolver());

report is a MemoryStream which contains the large XSLT stylesheet.

like image 829
Gene S Avatar asked Apr 18 '13 19:04

Gene S


People also ask

What is XSLT style sheets?

Extensible Stylesheet Language Transformations (XSLT) is an XML-based language used, in conjunction with specialized processing software, for the transformation of XML documents.

What is XSLT in HTML?

XSLT (Extensible Stylesheet Language Transformations) is a language originally designed for transforming XML documents into other XML documents, or other formats such as HTML for web pages, plain text or XSL Formatting Objects, which may subsequently be converted to other formats, such as PDF, PostScript and PNG.

What is XSLT standard?

The Extensible Stylesheet Language Transformation (XSLT) standard specifies a language definition for XML data transformations. XSLT is used to transform XML documents into XHTML documents, or into other XML documents.

Is XSLT easy?

XSLT has several important advantages over all other programming languages when it comes to XML data transformation: It has been designed to work with enterprise XML technologies. It makes most data conversion tasks very easy to understand and implement.


2 Answers

Turns out this is a feature/defect in the .Net Framework 4.5. Message from Microsoft...

We recently released a fix for this in a hotfix rollup. See http://support.microsoft.com/kb/2828843 for .NET Framework 4.0 and http://support.microsoft.com/kb/2828841 for .NET Framework 4.5.

Then add this to your config file and the problem should go away.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="system.xml">
      <section name="xslt" type="System.Xml.XmlConfiguration.XsltConfigSection, System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
    </sectionGroup>
  </configSections>
  <system.xml>
    <xslt limitXPathComplexity="false"/>
  </system.xml>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
  </startup>
</configuration>

This solved my problem.

like image 53
Gene S Avatar answered Sep 18 '22 21:09

Gene S


I had the same issue, and isolated it to a machine-generated choice-element with more than 1500 xsl:when-elements. Further testing proved .NET 4.7 will give the "too complex" exception at 789 when-elements or more.

Setting limitXPathComplexity to false in app.config crashes the program with an StackOverflowException instead.

The same code on .NET Core 2.0 throws StackOverflowException at appx 2800 elements. I have reported the bug to both teams, but don't expect it to be prioritized anytime soon.

like image 31
EventHorizon Avatar answered Sep 22 '22 21:09

EventHorizon