.net 4 xslt transformation Extension Function broken

风格不统一 提交于 2019-12-31 03:36:06

问题


I'm in the process of upgrading an asp.net v3.5 web app. to v4 and I'm facing some problems with XSLT transformations I use on XmlDataSource objects.

Part of a XSLT file:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:HttpUtility="ds:HttpUtility">
  <xsl:output method="xml" indent="yes" encoding="utf-8"/>
  <xsl:template match="/Menus">
    <MenuItems>
      <xsl:call-template name="MenuListing" />
    </MenuItems>
  </xsl:template>

  <xsl:template name="MenuListing">
    <xsl:apply-templates select="Menu" />
  </xsl:template>

  <xsl:template match="Menu">
      <MenuItem>
        <xsl:attribute name="Text">
          <xsl:value-of select="HttpUtility:HtmlEncode(MenuTitle)"/>
        </xsl:attribute>
        <xsl:attribute name="ToolTip">
          <xsl:value-of select="MenuTitle"/>
        </xsl:attribute>
      </MenuItem>
  </xsl:template>
</xsl:stylesheet>

The problem seems to be in the line

<xsl:value-of select="HttpUtility:HtmlEncode(MenuTitle)"/>

Removing this and replacing it with a normal text, it will work. The way I setup the XML datasource:

    xmlDataSource.TransformArgumentList.AddExtensionObject("ds:HttpUtility", new System.Web.HttpUtility());
    xmlDataSource.Data = Cache.FetchPageMenu();

I've been searching the Microsoft pages for any changes in v4, but can't find any. All this worked fine in v3.5 (and before v2). Not receiving any errors, the data is just not displaying.


回答1:


The problem seems to be that .NET 4.0 introduces an additional overload for HttpUtility.HtmlEncode. Up to .NET 3.5 there were the following overloads:

public static string HtmlEncode(string s);
public static void HtmlEncode(string s, TextWriter output);

.NET 4.0 also has the following method:

public static string HtmlEncode(object value);

This results in an XslTransformException:

(Ambiguous method call. Extension object 'ds:HttpUtility' contains multiple 'HtmlEncode' methods that have 1 parameter(s).

You probably don't see the exception because it is caught somewhere and not immediately reported.

Using .NET Framework classes as extension objects is a fragile thing as a new Framework version might break your transformation.

A fix would be to create a custom wrapper class and use that as extension object. This wrapper class may not have overloads with the same number of parameters:

class ExtensionObject
{
    public string HtmlEncode(string input)
    {
        return System.Web.HttpUtility.HtmlEncode(input);
    }
}

//...
XsltArgumentList arguments = new XsltArgumentList();
arguments.AddExtensionObject("my:HttpUtility", new ExtensionObject());


来源:https://stackoverflow.com/questions/3409767/net-4-xslt-transformation-extension-function-broken

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