【发布时间】:2016-12-14 12:01:40
【问题描述】:
我不需要在使用 xslt 的项目列表元素的最后一次出现中使用逗号符号
我的输入xml文件是:
<items>
<item>
<statement>I’m afraid</statement>
<response>
<p>Some complications</p>
</response>
<media>
<type>html</type>
<link>Brightcove.com%2FZ678766-1.avi</link>
</media>
</item>
<item>
<statement>I don’t know</statement>
<response>
<p>Some complications</p>
</response>
<media>
<type>html</type>
<link>Brightcove.com%2FZ678766-2.avi</link>
</media>
</item>
<item>
<statement>I don’t know</statement>
<response>
<p>Some complications</p>
</response>
<media>
<type>html</type>
<link>Brightcove.com%2FZ678766-3.avi</link>
</media>
</item>
</items>
我使用 XSL 作为 json 输出:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:json="http://json.org/" xmlns:mf="http://example.com/mf"
exclude-result-prefixes="xs mf">
<xsl:template match="items">
<xsl:apply-templates select="item[1]/media" />,
items: [
<xsl:apply-templates select="item[position() > 1]"/>
]
</xsl:template>
<xsl:template match="item">
{
<xsl:apply-templates/>
},
</xsl:template>
<xsl:template match="statement">
"statement": "<xsl:apply-templates/>",
</xsl:template>
<xsl:template match="media">
media: {
<xsl:apply-templates/>
}
</xsl:template>
<xsl:template match="type">
"type": "<xsl:apply-templates/>"
</xsl:template>
<xsl:template match="link">
<xsl:text>"link": "files/</xsl:text>
<xsl:value-of select="tokenize(., '/')[last()]"/>"
</xsl:template>
<xsl:template match="p">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
我得到的 json 输出是:
items: [
{
"statement": "I’m afraid",
"response": "Some complications",
media: {
"type": "html"
"link": "files/Brightcove.com%2FZ678766-1.avi"
}
},
{
"statement": "I don’t know",
"response": "Some complications",
media: {
"type": "html"
"link": "files/Brightcove.com%2FZ678766-2.avi"
}
},
{
"statement": "I don’t know",
"response": "Some complications",
media: {
"type": "html"
"link": "files/Brightcove.com%2FZ678766-3.avi"
}
},
]
但我想删除最后一项 (},) 符号处的逗号,如下所示:
items: [
{
"statement": "I’m afraid",
"response": "Some complications",
media: {
"type": "html"
"link": "files/Brightcove.com%2FZ678766-1.avi"
}
},
{
"statement": "I don’t know",
"response": "Some complications",
media: {
"type": "html"
"link": "files/Brightcove.com%2FZ678766-2.avi"
}
},
{
"statement": "I don’t know",
"response": "Some complications",
media: {
"type": "html"
"link": "files/Brightcove.com%2FZ678766-3.avi"
}
}
]
请帮助我。提前谢谢你
【问题讨论】: