【问题标题】:Create XSLT elements where the used use-attribute-set is depending on an attribute value创建 XSLT 元素,其中使用的 use-attribute-set 取决于属性值
【发布时间】:2013-04-29 11:34:06
【问题描述】:

以下 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 表达式,而只是限定名称的静态列表。
  • 添加了 xml 测试输入和使用的属性集示例

标签: xml xslt


【解决方案1】:

我通过将模板分成两部分解决了这个问题。第一个为一般变换

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    • 2016-11-16
    • 1970-01-01
    • 2017-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多