7

次のような XML があります。

<Artist name="Syd Mead" id="3412" ntrack="28" pop="8"/> 

HTMLマークアップで使用する必要があるもの:

<a href="#" data-name="Syd Mead" data-id="3412"
  data-ntrack="28" data-pop="8"
  class="pop-8"><span>Syd Mead</span></a>

最も幅広いブラウザでこれを行う「正しい」方法は何ですか? これは、XSLT 変換で確実に実行できますか? 正規表現を使用する方が良いですか (ありそうもない)、それとも xml を解析して、<Artist>タグごとに各属性を読み取り、document.createElement と setAttribute を手動で実行する必要がありますか?

タグは<Artist>親ノードにあり、多数あります。これに対するベストプラクティスはありますか?

4

3 に答える 3

5

以下は、単純な (条件なし、追加のテンプレートなし、 no xsl:attribute、 no xsl:for-each)、短くて完全な変換です。

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="no"/>

 <xsl:template match="Artist">
     <a href="#" data-name="{@name}"
                 data-id="{@id}"
                 data-ntrack="{@ntrack}"
                 data-pop="{@pop}"
                 class="pop-{@pop}">
    <span><xsl:value-of select="@name"/></span>
  </a>
 </xsl:template>
</xsl:stylesheet>

この変換が提供された XML ドキュメントに適用されると、次のようになります。

<Artist name="Syd Mead" id="3412" ntrack="28" pop="8"/>

必要な正しい結果が生成されます。

<a href="#" data-name="Syd Mead" data-id="3412" data-ntrack="28" data-pop="8" class="pop-8"><span>Syd Mead</span></a>

解説: AVT (属性値テンプレート)の適切な使用

于 2012-06-07T12:36:04.243 に答える
5

これは、XSLT の最適な候補のように見えます。XML はクリーンで整形式です。ブラウザーの互換性が気になる場合は、サーバー側で変換を行ってみませんか?

この XSLT はデータを変換します -ここでテストできます:

ソースデータ:

<Artists>
    <Artist name="Syd Mead" id="3412" ntrack="28" pop="8"/>
</Artists> 

XSLT:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
    <xsl:for-each select="Artists/Artist">
        <a href="#">
            <xsl:attribute name="data-id">      
                <xsl:value-of select="@id"/>
            </xsl:attribute>
            <xsl:attribute name="data-ntrack">      
                <xsl:value-of select="@ntrack"/>
            </xsl:attribute>
            <xsl:attribute name="data-pop">      
                <xsl:value-of select="@pop"/>
            </xsl:attribute>
            <xsl:attribute name="data-name">      
                <xsl:value-of select="@name"/>
            </xsl:attribute>
           <xsl:attribute name="class">    
                <xsl:value-of select="concat('pop-',@pop)"/>
           </xsl:attribute>

            <span><xsl:value-of select="@name"/></span>
        </a>
    </xsl:for-each>
</xsl:template>
</xsl:stylesheet> 

私はクライアント側でこれを行っていないので、残念ながらブラウザの互換性について話すことはできません.

于 2012-06-07T05:28:09.063 に答える
2

私の意見では、より単純な別の XSLT スタイルシート オプションを次に示します。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html"/>

    <xsl:template match="/*/Artist">
        <a href="#" class="pop-{@pop}"> 
            <xsl:apply-templates select="@*"/>
            <span><xsl:value-of select="@name"/></span>
        </a>        
    </xsl:template>

    <xsl:template match="@*">
        <xsl:attribute name="data-{name()}">
            <xsl:value-of select="."/>
        </xsl:attribute>
    </xsl:template>

</xsl:stylesheet>

XML 入力

<Artists>
    <Artist name="Syd Mead" id="3412" ntrack="28" pop="8"/> 
</Artists>

HTML出力

<a class="pop-8" href="#" data-name="Syd Mead" data-id="3412" data-ntrack="28" data-pop="8">
  <span>Syd Mead</span>
</a>
于 2012-06-07T06:51:11.160 に答える