使用xslt进入Json输出的不需要的xml版本

时间:2016-12-29 05:13:33

标签: json xml xslt saxon

我使用XSL样式表版本= 3.0和Saxon-PE 9.6.0.7,我得到 xml版本=" 1.0" encoding =" UTF-8" 标签出现在JSON输出中:

我的输入xml文件是:

<?xml version="1.0" encoding="UTF-8"?>
<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>
</description>

我用作的XSL:

<xsl:stylesheet version="3.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="#all">

<xsl:output omit-xml-declaration="yes" 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('', 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(serialize(node())))"/>",
    </xsl:template>

</xsl:stylesheet>

我将Json输出为:

"description": "<?xml version="1.0" encoding="UTF-8"?><p>Here are some things other smokers say +
 that they like about smoking:</p> <?xml version="1.0" encoding="UTF-8"?> +<ul><li>Smoking is a reward I can give myself when I finish a +
 task.</li></ul>

但我需要输出为:

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

为什么xml版本没有我在输入中给出。请给我这个建议。提前致谢

1 个答案:

答案 0 :(得分:2)

我向前面提到了序列化函数的Saxon 9.6文档,它显示它需要第二个参数,因此您只需按照文档中的概述(https://www.w3.org/TR/xpath-functions-30/#func-serializehttps://www.w3.org/TR/xslt-xquery-serialization-30/#serparams-in-xdm-instance使用它)文档链接到:

<xsl:stylesheet version="3.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="#all">

    <xsl:output method="text"/>

    <xsl:param name="ser-params" as="element()">
        <output:serialization-parameters
            xmlns:output="http://www.w3.org/2010/xslt-xquery-serialization">
            <output:method value="xml"/>
            <output:version value="1.0"/>
            <output:indent value="yes"/>
            <output:omit-xml-declaration value="yes"/>
        </output:serialization-parameters>
    </xsl:param>

    <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('', 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(string-join(node()/serialize(., $ser-params), '')))"/>",
    </xsl:template>

</xsl:stylesheet>