AddExtensionObject - 性能性能、AddExtensionObject

2023-09-03 15:18:58 作者:记忆里的尘埃随风而逝

.NET XSLT引擎允许通过AddExtensionObject方法传递对象到XSLT处理引擎。

.NET XSLT engine allows passing objects to the XSLT processing engine through the AddExtensionObject method.

可以使用这个检索本地化字符串的表现发表评论的人在XSLT中使用?

Can someone comment on the performance of using this to retrieve localized strings to be used in the XSLT?

推荐答案

扩展对象 可以用来改善,如果XSLT转换的一部分被认为是效率低下的表现。

Extension objects can be used to improve performance if a part of an XSLT transformation is deemed inefficient.

虽然使用的扩展方法的方法不会降低性能(不包括越野车和低效code),他们不会提高性能显著,如果正在使用适当的XSLT技术来访问本地化的字符串

While the use of methods of an extension method would not decrease performance (excluding buggy and inefficient code), they will not improve performance significantly, if proper XSLT techniques are being used for accessing localized strings.

如果不是迫切需要扩展对象​​,这是一件好事,创建纯的XSLT解决方案。这提供了便携的好处到任何平台,它提供了符合标准的XSLT处理器

一个可以将所有的本地化字符串在一个单独的XML文件中给定的语言。该文件将使用XSLT 文档() 功能。每个字符串将被编入索引的 @msgId 使用索引构建的 < XSL:关键> 指令。在转型的个人信息将使用XSLT 键() 功能。

One could place all localized strings for a given language in a separate XML file. This file will be accessed using the XSLT document() function. Each string will be indexed by its @msgId attribute using an index build by the <xsl:key> instruction. Within the transformation an individual message will be fetched with the XSLT key() function.

下面是一个小的code为例,展示了如何从一个XML文件,其中所有语言的邮件存储检索消息由MsgId和Languge- code。为方便起见,我们已经把消息XSLT样式表本身中。在实践中,这些消息可以是在一个单独的XML文件:

Below is a small code example, showing how to retrieve a message by msgId and Languge-code from an xml file, where messages for all languages are stored. For convenience we have put the messages inside the XSLT stylesheet itself. In practice, the messages can be in a separate XML file:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:key name="kMsgByLangAndId" match="msg"
      use="concat(../@name, @msgId)"/>

    

 <xsl:param name="pLang" select="'De'"/>
 <xsl:param name="pTime" select="19"/>

 <xsl:variable name="vMsgEn">
  <msg msgId="MornGreet">Good morning.</msg>
  <msg msgId="AftnGreet">Good afternoon.</msg>
  <msg msgId="EvnGreet">Good evening.</msg>
 </xsl:variable>

 <xsl:variable name="vMsgDe">
  <msg msgId="MornGreet">Guten morgen.</msg>
  <msg msgId="AftnGreet">Guten tag.</msg>
  <msg msgId="EvnGreet">Guten abend.</msg>
 </xsl:variable>

    <xsl:template match="/">
      <xsl:variable name="vLangVarName"
           select="concat('vMsg', $pLang)"/>

    <xsl:variable name="vMsgId">
      <xsl:choose>
       <xsl:when test="not($pTime >= 12)">MornGreet</xsl:when>
       <xsl:when test="not($pTime >= 18)">AftnGreet</xsl:when>
       <xsl:otherwise>EvnGreet</xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

   <xsl:for-each select="document('')">
    <xsl:value-of select=
      "key('kMsgByLangAndId',
            concat($vLangVarName,$vMsgId)
            )"/>
   </xsl:for-each>

    </xsl:template>
</xsl:stylesheet>

在此转换应用于任何输入XML文档(忽略),通缉的结果是产生:

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

Guten异常结束。

Guten abend.