次の XSLT コードは機能しますが、これらの 5 行の冗長な発生を避けたいと考えています。
<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>