View Old View New View Both View Only Previous Next

This draft contains only sections that have differences from the version that it modified.

W3C

XSLT and XQuery Serialization 4.0

W3C Editor's Draft 2 February 2026

This version:
https://www.w3.org/TR/2000/REC-xslt-xquery-serialization-40-20000101/
Latest version of XSLT and XQuery Serialization 4.0:
https://www.w3.org/TR/xslt-xquery-serialization-40/
Most recent version of XSLT and XQuery Serialization 4:
https://www.w3.org/TR/xslt-xquery-serialization-4/
Most recent version of XSLT and XQuery Serialization:
https://www.w3.org/TR/xslt-xquery-serialization/
Most recent Recommendation of XSLT and XQuery Serialization:
https://www.w3.org/TR/xslt-xquery-serialization-31/
Editor:
Michael Kay, Saxonica <http://www.saxonica.com/>

This document is also available in these non-normative formats: XML.


Abstract

This document defines methods of serializing an instance of the data model defined in [XDM 4.0] into a sequence of octets, conforming to a variety of formats including XML, HTML, and JSON. Serialization is designed to be a component that can be used either on its own, or invoked from languages such as [XSLT 4.0], [XPath 4.0] or [XQuery 4.0].

Status of this Document

This section describes the status of this document at the time of its publication. Other documents may supersede this document.

This document is a working draft developed and maintained by a W3C Community Group, the XQuery and XSLT Extensions Community Group unofficially known as QT4CG (where "QT" denotes Query and Transformation). This draft is work in progress and should not be considered either stable or complete. Standard W3C copyright and patent conditions apply.

The community group welcomes comments on the specification. Comments are best submitted as issues on the group's GitHub repository.

The community group maintains two extensive test suites, one oriented to XQuery and XPath, the other to XSLT. These can be found at qt4tests and xslt40-test respectively. New tests, or suggestions for correcting existing tests, are welcome. The test suites include extensive metadata describing the conditions for applicability of each test case as well as the expected results. They do not include any test drivers for executing the tests: each implementation is expected to provide its own test driver.

Dedication

The publications of this community group are dedicated to our co-chair, Michael Sperberg-McQueen (1954–2024).


9 JSON Output Method

Changes in 4.0 (next | previous)

  1. Added the escape-solidus parameter for JSON serialization.   [Issue 530 PR 534 6 June 2023]

  2. Added the json-lines parameter for JSON serialization.   [Issue 1471  15 October 2024]

  3. The serialization of maps retains the order of entries.   [Issue 1651 PR 1703 14 January 2025]

  4. A JNode is replaced by its ·content· property.   [Issue 2025 PR 2031 29 May 2025]

  5. JSON canonicalization is supported by the ·canonical· property.   [Issue 938 PR TODO 20 October 2025]

  6. The JSON output method now produces fallback representation of NaN and infinity, rather than reporting an error for such values.   [Issue 641 PR 2387 16 January 2026]

The JSON output method serializes the input tree using the JSON syntax defined in [RFC 7159], or (if the json-lines parameter is set to true) the json-lines syntax defined at [JSON Lines]. Sequence normalization is not performed for this output method. The effect of the json-lines parameter is explained at 9.2 JSON Lines.

If json-lines is set to false, then:

9.1 Serializing Items

An individual item is serialized as follows:

  • A JNode is serialized by serializing its ·content· property.

  • An array item in the input tree is serialized to a JSON array by outputting the serialized JSON value of each member within the array separated by delimiters according to the JSON array syntax, i.e. [member, member, ...]. Each member in the array is to be serialized by recursively applying the rules in this section.

  • A map item in the input tree is serialized to a JSON object by outputting, for each key/value pair, the string value of the key to a JSON string, followed by the serialized JSON value of the entry, separated by delimiters according to the JSON object syntax, i.e. {key:value, key:value, ...}. The key/value pairs in the serialized output retain the entry orderDM of entries in the map, unless canonical is true, in which case map entries must be sorted according to the rules of [RFC8785].

    Note:

    These rules require sorting according to the UTF-16 representation of the string, which is not the same (in the presence of surrogate pairs) as sorting using the XPath codepoint collation.

    If any two keys of the map item have the same string value, serialization error [err:SERE0022] is raised, unless the allow-duplicate-names parameter is true and the canonical parameter is false.

  • A node in the input tree is serialized to a JSON string by outputting the result of serializing the node using the method specified by the json-node-output-method parameter. If the value of the canonical parameter is false, the node is serialized with the serialization parameter omit-xml-declaration set to true and with no other serialization parameters set, otherwise (canonical is true) all parameters are inherited by the sub-serialization process.

  • An atomic valueXP of type xs:numeric is serialized to a JSON number.

    If the canonical parameter is true, then the value is cast to type xs:double and the result is serialized according to the rules of [RFC8785] (and by extension [RFC7493]). A serialization error occurs for the values NaN, INF, and -INF which cannot be represented in canonical JSON.

    If the canonical parameter is false then:

    • A value of type xs:decimal (including xs:integer) is output in the format that results from casting the value to xs:string.

    • A value of type xs:float is first cast to type xs:double, and then output in the same way as an xs:double.

    • Implementations may serialize an xs:double value using any lexical representation of a JSON number defined in [RFC 7159], but it is recommended to use the same representation as when the canonical parameter is true; except that negative zero should be output with a leading minus sign.

    • The value NaN is serialized as the JSON token null.

    • The values positive and negative infinity are serialized as 1e9999 and -1e9999 respectively.

      Note:

      These values are permitted by the JSON grammar, but some JSON parsers may reject them. For interoperability, they should be avoided.

  • An atomic itemXP of type xs:boolean is serialized to the JSON token true or false.

  • An atomic itemXP of type xs:QName in the input tree whose namespace part is "http://www.w3.org/2005/xpath-functions" and whose local part is "null" is serialized to the JSON token null.

    Note:

    This rule is introduced in 4.0, along with an option in the fn:parse-json function to allow a user-defined representation of the JSON value null. While the default representation of null as an empty sequence is usable in many circumstances, an explicit representation of null as a recognizable item can make some operations on JSON-derived values easier.

  • Any other atomic valueXP in the input tree is serialized to a JSON string by outputting the result of applying the fn:string function to the item.

  • An empty sequence in the input tree is serialized to the JSON token null.

  • A sequence of length greater than one in the input tree will result in a serialization error [err:SERE0023].

  • Any item in the input tree of a type not specified in the above list will result in a serialization error [err:SERE0021].

[Definition: Whenever a value is serialized to a JSON string, the following procedure is applied to the supplied string:]

  1. If the canonical parameter is true, then the string is output as specified in [RFC8785]. The string is first normalized if requested using the normalization-form parameter. The serialization parameters escape-solidus and use-character-maps are ignored.

    Note:

    The escaping rules are restated here for convenience:

    • If the Unicode value falls within the traditional ASCII control character range (U+0000 (NULL) through U+001F (IS1) ), it must be serialized using lowercase hexadecimal Unicode notation (\uhhhh) unless it is in the set of predefined JSON control characters U+0008 (BACKSPACE) , U+0009 (TAB) , U+000A (NEWLINE) , U+000C (FORM FEED) , or U+000D (CARRIAGE RETURN) , which must be serialized as \b, \t, \n, \f, and \r, respectively.

    • If the Unicode value is outside of the ASCII control character range, it must be serialized “as is” unless it is equivalent to U+005C (REVERSE SOLIDUS, BACKSLASH, \) or U+0022 (QUOTATION MARK, ") , which must be serialized as \\ and \", respectively.

    Note that the C1 control characters (codepoints 127-159) are not included in this list.

    Note:

    In canonical JSON, property values within an object are sorted based on their UTF16 representation. This corresponds to the default sort order in languages such as Javascript, Java, and C#, but in the presence of codepoints above 65535, it is not the same as the order produced by Unicode codepoint collation.

  2. Otherwise (when canonical is false):

    1. Any character in the string for which character mapping is defined (see 11 Character Maps) is substituted by the replacement string defined in the character map.

    2. Any other character in the input string (but not a character produced by character mapping) is a candidate for Unicode Normalization if requested by the normalization-form parameter, and JSON escaping. JSON escaping replaces the characters U+005C (REVERSE SOLIDUS, BACKSLASH, \) , U+0008 (BACKSPACE) , U+000C (FORM FEED) , U+000A (NEWLINE) , U+000D (CARRIAGE RETURN) , U+0009 (TAB) , or U+005C (REVERSE SOLIDUS, BACKSLASH, \) by the corresponding JSON escape sequences \", \b, \f, \n, \r, \t, or \\ respectively, and any other codepoint in the range 1-31 or 127-159 by an escape in the form \uHHHH where HHHH is the hexadecimal representation of the codepoint value. Escaping further replaces the solidus character (/) by the escape sequence \/ if the escape-solidus parameter is set to true, but not if it is set to false. Escaping is also applied to any characters that cannot be represented in the selected encoding.

  3. The resulting string is enclosed in double quotation marks.

Finally, encoding, as controlled by the encoding parameter, converts the character stream produced by the preceding rules into an octet stream. The encoding parameter is ignored if canonical is true.

9.3 The Influence of Serialization Parameters upon the JSON Output Method

When nodes are serialized using the JSON output method, serialization is delegated to the output method specified by the json-node-output-method serialization parameter. The omit-xml-declaration parameter is set to true, and no other serialization parameters are passed down to the serialization method responsible for serializing the node.

The serialization parameters that affect the JSON output method are listed in the following subsections.

Serialization parameters other than those listed are not applicable to this output method. It is the responsibility of the host language to specify whether an error occurs if such a parameter is specified in combination with the JSON output method, or whether the parameter is ignored, or whether it is validated and then ignored.

9.3.1 JSON Output Method: the canonical Parameter

The canonical parameter specifies whether the JSON output should be normalized to its canonical form, as specified by [RFC8785]. The default value is false.

If the value is true this imposes constraints on the representation of maps, strings, and numbers, as described in 9.1 Serializing Items. All other parameters except normalization-form, media-type, json-lines, and json-node-output-method are ignored (except that they are passed though to any sub-serialization of contained nodes).

The serializermust serialize the input tree according to [RFC8785].

Note:

[RFC8785] defines the input to canonicalization as an I-JSON text [RFC7493]. The rules given in 9.1 Serializing Items effectively define how any XDM value in the input tree is mapped to a value in the implicit data model of I-JSON. Values that cannot be mapped to I-JSON, such as function items, result in a serialization error.

Note:

The xs:double values NaN, INF, and -INF cannot be represented in canonical JSON. A serialization error therefore occurs if these values appear in the input tree.

A serialization error [err:SERE0024] results if any error is raised during the canonicalization process.