【问题标题】:Optimizing my XSLT selector优化我的 XSLT 选择器
【发布时间】:2013-03-18 09:13:02
【问题描述】:

我正在使用 orbeon 表单构建器,并希望为某些元素的 class 属性添加一个值。为此,我使用下面的代码,但我想知道如何优化此代码。必须可以组合每 2 个模板标签,因为唯一的区别是案例 1 我将类属性设置为一个值,而在案例 2 中我将一些文本添加到类属性..

也许甚至可以将所有这些代码组合成 1 个模板标签? (一个具有多个选择器(匹配)并具有 set/append 类属性?

Case 1: 
   <xsl:template match="xforms:input/@id">
        <xsl:attribute name="id" select="."/> 
        <xsl:attribute name="class">tsbinput-<xsl:value-of select="."/></xsl:attribute>      
   </xsl:template> 
Case 2:
   <xsl:template match="xforms:input/@class">
        <xsl:attribute name="class"><xsl:value-of select="."/> tsbinput-<xsl:value-of select="../@id"/></xsl:attribute>      
   </xsl:template>

Case 1:    
    <xsl:template match="fr:number/@id">
        <xsl:attribute name="id" select="."/> 
        <xsl:attribute name="class">tsbinput-<xsl:value-of select="."/></xsl:attribute>      
   </xsl:template> 
Case 2:
   <xsl:template match="fr:number/@class">
        <xsl:attribute name="class"><xsl:value-of select="."/> tsbinput-<xsl:value-of select="../@id"/></xsl:attribute>      
   </xsl:template>

Case 1:
   <xsl:template match="fr:textcount/@id">
        <xsl:attribute name="id" select="."/> 
        <xsl:attribute name="class">tsbinput-<xsl:value-of select="."/></xsl:attribute>      
   </xsl:template> 
Case 2:
   <xsl:template match="fr:textcount/@class">
        <xsl:attribute name="class"><xsl:value-of select="."/> tsbinput-<xsl:value-of select="../@id"/></xsl:attribute>      
   </xsl:template>

请帮助我。 谢谢,尼科

【问题讨论】:

    标签: xml xslt orbeon


    【解决方案1】:

    是的,这些模板可以稍微简化一下。你可以用这三个替换你拥有的六个:

      <xsl:template match="@id[parent::xforms:input or 
                               parent::fr:number or 
                               parent::fr:textcount]">
        <xsl:copy />
        <xsl:attribute name="class">
          <xsl:value-of select="concat(../@class, ' tsbinput-', .)"/>
        </xsl:attribute>
      </xsl:template>
    
      <xsl:template match="@class[../@id]
                                 [parent::xforms:input or 
                                  parent::fr:number or 
                                  parent::fr:textcount]" />
    
      <xsl:template match="@class[not(../@id)]
                                 [parent::xforms:input or 
                                  parent::fr:number or 
                                  parent::fr:textcount]">
        <xsl:copy />
      </xsl:template>
    

    如果可以保证xforms:inputfr:numberfr:textcount始终具有@id 属性,则可以删除第三个模板。

    进一步简化的一种方法是将此键添加到 XSLT 的顶部:

      <xsl:key name="kAdjustClass" 
               match="xforms:input | fr:number | fr:textcount"
               use="name()" />
    

    然后你可以把上面三个模板改成这样:

      <xsl:template match="@id[key('kAdjustClass', name(..))]">
        <xsl:copy />
        <xsl:attribute name="class">
          <xsl:value-of select="concat(../@class, ' tsbinput-', .)"/>
        </xsl:attribute>
      </xsl:template>
      <xsl:template match="@class[../@id][key('kAdjustClass', name(..))]" />
      <xsl:template match="@class[not(../@id)][key('kAdjustClass', name(..))]">
        <xsl:copy />
      </xsl:template>
    

    【讨论】:

    • 你能告诉我如何添加父 fr:section 的 id 吗?就像是: ? &lt;xsl:template match="@id[key('kAdjustClass', name(..))]"&gt; &lt;xsl:copy /&gt; &lt;xsl:attribute name="class"&gt; &lt;xsl:value-of select="concat(../@class, ' 123 ', parent::fr:section/@id, '-tsb2input-', .)"/&gt; &lt;/xsl:attribute&gt; &lt;/xsl:template&gt;
    • 代替parent::fr:section/@id,请尝试:../parent::fr:section/@idancestor::fr:section/@id
    猜你喜欢
    • 1970-01-01
    • 2011-08-27
    • 2011-06-09
    • 1970-01-01
    • 1970-01-01
    • 2014-11-25
    • 2020-07-29
    • 2012-07-20
    • 2011-11-18
    相关资源
    最近更新 更多