4

我正在使用一个由许多复杂的 XSL 转换组成的系统,每个转换都适用于大型 XML 文件。在将 XML 文件传递​​到我们的数据库之前,专有程序会在每个 XML 文件上编译 XSLT。

XSL 转换几乎总是涉及<msxsl>标签中的 C# 函数,其中许多在多个文件之间重复,代码是手动复制的。我正在尝试实现一个系统,其中函数的通用存储库存储在一个文件中,然后在被<msxsl>标签读取之前加载到 XSLT 文件中。

我的问题是我一直无法找到<msxsl>从外部文件加载代码的方法。这是我的意思的一个例子:

带有硬编码函数的变换:

...

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:msxsl="urn:schemas-microsoft-com:xslt"
            xmlns:cs="urn:my-scripts-csharp">

...

<msxsl:script language="C#" implements-prefix="cs">
  <![CDATA[
    public string emphasise(string input) {
      return input+"!";
    }
  ]]>
</msxsl:script>

...

虽然我希望从外部加载函数:

...

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
            xmlns:msxsl="urn:schemas-microsoft-com:xslt"
            xmlns:cs="urn:my-scripts-csharp">
...

<msxsl:script language="C#" implements-prefix="cs">
  <--- file loaded here --->
</msxsl:script>

...

源文件类似于:

<![CDATA[
  public string emphasise(string input) {
    return input+"!";
  }
]]>

这可能吗?这些函数通常很复杂(与此示例不同)并且无法使用 XSLT 代码重现。是否会读取外部文件中的关键字,还是必须将它们与标签一起包含在外部文件namespace中?using<msxsl:using>

我对<msxsl>标签的使用很陌生,如果我误解了一些基本的东西,请告诉我!

4

2 回答 2

3

I. Here is how to do it:

File: C:\temp\delete\c#script.xsl

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:msxsl="urn:schemas-microsoft-com:xslt"
 xmlns:cs="urn:my-scripts-csharp">  
    <msxsl:script language="C#" implements-prefix="cs">
    <![CDATA[
     public string emphasise(string input)
     {
      return input+"!";
      }
      ]]></msxsl:script>
</xsl:stylesheet>

Your transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:msxsl="urn:schemas-microsoft-com:xslt"
 xmlns:cs="urn:my-scripts-csharp">

 <xsl:include href="C:\temp\delete\c#script.xsl"/>

 <xsl:template match="/">
  <xsl:value-of select="cs:emphasise('Hello')"/>
 </xsl:template>
</xsl:stylesheet>

When this transformation is performed (on any XML document), the wanted result is produced:

Hello!

Warning: Using inline scripts like these is known to cause memory fragmentation if the transformation is repeated many times and this can lead to memory leaks.

I strongly recommend not to use inline scripts at all. A better, more efficient and safe approach is to have all necessary extension functions in an extension object -- read about the XsltArgumentList class and its AddExtensionObject() method.

于 2012-06-29T12:40:24.527 回答
2

您可以使用以下方式引用外部程序集中的代码:http: //msdn.microsoft.com/en-us/library/ms256188.aspx - 这可以提供一种将代码移动到外部位置的方法

于 2012-06-29T12:35:38.397 回答