.NET 4 XSLT转换扩展功能打破 [英] .net 4 xslt transformation Extension Function broken

查看:134
本文介绍了.NET 4 XSLT转换扩展功能打破的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在升级asp.net 3.5版Web应用程序的过程。至v4和我面临的一些问题,我上的XmlDataSource对象使用XSLT转换。

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.

一个XSLT文件的部分:

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)"/>

删除此,用一个普通的文本替换它,它会工作。就是我设置的XML数据源:

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

我一直在寻找在V4任何改变微软的网页,但找不到任何。所有这一切都正常工作的v3.5版本(和v2之前)。没有收到任何错误,数据只是没有显示。

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.

推荐答案

这个问题似乎是,.NET 4.0中引入了一个额外的过载 HttpUtility.HtmlEn code 。截至.NET 3.5有以下重载:

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还具有以下的方法:

.NET 4.0 also has the following method:

public static string HtmlEncode(object value);

这将导致一个 XslTransformException

(不明确的方法调用扩展对象DS:HttpUtility。包含多个'HtmlEn code'有1个参数(S)的方法

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

使用.NET Framework类作为扩展对象是个脆弱的东西作为一个新的框架版本可能会破坏你的转型。

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

这篇关于.NET 4 XSLT转换扩展功能打破的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆