2

特定の順序で (属性を持つノードの) 属性を処理する必要があります。例えば:

<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 つだけでなく、数十の属性があります。

4

1 に答える 1

1

たとえば要素とは対照的に、XML では属性の順序は重要ではありません。つまり、XPath と XSLT は属性を任意の順序で処理できます。したがって、特定の順序を強制する唯一の方法は、何らかの形で指定することです。1 つの方法は、最後のコード例のように明示的に呼び出すことです。すべての属性名を抽出して別の XML ファイルに保存することもできます。

<attributes>
  <attribute>era</attribute>
  <attribute>year</attribute>
  <attribute>month</attribute>
  ...
<attributes>

これらの要素を document() 関数でロードし、すべての属性要素を反復処理できます。

<xsl:variable name="attributes" select="document('attributes.xml')//attribute"/>
...
<xsl:template match="*">
  <xsl:variable name="self" select="."/>
  <xsl:for-each select="$attributes">
    <xsl:apply-templates select="$self/@*[name()=current()]"/>
  </xsl:for-each>    
</xsl:template>
于 2012-04-21T09:38:42.477 に答える