2016-12-14 57 views
0

停止逗號象徵一個次數使用XSLT需要使用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() &gt; 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

更改的項目模板從

<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> 

也就是說,輸出一個逗號除了第一個每一個項目之前。