我在編寫XSLT來將特定的網頁轉換爲JSON。下面的代碼演示了Ruby如何進行這種轉換,但XSLT不會生成有效的JSON(數組中有太多逗號) - 任何人都知道如何編寫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>
恕我直言,position()是XSLT唯一的閃現天才功能。 – 2010-05-14 21:47:59