Inline c# in XSLT results in weird behavior

点点圈 提交于 2019-12-11 15:43:47

问题


I generated an xsl stylesheet with some inline c# code. I need to use it in an asmx application running on IIS. This is an excerpt of the stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="xsl in lang user" xmlns:in="http://www.composite.net/ns/transformation/input/1.0" xmlns:lang="http://www.composite.net/ns/localization/1.0" xmlns:f="http://www.composite.net/ns/function/1.0" xmlns="http://www.w3.org/1999/xhtml" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="urn:my-scripts">
  <xsl:output omit-xml-declaration="no" indent="yes" method="text"/>
  <xsl:strip-space elements="*"/>

  <msxsl:script language="C#" implements-prefix="user">
  <msxsl:assembly name="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
  <msxsl:assembly name="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />


    <msxsl:using namespace="System.Collections.Generic"/>
    <msxsl:using namespace="System.Linq"/>
    <msxsl:using namespace="System.Xml.Linq"/>
    <![CDATA[

    private static string GenerateLine(string what, int howmuch)
    {
      var unuseful_var = "";
      return string.Concat(Enumerable.Repeat(what, howmuch).ToArray()).Substring(0, howmuch);
    }

    ]]>
  </msxsl:script>

  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="lines">
    <xsl:for-each select="line">
      <xsl:value-of select="user:GenerateLine("-", 10)" />
      <xsl:text>&#10;</xsl:text>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

In the application, I use the xsl in this way:

XslCompiledTransform transformer = new XslCompiledTransform();
...
transformer.Load(xsltReader, new XsltSettings() { EnableScript = true }, null);
transformer.Transform(xmlReader, null, outputStream);
...

Everything works fine, in the test application but when I deployed the same exact code and xsl stylesheet in the web application, suddenly, the xsl transformation raises exceptions like:

  • The type or namespace name 'var' could not be found ( are you missing a using directive or assembly reference?)
  • 'System.Collections.Generic.IEnumerable' does not contain a definition for 'ToArray'

Googling I found this (maybe) related SO question but it is still without answers.

I solved the issues with some workarounds but I definitely would like to know what is going on under the wood.

Thank you

来源:https://stackoverflow.com/questions/49493100/inline-c-sharp-in-xslt-results-in-weird-behavior

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!