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