【发布时间】:2016-12-26 11:21:19
【问题描述】:
我想在 json 输出中添加元素标记以及加号,在单词空间的末尾,我使用了一些 XSLT,因为我得到了带加号的 json 输出,但元素标记不是来了。
我的输入 xml 是:
<description>
<p>Here are some things other smokers say that they like about smoking:</p>
<ul>
<li>Smoking is a reward I can give myself when I finish a task.</li>
<p>Write your thoughts here. . .</p>
</description>
我使用的 XSLT:
<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:output indent="yes" omit-xml-declaration="yes" method="xml" encoding="utf-8"/>
<xsl:param name="length" as="xs:integer" select="80"/>
<xsl:param name="pattern" as="xs:string" select="concat('((.{1,', $length, '})( |$))')"/>
<xsl:param name="sep" as="xs:string" select="' + '"/>
<xsl:function name="mf:break" as="xs:string">
<xsl:param name="input" as="xs:string"/>
<xsl:variable name="result">
<xsl:analyze-string select="$input" regex="{$pattern}">
<xsl:matching-substring>
<xsl:value-of select="concat('"', regex-group(2), '"')"/>
<xsl:if test="position() ne last()">
<xsl:value-of select="$sep"/>
</xsl:if>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:variable>
<xsl:sequence select="$result"/>
</xsl:function>
<xsl:template match="description">
"description": <xsl:sequence select="mf:break(normalize-space())"/>,
</xsl:template>
<xsl:template match="p">
<p><xsl:apply-templates/></p>
</xsl:template>
<xsl:template match="ul">
<ul><xsl:apply-templates/></ul>
</xsl:template>
<xsl:template match="li">
<li><xsl:apply-templates/></li>
</xsl:template>
</xsl:stylesheet>
我的 Json 输出为:
"description": "Here are some things other smokers say that they like about smoking:Smoking is a" +
"reward I can give myself when I finish a task.Write your" +
"thoughts here. . .",
但我的预期输出为:
"description": "<p>Here are some things other smokers say that they like about smoking:</p><ul><li>Smoking is a" +
"reward I can give myself when I finish a task.</li></ul><p>Write your" +
"thoughts here. . .</p>",
请帮助我。我想要带有元素标签和加号的输出。提前致谢
【问题讨论】:
-
您使用哪种 XSLT 处理器?如果您有 Saxon 9 PE 或 EE(例如,在 oXygen 内部),那么您可以先序列化节点,然后调用我在之前关于节点序列化的响应中发布的函数。如果你有 Saxon 9.7 HE,那么你可以简单地设置
version="3.0",你甚至可以在开源版本中序列化"description": <xsl:sequence select="mf:break(normalize-space(serialize(node()))"/>。 -
目前我在氧气中使用 Saxon 9 PE 处理器,我尝试使用 3.0 版的 SAXON 9.7 HE,它的输出没有改变@Martin。
标签: javascript json xml xslt