2013-04-29 30 views
1

以下XSLT代碼工作的XSLT元素,但我想避免這五個線的冗餘發生:創建其中使用使用屬性集取決於屬性值

<xsl:element name="TextBlock" use-attribute-sets="Heading2"> 
     <xsl:call-template name="process-element"> 
     <xsl:with-param name="attr" select="'text|Text'" /> 
     </xsl:call-template> 
    </xsl:element> 

唯一的不同之是使用的屬性集。屬性集取決於LabelWrapper.theme屬性。我已經嘗試使用變量,但沒有運氣。

問題:

如何避免代碼冗餘線路?

有沒有更好的解決方案待辦事項?

XSL樣式表:

<xsl:template match="LabelWrapper"> 
    <xsl:choose> 
     <xsl:when test="./@theme = 'Heading1'"> 
     <xsl:element name="TextBlock" use-attribute-sets="Heading1"> 
      <xsl:call-template name="process-element"> 
      <xsl:with-param name="attr" select="'text|Text'" /> 
      </xsl:call-template> 
     </xsl:element> 
     </xsl:when> 

     <xsl:when test="./@theme = 'Emphasis'"> 
     <xsl:element name="TextBlock" use-attribute-sets="Heading2"> 
      <xsl:call-template name="process-element"> 
      <xsl:with-param name="attr" select="'text|Text'" /> 
      </xsl:call-template> 
     </xsl:element> 
     </xsl:when> 

     <xsl:when test="./@theme = 'Title'"> 
     <xsl:element name="TextBlock" use-attribute-sets="Title"> 
      <xsl:call-template name="process-element"> 
      <xsl:with-param name="attr" select="'text|Text'" /> 
      </xsl:call-template> 
     </xsl:element> 
     </xsl:when> 

     <xsl:otherwise> 
     <xsl:element name="TextBlock" use-attribute-sets="Normal"> 
      <xsl:call-template name="process-element"> 
      <xsl:with-param name="attr" select="'text|Text'" /> 
      </xsl:call-template> 
     </xsl:element> 
     </xsl:otherwise>  
    </xsl:choose> 
    </xsl:template> 

的(簡化的)XML輸入看起來像這樣:

<LabelWrapper id="lblHeader" text="Big Header" theme="Heading1" /> 
<LabelWrapper id="lblHeader" text="normal text" theme="Normal" /> 
<LabelWrapper id="lblHeader" text="page title" theme="Title" /> 
<LabelWrapper id="lblHeader" text="Small Header " theme="Heading2" /> 

使用屬性集(樣品)

<xsl:attribute-set name="Heading1"> 
    <xsl:attribute name="FontFamily">Segoe UI</xsl:attribute> 
    <xsl:attribute name="FontSize">30px</xsl:attribute> 
    <xsl:attribute name="FontStyle">Normal</xsl:attribute> 
    <xsl:attribute name="FontWeight">Normal</xsl:attribute> 
    </xsl:attribute-set> 

    <xsl:attribute-set name="Heading2"> 
    <xsl:attribute name="FontFamily">Segoe UI</xsl:attribute> 
    <xsl:attribute name="FontSize">16px</xsl:attribute> 
    <xsl:attribute name="FontStyle">Normal</xsl:attribute> 
    <xsl:attribute name="FontWeight">Bold</xsl:attribute> 
    </xsl:attribute-set> 
+1

我認爲您應該發佈一個輸入XML樣本以及您想用XSLT創建的相應輸出,然後就如何縮短代碼提出建議更容易。有了目前爲止提供的信息,我不知道'xsl:when test'是否不應該被'xsl:template match'替代。但是,由於你的問題主要是縮短重複使用'use-attribute-sets',我不確定有什麼辦法,因爲該屬性值不允許XPath表達式,而只是一個靜態的限定名稱列表。 – 2013-04-29 11:44:01

+0

添加了xml測試輸入和已用屬性集的示例 – Joel 2013-04-29 11:51:46

回答

1

我解決通過分裂t來解決問題他模板分成兩部分。第一個用於一般轉換

<xsl:template match="LabelWrapper"> 
    <xsl:element name="TextBlock"> 
     <xsl:call-template name="process-element"> 
     <xsl:with-param name="attr" select="'text|Text'" /> 
     </xsl:call-template> 
    </xsl:element> 
    </xsl:template> 

第二個屬性集。

<xsl:template match="LabelWrapper/@theme|TextBoxWrapper/@theme|RadioButtonWrapper/@theme|CheckBoxWrapper/@theme" mode="to-attr"> 
    <xsl:choose> 
     <xsl:when test=". = 'Heading1'"> 
     <xsl:attribute name="FontFamily">Segoe UI</xsl:attribute> 
     <xsl:attribute name="FontSize">30px</xsl:attribute> 
     <xsl:attribute name="FontStyle">Normal</xsl:attribute> 
     <xsl:attribute name="FontWeight">Normal</xsl:attribute> 
     </xsl:when> 

     <xsl:when test=". = 'Heading2'"> 
     <xsl:attribute name="FontFamily">Segoe UI</xsl:attribute> 
     <xsl:attribute name="FontSize">16px</xsl:attribute> 
     <xsl:attribute name="FontStyle">Normal</xsl:attribute> 
     <xsl:attribute name="FontWeight">Bold</xsl:attribute> 
     </xsl:when> 

     <xsl:when test=". = 'Title'"> 
     <xsl:attribute name="FontFamily">Segoe UI Light</xsl:attribute> 
     <xsl:attribute name="FontSize">23px</xsl:attribute> 
     <xsl:attribute name="FontStyle">Normal</xsl:attribute> 
     <xsl:attribute name="FontWeight">Normal</xsl:attribute> 
     </xsl:when> 

     <xsl:when test=". = 'Emphasis'"> 
     <xsl:attribute name="FontFamily">Segoe UI</xsl:attribute> 
     <xsl:attribute name="FontSize">12px</xsl:attribute> 
     <xsl:attribute name="FontStyle">Normal</xsl:attribute> 
     <xsl:attribute name="FontWeight">Bold</xsl:attribute> 
     </xsl:when> 

     <xsl:otherwise> 
     <xsl:attribute name="FontFamily">Segoe UI</xsl:attribute> 
     <xsl:attribute name="FontSize">12px</xsl:attribute> 
     <xsl:attribute name="FontStyle">Normal</xsl:attribute> 
     <xsl:attribute name="FontWeight">Normal</xsl:attribute> 
     </xsl:otherwise> 
    </xsl:choose> 
    </xsl:template>