Ich versuche, XSLT zu schreiben, um eine bestimmte Webseite in JSON umzuwandeln. Der folgende Code demonstriert, wie Ruby diese Konvertierung durchführen würde, aber das XSLT erzeugt kein gültiges JSON (es gibt ein zu viel Komma innerhalb des Arrays) - weiß jemand, wie man XSLT schreibt, um gültiges JSON zu generieren?JSON mit XSLT schreiben
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>
IMHO, Position() ist XSLTs einzige geniale Funktion. –