可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'm trying to write XSLT to transform a specific web page to JSON. The following code demonstrates how Ruby would do this conversion, but the XSLT doesn't generate valid JSON (there's one too many commas inside the array) - anyone know how to write XSLT to generate valid 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__ [ {'artist':'','track':''}, ]
回答1:
Omit the comma from the line inside the for-each and add:
,
This will add a comma to each item except the last one.
回答2:
Splitting up your XSLT into separate templates can increase readability.
[]{'artist':'','track':''},
Also, the values of artist and song can break your JSON if they contain single quotes, replacing single quotes could be necessary.
回答3:
Why not use the Sitecore Item Web API instead? It is available on SDN and is installed as a simple plugin. When it is installed you can use REST to get items back as JSON. It is possible to search for items and you can set security for the individual fields available through JSON. Further on you can actually create, delete and update Sitecore items using REST and JSON.