我不需要使用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>
我用于json输出的XSL:
<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"
}
}
]
请帮我解决这个问题。提前谢谢
答案 0 :(得分:0)
从
更改项目模板<xsl:template match="item">
{
<xsl:apply-templates/>
},
</xsl:template>
到
<xsl:template match="item">
<xsl:if test="position() != 1">,</xsl:if>
{
<xsl:apply-templates/>
}
</xsl:template>
即,在除第一个项目之外的每个项目之前输出逗号。