特定の順序で (属性を持つノードの) 属性を処理する必要があります。例えば:
<test>
<event
era ="modern"
year ="1996"
quarter = "first"
day = "13"
month= "January"
bcad ="ad"
hour ="18"
minute = "23"
>The big game began.</event>
<happening
era ="modern"
day = "18"
bcad ="ad"
month= "February"
hour ="19"
minute = "24"
>The big game ended.</happening>
<other>Before time existed.</other>
</test>
これ
<xsl:template match="test//*">
<div>
<xsl:apply-templates select="@*" />
<xsl:apply-templates />
</div>
</xsl:template>
<xsl:template match="@*">
<span class="{name()}">
<xsl:value-of select="."/>
</span>
</xsl:template>
必要に応じてフォーマットします。つまり、私は得るだろう
<div><span class="era">modern</span>
<span class="year">1996</span>
<span class="quarter">first</span>
<span class="day">13</span>
<span class="month">January</span>
<span class="bcad">ad</span>
<span class="hour">18</span>
<span class="minute">23</span>The big game began.</div>
<div><span class="era">modern</span>
<span class="day">18</span>
<span class="bcad">ad</span>
<span class="month">February</span>
<span class="hour">19</span>
<span class="minute">24</span>The big game ended.</div>
<div>Before time existed.</div>
(ただし、読みやすくするためにここに追加した改行はありません)。
しかし、属性の順序は (必ずしも) 正しくありません。
これを修正するには、次のように、必要な順序でテンプレートを適用するテンプレートに変更<xsl:apply-templates select="@*" />
して追加します。<xsl:call-template name="atts" />
<xsl:template match="test//*">
<div>
<xsl:call-template name="atts" />
<xsl:apply-templates />
</div>
</xsl:template>
<xsl:template name="atts">
<xsl:apply-templates select="@era" />
<xsl:apply-templates select="@bcad" />
<xsl:apply-templates select="@year" />
<xsl:apply-templates select="@quarter" />
<xsl:apply-templates select="@month" />
<xsl:apply-templates select="@day" />
<xsl:apply-templates select="@hour" />
<xsl:apply-templates select="@minute" />
</xsl:template>
<xsl:template match="@*">
<span class="{name()}">
<xsl:value-of select="."/>
</span>
</xsl:template>
これは、指定された順序で属性を処理するベスト プラクティスの方法ですか? キーまたはグローバル変数を使用する方法があるかどうか疑問に思っています。
XSLT 1.0 を使用する必要がありますが、実際には、8 つだけでなく、数十の属性があります。