1

次の 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>
4

1 に答える 1

2

テンプレートを2つの部分に分割することで問題を解決しました。一般的な変換の最初の

  <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>

2 つ目は属性セットです。

<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> 
于 2013-04-29T12:32:48.453 に答える