Xml 优化我的XSLT选择器

Xml 优化我的XSLT选择器,xml,xslt,orbeon,Xml,Xslt,Orbeon,我正在使用orbeon表单生成器,希望为某些元素的class属性添加一个值。要做到这一点,我使用下面的代码,但我想知道如何优化这段代码。必须能够组合每两个模板标记,因为唯一的区别是案例1我将class属性设置为一个值,案例2中我向class属性添加一些文本 也许可以将所有这些代码组合成一个模板标记?(一个具有多个选择器(匹配)和set/append class属性 Case 1: <xsl:template match="xforms:input/@id">

我正在使用orbeon表单生成器,希望为某些元素的class属性添加一个值。要做到这一点,我使用下面的代码,但我想知道如何优化这段代码。必须能够组合每两个模板标记,因为唯一的区别是案例1我将class属性设置为一个值,案例2中我向class属性添加一些文本

也许可以将所有这些代码组合成一个模板标记?(一个具有多个选择器(匹配)和set/append class属性

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>
案例1:
tsbinput-
案例2:
tsbinput-
案例1:
tsbinput-
案例2:
tsbinput-
案例1:
tsbinput-
案例2:
tsbinput-
请帮帮我。
谢谢,Nico

是的,这些模板可以简化一点。您可以用这三个模板替换现有的六个模板:

  <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>
然后您可以将上述三个模板更改为:

  <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吗?类似于:?
而不是
父::fr:section/@id
,请尝试:
。/parent::fr:section/@id
祖先::fr:section/@id
  <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>