Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用XSLT编写JSON_Json_Xslt - Fatal编程技术网

用XSLT编写JSON

用XSLT编写JSON,json,xslt,Json,Xslt,我正在尝试编写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.re

我正在尝试编写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>
需要“rubygems”
需要“nokogiri”
需要“打开uri”
doc=Nokogiri::HTML(打开)http://bbc.co.uk/radio1/playlist'))
xslt=Nokogiri::xslt(DATA.read)
put out=xslt.transform(doc)
#接下来是XSLT
__结束__
[
{'artist':'','track':''},
]

省略
中每一行的逗号,并添加:

<xsl:if test="position() != last()">,</xsl:if>

这将在除最后一项之外的每个项目中添加逗号。

将XSLT拆分为单独的模板可以提高可读性

<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:text>[</xsl:text>
    <xsl:apply-templates select="//div[@id='playlist_a']//ul[@class='clearme']" />
    <xsl:text>]</xsl:text>
  </xsl:template>

  <xsl:template match="ul">
    <xsl:text>{'artist':'</xsl:text><xsl:value-of select="li[@class='artist']" />
    <xsl:text>','track':'</xsl:text><xsl:value-of select="li[@class='song']" />
    <xsl:text>'}</xsl:text>
    <xsl:if test="position() &lt; last()">,</xsl:if>
  </xsl:template>
</xsl:stylesheet>

[
]
{‘艺术家’:'
'轨道':'
'}
,

此外,如果artist和song的值包含单引号,则它们可能会破坏JSON,因此可能需要替换单引号。

为什么不改用Sitecore项目Web API?它在SDN上可用,并作为一个简单的插件安装。安装后,您可以使用REST将项目作为JSON返回。可以搜索项目,并且可以通过JSON为各个可用字段设置安全性。此外,您还可以使用REST和JSON实际创建、删除和更新Sitecore项目。

IMHO,position()是XSLT唯一的flash of genius功能。当然,在您提出问题时,项目Web API不可用:-)此评论与帖子()相关吗?如果是的话,是什么让你认为这是一个Sitecore问题?如果不是,那么肯定是某个地方有一个bug,它正在变得混乱。