使用XSLT 1.0在xslt处理模板期间进行优化?

时间:2016-10-03 14:41:34

标签: xml xpath xslt-1.0 xalan oxygenxml

我们使用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次,变化为59ms。

有没有更好的解决方案来缩短转型时间?

修改 进程'sourceObject'时输入结构模板调用:

object
  tplAttribute 
  tplAttribute
  tplAttributeDefault
  tplAttribute
  tplAttributeSomeDifferentLogic
  tplAttribute
  tplAttributeOther

enter image description here

1 个答案:

答案 0 :(得分: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元素,则显示的基于密钥的方法将起作用( s)您正在寻找完整的文件。如果您只是查看子树,那么您可能需要在键值中包含一个id。