问题
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:
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 which i used as:
<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="' + '"/>
<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>
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>
But i need the output as:
"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>
Why the xml version coming without I'm giving in the input. Please give me suggestion on this. Thanks in advance
回答1:
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="' + '"/>
<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>
来源:https://stackoverflow.com/questions/41373473/unwanted-xml-versions-coming-in-json-output-using-xslt