开发者

.net 4 xslt transformation Extension Function broken

开发者 https://www.devze.com 2023-01-10 11:39 出处:网络
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.

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.


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());
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号