【问题标题】:Need to stop comma symbol for one occurance by using xslt需要使用 xslt 停止一次出现的逗号符号
【发布时间】: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() &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"

}

}
]

请帮助我。提前谢谢你

【问题讨论】:

    标签: json xml xslt


    【解决方案1】:

    更改项目模板
    <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>
    

    即在除第一个之外的每个项目之前输出一个逗号。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-27
      • 2022-06-11
      • 1970-01-01
      • 1970-01-01
      • 2013-11-01
      • 2011-10-07
      • 2011-01-01
      • 2015-05-04
      相关资源
      最近更新 更多