使用xslt在Json输出中出现不需要的xml版本

使用xslt在Json输出中出现不需要的xml版本

本文介绍了使用xslt在Json输出中出现不需要的xml版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是XSL Stylesheet version = 3.0和Saxon-PE 9.6.0.7,我收到了 xml version ="1.0" encoding ="UTF-8" 标记JSON输出:

I'm using XSL Stylesheet version=3.0 and Saxon-PE 9.6.0.7, I'm getting the xml version="1.0" encoding="UTF-8" tags is coming along in JSON Output:

我的输入xml文件是:

My Input xml file is:

<?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输出为:

I'm getting the Json output as:

"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版本.请给我建议.预先感谢

Why the xml version coming without I'm giving in the input. Please give me suggestion on this. Thanks in advance

推荐答案

我已为您指出了序列化函数的Saxon 9.6文档,该文档显示了第二个参数,因此您只需要分别使用该文档中概述的参数即可.规范( https://www.w3.org/TR/xpath-functions-30/#func-serialize https://www.w3.org/TR/xslt-xquery-serialization-30/#serparams-in-xdm-instance )中的文档链接为:

I pointed you to the Saxon 9.6 documentation of the serialize function earlier that shows it takes a second argument so you simply have to use that as outlined in the docucmentation respectively the specs (https://www.w3.org/TR/xpath-functions-30/#func-serialize, https://www.w3.org/TR/xslt-xquery-serialization-30/#serparams-in-xdm-instance) the documentation links to:

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

这篇关于使用xslt在Json输出中出现不需要的xml版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 08:47