2016-12-15 51 views
1

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

我输入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" 

请指导我。在此先感谢

+0

能否请您重新表述您的问题 – ScanQR

+0

我多么想重聚@scanQR – User501

+0

如果你想JSON输出,你不能使用“+”号连接字符串。这可能是有效的JavaScript,但它不会是有效的JSON。 –

回答

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. . ." 
+0

这不起作用@rudramuni,所有发生的符号+ – User501

+0

最后的'+'符号不是必需的或所有符号不是必需的? –

+0

last + symbol only @rudramuni,但我用这个。但一切都发生 – User501