需要使用XSLT删除特定事件的符号

时间:2016-12-15 11:16:01

标签: json xml xslt

我需要使用xsl删除特定事件的+符号。

我的输入xml文件是:

<term>
<image>Mediabakery_SPY0006138.jpg</image>
<name>Pramlintide</name>
<description>
<p>This medicine</p>
<ol>
<li>Use this medicine</li>
</ol>
<p>Write your thoughts here. . .</p>
</description>
<commentBox>false</commentBox>
</term>

我使用的XSL:

<xsl:stylesheet version="2.0">

<xsl:template match="term">
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="description">
        "description": 
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="p">
        "<xsl:apply-templates/>" +
    </xsl:template>

    <xsl:template match="ol">
        "<xsl:copy-of select="." />" +
    </xsl:template>

<xsl:template match="commentBox">
        "commentBox": "<xsl:apply-templates/>"
    </xsl:template>
</xsl:stylesheet>

json输出我得到了:

"description": 

"This medicine" +

"<ol><li>Use this medicine</li></ol>" +

"Write your thoughts here. . ." +
"commentBox": "false"

但我不希望最后的+符号如下所示:

"description": 

"This medicine" +

"<ol><li>Use this medicine</li></ol>" +

"Write your thoughts here. . ." 
"commentBox": "false"

请指导我。提前致谢

1 个答案:

答案 0 :(得分:0)

试试这个:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="description">
    "description": 
    <xsl:apply-templates/>
</xsl:template>
<xsl:template match="p">
 "<xsl:apply-templates/>" <xsl:if test="following::text()[
     normalize-space()]
     [generate-id(ancestor::description)=generate-id(current()/ancestor::description)]">+</xsl:if>
</xsl:template>

<xsl:template match="ol">
    "<xsl:copy-of select="." />" <xsl:if test="following::text()[normalize-space()]">+</xsl:if>
</xsl:template>
</xsl:stylesheet>

输出:

    "description": 


    "This medicine" +

    "<ol><li>Use this medicine</li></ol>" +

    "Write your thoughts here. . ."