将XML转换为HTML:最佳实践?

时间:2012-06-07 05:07:17

标签: javascript xml xslt

我有这样的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>

什么是&#34;权利&#34;为最广泛的浏览器做这个的方法?这可以通过XSLT转换可靠地完成吗?是否更好地使用正则表达式(不太可能)或者我必须解析xml,并且对于每个<Artist>标记读取每个属性并手动执行document.createElement和setAttribute?

<Artist>标记位于父节点中,其中有许多标记。这是最好的做法吗?

3 个答案:

答案 0 :(得分:5)

这看起来像XSLT的完美候选者 - XML是干净的&amp;形成良好。如果您担心浏览器兼容性,为什么不在服务器端进行转换?

此XSLT将转换您的数据 - 您可以对其进行测试here

来源数据:

<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> 

我没有在客户端做过这个,所以不幸的是我无法说出浏览器兼容性。

答案 1 :(得分:5)

这是一个简单的(没有条件,没有其他模板,没有xsl:attribute,没有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 (属性值模板)

答案 2 :(得分: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>