Writing JSON with XSLT

匿名 (未验证) 提交于 2019-12-03 01:18:02

问题:

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__ 

回答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.

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!