需要添加元素标签以及' +' Json输出中的符号

时间:2016-12-26 11:21:20

标签: javascript json xml xslt

我想在json输出中添加元素标签以及Plus符号,在单词空格的末尾,我使用了一些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="' +&#10; '"/>

<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('&quot;', regex-group(2), '&quot;')"/>
<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>",

请帮我解决这个问题。我希望输出带有元素标签以及加号。提前致谢

1 个答案:

答案 0 :(得分:0)

假设您可以使用XPath 3.0 / 3.1序列化函数(https://www.w3.org/TR/xpath-functions-31/#func-serialize),例如使用Saxon 9.7 HE(当然是PE或EE)并在样式表中设置version="3.0",那么您可以序列化您拥有的节点,然后应用该函数将字符串分解为块。我还建议切换到输出方法text,因为使用纯文本混合XML或HTML与输出方法xml不兼容。所以试试

<xsl:output method="text"/>

<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="' +&#10; '"/>

<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('&quot;', regex-group(2), '&quot;')"/>
                <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(serialize(node())))"/>,
</xsl:template>

,输出就像

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

您可以根据需要更改该参数length,以调整换行符的位置。