0

我们将 xslt 1.0 与 Xpath 1.0 和 XalanJ 2.7.1 一起使用。我们对模板进行分析并尝试减少调用时间。所以最大的热点是:

<xsl:template name="tplAttribute">
    <xsl:param name="sourceObject" />
    <xsl:param name="sourceName" />
    <xsl:param name="targetName" />

    <xsl:variable name="sourceAttribute" select="$sourceObject/attribute[@name = $sourceName]" />
    <xsl:if test="$sourceAttribute">
        <dcx:attribute name="{$targetName}">
            <xsl:value-of select="$sourceAttribute" />
        </attribute>
    </xsl:if>
</xsl:template>

871 次点击的平均时间为 103 次,可变时间为 59 毫秒。

有没有更好的解决方案来减少转换的时间?

编辑: 处理“sourceObject”时调用输入结构模板:

object
  tplAttribute 
  tplAttribute
  tplAttributeDefault
  tplAttribute
  tplAttributeSomeDifferentLogic
  tplAttribute
  tplAttributeOther

在此处输入图像描述

4

1 回答 1

0

定义一个键<xsl:key name="att-by-name" match="attribute" use="@name"/>,然后使用

<xsl:for-each select="$sourceObject">
  <xsl:if test="key('att-by-name', $sourceName)">
            <dcx:attribute name="{$targetName}">
                <xsl:value-of select="key('att-by-name', $sourceName)" />
            </attribute>
  </xsl:if>
</xsl:for-each>

代替

    <xsl:variable name="sourceAttribute" select="$sourceObject/attribute[@name = $sourceName]" />
    <xsl:if test="$sourceAttribute">
        <dcx:attribute name="{$targetName}">
            <xsl:value-of select="$sourceAttribute" />
        </attribute>
    </xsl:if>

您可能需要使用 sourceObject 向我们展示文档的确切结构,如果元素的name属性attribute标识了attribute您在完整文档中查找的元素,则所示的基于键的方法将起作用。如果您只是查看子树,那么您可能需要在键值中包含一个 id。

于 2016-10-03T14:54:57.637 回答