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

7

I have XML like this:

<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>标记位于父节点中,有许多这样的标记。是否有最佳实践?

是的,可以使用XSLT非常简洁和优雅地完成这个任务。 - Dimitre Novatchev
3个回答

5
这似乎是XSLT的完美应用场景 - XML数据干净且格式良好。如果您担心浏览器兼容性问题,为什么不在服务器端进行转换呢?以下是可用于转换数据的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> 

我没有在客户端完成过这项任务,所以很遗憾我无法保证浏览器兼容性。


服务器端不是一个选项。那么转换的 XSLT 会是什么样子呢?XSLT 语法是否相同,可在顶级浏览器 (IE7+、Chrome、Firefox、Safari) 中使用相同的代码? - artlung
@SimonMcKenzie - 别忘了 class 属性。 - Daniel Haley
@DevNull - 我花了一些时间才明白你的意思 - 我没有注意到它!感谢你的指出。 - Simon MᶜKenzie
对于未来想在客户端执行此操作的任何人,似乎可以参考以下链接:http://www.w3schools.com/xsl/xsl_client.asp - Patrick M

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 (属性值模板)。

在这种情况下,对我来说,这似乎是一种更可行的解决方案,特别是因为它的简洁语法非常像现代Web框架中的模板。 - Lars Gyrup Brink Nielsen
1
@LayZee,是的,我完全同意:)。不客气。 - Dimitre Novatchev

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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接