【问题标题】:unwanted xml versions coming in Json output using xslt使用 xslt 在 Json 输出中出现不需要的 xml 版本
【发布时间】:2016-12-29 05:13:32
【问题描述】:

我正在使用 XSL 样式表 version=3.0 和 Saxon-PE 9.6.0.7,我收到了 xml version="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 版本。请给我这方面的建议。提前致谢

【问题讨论】:

    标签: json xml xslt saxon


    【解决方案1】:

    我之前向您指出了序列化函数的 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-04
      • 1970-01-01
      • 1970-01-01
      • 2021-03-24
      相关资源
      最近更新 更多