【发布时间】:2010-05-14 21:41:59
【问题描述】:
我正在尝试编写 XSLT 来将特定网页转换为 JSON。下面的代码演示了 Ruby 是如何进行这种转换的,但是 XSLT 没有生成有效的 JSON(数组中的逗号太多了)——有人知道如何编写 XSLT 来生成有效的 JSON 吗?
require 'rubygems'
require 'nokogiri'
require 'open-uri'
doc = Nokogiri::HTML(open('http://bbc.co.uk/radio1/playlist'))
xslt = Nokogiri::XSLT(DATA.read)
puts out = xslt.transform(doc)
# Now follows the XSLT
__END__
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
<xsl:output method="text" encoding="UTF-8" media-type="text/plain"/>
<xsl:template match="/">
[
<xsl:for-each select="//*[@id='playlist_a']//div[@class='artists_and_songs']//ul[@class='clearme']">
{'artist':'<xsl:value-of select="li[@class='artist']" />','track':'<xsl:value-of select="li[@class='song']" />'},
</xsl:for-each>
]
</xsl:template>
</xsl:stylesheet>
【问题讨论】: