2016-10-03 59 views
0

我們在xpath 1.0和XalanJ 2.7.1中使用xslt 1.0。 我們對我們的模板進行分析並嘗試縮短通話時間。所以出最大的熱點是:使用XSLT 1.0進行xslt模板處理期間的優化?

<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

+0

有多少「屬性」元素通常會作爲$ sourceObject的子元素被找到? –

+0

取決於從20到100.所以我們有$ sourceObject,並且在其中我們必須在其中找到一個特定的屬性。但是,我們必須尋找另一個,等等。在某些情況下,我們有不同的邏輯 - tplAttributeOther等。所以我不確定變量的創建是否會減慢進程,或者如果我們有兩個選擇而不是sourceAttribute,基於處理器優化,var會更快嗎? – Xelian

+0

我不知道Xalan的內部。 Saxon-EE可能會創建一個索引來評估這一個 - 試一試,讓我們知道結果。 –

回答

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文檔的確切結構,重點爲基礎的方法如圖所示,如果attribute元素的name屬性標識attribute元素(S)將工作,你正在尋找完整的文件。如果您只查看子樹,則可能需要在鍵值中包含一個ID。