【问题标题】:Need to remove symbol for particular occurrence using XSLT需要使用 XSLT 删除特定出现的符号
【发布时间】:2016-12-15 11:16:00
【问题描述】:

我需要使用 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"

请指导我。提前致谢

【问题讨论】:

  • 能否请您重新表述您的问题
  • 我想如何重新调整@scanQR
  • 如果你想要 JSON 输出,你不能使用“+”号来连接字符串。这可能是有效的 Javascript,但不是有效的 JSON。

标签: json xml xslt


【解决方案1】:

试试这个:

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

【讨论】:

  • 这不起作用@rudramuni,+ 符号出现在所有情况下
  • 不需要最后一个“+”符号或不需要所有符号?
  • 最后一个+符号只有@rudramuni,但我用这个。但一切都发生了
  • 我更新了我的代码,看看有帮助吗?如果后面没有任何文本,这将抑制最后一个“+”。
  • 对不起@Rudramuni,它不起作用,在最后一个 para 标记之后,另一个文本元素即将到来。是因为这个造成的影响吗???
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多