我有任何元素與<var>
孩子匹配的XSL模板:xslt:如何在處理過程中將兩個模板應用於同一個節點?
<xsl:template match="*[var]">
<xsl:copy >
<xsl:attribute name="editable">
<xsl:for-each select="var[@attr]">
<xsl:value-of
select="concat(@attr,
substring(' ',
1 div (position()!=last())))"/>
</xsl:for-each>
</xsl:attribute>
<xsl:attribute name="constraints">
<xsl:for-each select="var[@ok]">
<xsl:value-of
select="concat(@attr,':',@ok,
substring(';',
1 div (position()!=last())))"/>
</xsl:for-each>
</xsl:attribute>
<!-- if this is an <a> then we have to put the stuff inside it inside it -->
<xsl:apply-templates select="@*" />
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
它加到了attr
S中的變種元素融入母公司的editable
屬性;和ok
s分成constraint
s。
然後,我有任何<field>
元素相匹配的模板:
<xsl:template match="field">
<span>
<xsl:attribute name="field">
<xsl:choose>
<xsl:when test="boolean(@name)">
<xsl:value-of select="./@name"/>
</xsl:when>
<xsl:otherwise>
<xsl:text>true</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:apply-templates />
</span>
</xsl:template>
這僅僅是一個名稱相同的字段轉換爲<span field="name">
,如果字段有一個,否則「真」。
問題我現在是,*[var]
匹配一個字段,如果該字段有一個<var>
作爲孩子。但我想要發生的是*[var]
匹配第一個,然後field
以匹配以及,但之後。
目前,隨着
<field name="field1">
<var attr="class" ok="small,smaller" />
Text
</field>
輸入我得到
<field name="field1" editable="class" constraints="class:small,smaller">
Text
</field>
但我想
<span field="field1" editable="class" constraints="class:small,smaller">
Text
</span>
我發現SO一些答案這樣做兩遍,但我不知道這是正確的答案,也不知道如何實現我找到的答案。我應該如何處理這個問題,如果有簡單的答案,它是什麼? :)
TIA Altreus
像這樣移動var元素是我考慮的事情;但我不確定我將如何執行第二個連接 - 帶分號的連接。我害怕我沒有完全理解原作 - 它只是工作 - 而且我還沒有花時間坐下來解決它:o!也許在這個項目迭代結束時...... – Altreus 2011-01-27 16:24:29