2013-03-18 3 views
0

Я использую построитель формы orbeon и хочу добавить значение к атрибуту class для некоторых элементов. Для этого я использую код ниже, но я хотел бы знать, как оптимизировать этот код. Должно быть возможно объединить два тега шаблона, потому что единственное различие заключается в том, что в случае 1 я устанавливаю атрибут класса на значение, а в случае 2 добавляю текст в атрибут класса.Оптимизация моего селектора XSLT

Возможно, это даже возможно объединить весь этот код в 1 тег шаблона? (Один с несколькими селекторами (матч) и с набором/добавить атрибут класса?

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

Да, эти шаблоны можно упростить немного. Вы можете заменить шесть у вас есть эти три:

<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:input, fr:number и fr: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> 
+0

Не могли бы Вы скажите мне, как добавить id родительского раздела fr:? Что-то вроде: ? ' ' – nickoooname

+0

Вместо 'parent :: fr: section/@ id', попробуйте:' ../ parent :: fr: section/@ id' или 'ancestor :: fr: section/@ id'. – JLRishe

Смежные вопросы