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 30 January2 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).


1 Introduction

Changes in 4.0 (next)

  1. If a section of this specification has been updated since version 3.1, an overview of the changes is provided, along with links to navigate to the next or previous change.

  2. Sections with significant changes are marked with a ✭ symbol in the table of contents.

This document defines methods of serializing the W3C XQuery and XPath Data Model 4.0 ([XDM 4.0]), that is, methods of representing instances of the data model as strings or octet sequences. This is the data model used by [XPath 4.0], [XSLT 4.0], and [XQuery 4.0], and any other specifications that reference it.

In this document, examples and material labeled as “Note” are provided for explanatory purposes and are not normative.

Serialization is the process of converting an instance of the [XDM 4.0] into a sequence of octets.

[Definition: The XDM value supplied as input to the serializer is referred to as the input value.] Some serialization methods apply only to certain types of input value.

Note:

Where serialization is used to process the result of an XQuery evaluation or an XSLT transformation, the input value of the serializer corresponds to the output from XQuery or XSLT.

[Definition: In general the output of the serializer will represent the items actually present in the input value, together with other items that are reachable from these, for example (in the case of nodes) their descendants. The complete set of items that are represented in the output of the serializer is referred to (without loss of generality) as the input tree.]

1.1 Terminology

Changes in 4.0 (next | previous)

  1. The term atomic value has been replaced by atomic item.   [Issue 1337  2 August 2024]

In this specification, where they are rendered in small capitals, the words must, must not, should, should not, may, required, and recommended are to be interpreted as described in [RFC2119].

[Definition: As is indicated in 12 Conformance, conformance criteria for serialization are determined by other specifications that refer to this specification. A serializer is software that implements some or all of the requirements of this specification in accordance with such conformance criteria.] A serializer is not required to directly provide a programming interface that permits a user to set serialization parameters or to provide an input sequence for serialization. In this document, material labeled as "Note" and examples are provided for explanatory purposes and are not normative.

Certain aspects of serialization are described in this specification as implementation-defined or implementation-dependent.

[Definition: Implementation-defined indicates an aspect that may differ between serializers, but whose actual behavior must be specified either by another specification that sets conformance criteria for serialization (see 12 Conformance) or in documentation that accompanies the serializer.]

[Definition: Implementation-dependent indicates an aspect that may differ between serializers, and whose actual behavior is not required to be specified either by another specification that sets conformance criteria for serialization (see 12 Conformance) or in documentation that accompanies the serializer.]

[Definition: In some instances, the input tree cannot be successfully converted into a sequence of octets given the set of serialization parameter (3 Serialization Parameters) values specified. A serialization error is said to occur in such an instance.] In some cases, a serializer is required to raise such an error. What it means to raise a serialization error is determined by the relevant conformance criteria (12 Conformance) to which the serializer conforms. In other cases, there is an implementation-defined choice between raising a serialization error and performing a recovery action. Such a recovery action will allow a serializer to produce a sequence of octets that might not fully reflect the usual requirements of the parameter settings that are in effect.

[Definition: Where this specification indicates that two strings are to be compared without regard to case, the serializermust translate any characters in the range U+0041 (LATIN CAPITAL LETTER A, A) through U+005A (LATIN CAPITAL LETTER Z, Z) inclusive, to the corresponding lower-case letters in the range U+0061 (LATIN SMALL LETTER A, a) through U+007A (LATIN SMALL LETTER Z, z) only for the purposes of making the comparison. The comparison succeeds if the two strings are the same length and the code point of each character in the first string is equal to the code point of the character in the corresponding position in the second string.]

Many terms used in this document are defined in the XPath specification [XPath 4.0] or the Data Model specification [XDM 4.0]. Particular attention is drawn to the following:

Where this specification indicates that an XSLT instruction is evaluated, the behavior is as specified by [XSLT 4.0]. Where it indicates that an XQuery expression is evaluated, the behavior is as specified by [XQuery 4.0].

3 Serialization Parameters

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. Added the canonical parameter for XML, XHTML, and JSON serialization.   [Issue 938 PR TODO 20 October 2025]

There are a number of parameters that influence how serialization is performed. Host languagesmay allow users to specify any or all of these parameters, but they are not required to be able to do so. However, the host language specification must specify how the values of all applicable parameters are to be determined.

Host languages may also define alternative representations of the values of serialization parameters. For example, both XSLT and XQuery allow the boolean values true and false to be written as 1/0 or yes/no. The $options map passed to the fn:serialize function, by contrast, requires an xs:boolean value.

It is a serialization error [err:SEPM0016] if a parameter value is invalid for the given parameter. It is the responsibility of the host language to specify how invalid values should be handled at the level of that language.

The following serialization parameters are defined:

Serialization parameters
Serialization parameter namePermitted values for parameter
allow-duplicate-namesA boolean value, true or false. This parameter indicates whether a map item serialized as a JSON object using the JSON output method is allowed to contain duplicate member names. If the value false is specified, a serialization error [err:SERE0022] may be raised under certain conditions.
byte-order-markA boolean value, true or false. This parameter indicates whether the serialized sequence of octets is to be preceded by a Byte Order Mark (See Section 5.1 of [Unicode Encoding]). The actual octet order used is implementation-dependent. If the encoding defines no Byte Order Mark, or if the Byte Order Mark is prohibited for the specific encoding or implementation environment, then this parameter is ignored.
canonicalA boolean value, true or false.
cdata-section-elementsA list of expanded QNames, possibly empty.
doctype-publicA string of PubidCharXML characters. This parameter may be absent.
doctype-systemA string of Unicode characters that does not include both the characters U+0027 (APOSTROPHE, ') and U+0022 (QUOTATION MARK, ") . This parameter may be absent.
encodingA string of Unicode characters in the range U+0021 (EXCLAMATION MARK, !) through U+007E (TILDE, ~) (that is, printable ASCII characters); the value should be a charset registered with the Internet Assigned Numbers Authority [IANA], [RFC2978] or begin with the characters x- or X-.
escape-solidusA boolean value, true or false.
escape-uri-attributesA boolean value, true or false.
html-versionA decimal value. This parameter may be absent.
include-content-typeA boolean value, true or false.
indentA boolean value, true or false.
item-separatorA string of Unicode characters. This parameter may be absent.
json-linesA boolean value, true or false.
json-node-output-methodAn expanded QName with a non-null namespace URI, or with a null namespace URI and a local name equal to one of xml, xhtml, html or text. If the namespace URI is non-null, the parameter specifies an implementation-defined output method.
media-typeA string of Unicode characters specifying the media type (MIME content type) [RFC2046]; the charset parameter of the media type must not be specified explicitly in the value of the media-type parameter. If the destination of the serialized output is annotated with a media type, this parameter may be used to provide such an annotation. For example, it may be used to set the media type in an HTTP header.
methodAn expanded QName with a non-null namespace URI, or with a null namespace URI and a local name that must be equal to one of xml, xhtml, html, text, json, or adaptive, in which case, the output method specified must be used for serializing. If the namespace URI is non-null, the parameter specifies an implementation-defined output method; its behavior is not specified by this document.
normalization-formOne of the enumerated values NFC, NFD, NFKC, NFKD, fully-normalized or none, or an implementation-defined value of type NMTOKEN.
omit-xml-declarationA boolean value, true or false.
standaloneEither a boolean value, true or false, or the value or omit.
suppress-indentationA list of expanded QNames, possibly empty.
undeclare-prefixesA boolean value, true or false.
use-character-mapsA list of pairs, possibly empty, with each pair consisting of a single Unicode character and a string of Unicode characters.
versionA string of Unicode characters. This parameter may be absent.

In those cases where they have no important effect on the content of the serialized result, details of the output methods defined by this specification are left unspecified and are regarded as implementation-dependent. Whether a serializer uses apostrophes or quotation marks to delimit attribute values in the XML output method is an example of such a detail.

The detailed semantics of each parameter will be described separately for each output method for which it is applicable. If the semantics of a parameter are not described for an output method, then it is not applicable to that output method.

Implementations may define additional serialization parameters, and may allow users to do so. For this purpose, the name of a serialization parameter is considered to be a QName; the parameters listed above are QNames whose expanded-QName has a null namespace URI, while any additional serialization parameters that are either implementation-defined or defined by the host languagemust have names that are namespace-qualified. Any such additional serialization parameters must not be in the namespace https://www.w3.org/2010/xslt-xquery-serialization. A host languagemay specify the means by which an implementation can define such an additional serialization parameter, and implementations may provide mechanisms by which users can define such an additional serialization parameter. If the serialization method is one of the six methods xml, html, xhtml, text, json, or adaptive then the additional serialization parameters may affect the output of the serializer to the extent (but only to the extent) that this specification leaves the output implementation-defined or implementation-dependent. For example, such parameters might control whether namespace declarations on an element are written before or after the attributes of the element, or they might define the number of space or tab characters to be inserted when the indent parameter is set to true; but they could not instruct the serializer to suppress the error that occurs when the HTML output method encounters characters that are not permitted (see error [err:SERE0014]).

3.1 Setting Serialization Parameters by Means of a Parameter Document

A host languagemay provide, by reference to this section, a mechanism by which the settings of serialization parameters are supplied in the form of an output:serialization-parameters element node.

Note:

The namespace prefix output is assumed to be bound to the namespace URI http://www.w3.org/2010/xslt-xquery-serialization. The document may use any namespace prefix or none.

[Definition: An output:serialization-parameters element node used to hold the settings of serialization parameters is referred to as a parameter document].

Note:

The use of the word document does not imply that the output:serialization-parameters element must be the outermost element of an XDM document, although this will often be the case.

The parameter documentmust be processed as if by the procedure described below.

With the exception of the use-character-maps parameter, the setting of each serialization parameter defined in this specification is equal to the result of evaluating the XQuery expression

document { . } 
   /output:serialization-parameters
   /(validate lax { 
      output:*[local-name() eq $param-name] 
   })
   /data(@value)

or equivalently the XSLT instructions

<xsl:sequence>
  <xsl:variable name="validated-instance">
    <xsl:document validation="lax">
      <xsl:sequence select="
        self::output:serialization-parameters
        /output:*
       [local-name() eq $param-name]"/>
    </xsl:document>
  </xsl:variable>
  <xsl:sequence select="$validated-instance
                        /data(@value)"/>
</xsl:sequence>

with the parameter document as the context value, the param-name variable bound to a value of type xs:string equal to the local part of the name of the particular serialization parameter, and the other components of the dynamic context and static context as specified in the subsequent tables. If in any case evaluating this expression would yield an error, serialization error [err:SEPM0017] results.

If the result of evaluating this expression for a particular serialization parameter is the empty sequence, then

  1. If the parameter is either cdata-section-elements or suppress-indentation, and the result of evaluating the XQuery expression

    document { . }
    /output:serialization-parameters
    /(validate lax {  
       output:*[local-name() eq $param-name]
    })

    or equivalently the XSLT instructions

    <xsl:sequence>
      <xsl:variable name="validated-instance">
        <xsl:document select="." validation="lax">
          <xsl:sequence select="
            self::output:serialization-parameters
            /output:*
           [local-name() eq $param-name]"/>
        </xsl:document>
      </xsl:variable>
      <xsl:sequence select="$validated-instance"/>
    </xsl:sequence>

    with the same settings of the static context and dynamic context is not anthe empty sequence, the setting of the parameter is the empty list;

  2. otherwise, the setting of the parameter is absent.

The components of the static context used in evaluating the XQuery expressions or XSLT instructions are as defined in the following table.

Settings of static context components used in extracting serialization parameter settings from a parameter document
Static Context ComponentXQuery or XSLTSetting
XPath 1.0 compatibility modeBothfalse
Statically known namespacesXQueryThe pair (output,http://www.w3.org/2010/xslt-xquery-serialization)
XSLTThe pairs (output,http://www.w3.org/2010/xslt-xquery-serialization), (xsl,http://www.w3.org/1999/XSL/Transform)
Default element/type namespaceBoth"none"
Default function namespaceBothhttp://www.w3.org/2005/xpath-functions
In-scope schema types, In-scope element declarations, Substitution groups, In-scope attribute declarationsBothAs defined by the schema for serialization parameters (B Schema for Serialization Parameters) and any additional implementation-defined in-scope schema components
In-scope variablesBoth{param-name}
Context value static typeBothnode()
Statically known function signaturesBoth{fn:data($arg as item()*) as xs:anyAtomicType*}, {fn:local-name($arg as node()?) as xs:string}
Statically known collationsBoth { (http://www.w3.org/2005/xpath-functions/collation/codepoint, The Unicode codepoint collation ) }
Default collationBothThe Unicode codepoint collation
Construction modeXQuerystrip
Ordering modeXQueryordered
Default order for empty sequencesXQueryleast
Boundary space policyXQuerystrip
Copy-namespaces modeXQuery(preserve,inherit)
Base URIBothAbsent
Statically known documentsBothNone
Statically known collectionsBothNone
Statically known default collection typeBothnode()*
Statically known decimal formatsBothNone
Set of named keysXSLT{}
Values of system propertiesXSLTNone
Set of available instructionsXSLTThe set of all instructions defined by [XSLT 4.0]

The remaining components of the dynamic context used in evaluating the XQuery expressions or XSLT instructions in the preceding table are as defined in the following table.

Settings of dynamic context components used in extracting serialization parameter settings from a parameter document
Dynamic Context ComponentXQuery or XSLTSetting
Context positionBoth1
Context sizeBoth1
Variable valuesBothThe param-name variable has a value of type xs:string equal to the local part of the name of the serialization parameter under consideration
Function implementationsBothThe implementation of fn:data
Current dateTimeBothAbsent
Implicit timezoneBothAbsent
Available documentsBothNone
Available collectionsBothNone
Default collectionBothNone
Current template ruleXSLTAbsent
Current modeXSLTThe default mode
Current groupXSLTAbsent
Current grouping keyXSLTAbsent
Current captured substringsXSLTThe empty sequence
Output stateXSLTTemporary output state

In the case of the use-character-maps parameter, the XQuery expression

document { . }
/output:serialization-parameters
/ ( validate lax { output:use-character-maps } )
/output:character-map[@character eq $char]
/string(@map-string)

or equivalently the XSLT instructions

<xsl:sequence>
  <xsl:variable name="validated-instance">
    <xsl:document validation="lax">
      <xsl:sequence select="
        self::output:serialization-parameters
         /output:use-character-maps"/>
    </xsl:document>
  </xsl:variable>
  <xsl:sequence select="$validated-instance                          
                        /output:character-map
                        [@character eq $char]
                        /string(@map-string)"/>
</xsl:sequence>

is evaluated for each Unicode character that is permitted in an XML document. The dynamic context and static context used to evaluate the expression are as defined above, except that the component In-scope variables is the set {char} and the value of the variable "char" is a value of type xs:string of length one whose value is the Unicode character under consideration. If the result of evaluating the expression is not anthe empty sequence, the pair consisting of the Unicode character and the result of evaluating the expression is part of the list of pairs in the value of the use-character-maps parameter. It is a serialization error [err:SEPM0018] if the result of evaluating this expression for any character is a sequence of length greater than one.

Using the same settings of the components of the dynamic context and static context, serialization error [err:SEPM0019] results if the result of evaluating the following XQuery expression is not true

(document { . })/output:serialization-parameters
   /(count(distinct-values(*/node-name(.))) eq (count(*)))

or equivalently if the result of evaluating the following XSLT instructions is not true.

<xsl:sequence>
  <xsl:variable name="doc">
    <xsl:document>
      <xsl:sequence select="."/>
    </xsl:document>
  </xsl:variable>
  <xsl:sequence
    select="$doc/output:serialization-parameters
                /(count(distinct-values(
                    */node-name(.))) 
                eq (count(*)))"/>
</xsl:sequence>

The result of evaluating either will be false if the parameter document supplies a value for any particular serialization parameter more than once, or will be the empty sequence if the parameter document is not an element node whose local name is serialization-parameters and whose namespace URI is http://www.w3.org/2010/xslt-xquery-serialization.

Note:

A serializer or implementation of a host language does not need to be accompanied by an XQuery processor nor by a general-purpose schema validator in order to meet the requirements of this section. It merely needs to be capable of extracting values from an XDM instance that conforms to the schema for serialization parameters, while checking that the constraints implied by the schema and additional constraints implied by the XQuery validate expression or explicitly stated in this section are satisfied.

The host languagemay provide additional mechanisms for overriding the values of any serialization parameters specified through the mechanism defined in this section, as well as additional mechanisms for specifying the values of any serialization parameters whose values are absent after applying the mechanism defined in this section.

If the parameter document contains elements or attributes that are in a namespace other than http://www.w3.org/2010/xslt-xquery-serialization, the implementation may interpret them to specify the values of implementation-defined serialization parameters in an implementation-defined manner.

The following XML document, if parsed as a parameter document and processed using the mechanism described in this section, would specify the settings of the method, version and indent serialization parameters with the values xml, 1.0 and true, respectively.

<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:serialization-parameters>

The following document would specify the value of the cdata-section-elements serialization parameter with value equal to the pair of expanded QNames (http://example.org/book/chapter,heading) and (http://example.org/book,footnote)

<output:serialization-parameters
    xmlns:output
    = "http://www.w3.org/2010/xslt-xquery-serialization"
    xmlns:book="http://example.org/book"
    xmlns="http://example.org/book/chapter">
  <output:cdata-section-elements value="heading book:footnote"/>
</output:serialization-parameters>

The following document would specify the value of the method serialization parameter with the value html.

Notice that in this example, the default namespace declaration in scope has no effect on the interpretation of the setting of the method parameter.

<output:serialization-parameters
    xmlns:output
    = "http://www.w3.org/2010/xslt-xquery-serialization"
    xmlns="http://example.org/ext">
  <output:method value="html"/>
</output:serialization-parameters>

The following document would specify the value of the method serialization parameter with value equal to the expanded QName (http://example.org/ext, jsp), and the use-character-maps parameter with value equal to the list of pairs, («, <%), (», %>).

<output:serialization-parameters
    xmlns:output
    = "http://www.w3.org/2010/xslt-xquery-serialization"
    xmlns:ext="http://example.org/ext">
  <output:method value="ext:jsp"/>
  <output:use-character-maps>
    <output:character-map character="&#xAB;" map-string="&lt;%"/>
    <output:character-map character="&#xBB;" map-string="%&gt;"/>
  </output:use-character-maps>
</output:serialization-parameters>

7 HTML Output Method

Changes in 4.0 (next | previous)

  1. In the HTML and XHTML output methods, the rules for adding and replacing meta elements have been revised to take account of the new HTML5 syntax, for example <meta charset="UTF-8">.  [Issue 318 PR 342 14 February 2023]

  2. The default HTML version is now 5. This may result in changes to the serialized output in cases where no explicit HTML version is requested.  [Issue 1889 PR 1977 2 May 2025]

The HTML output method serializes the input tree as HTML.

For example, the following XSL stylesheet generates html output,

<xsl:stylesheet version="2.0" 
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="4.0"/>
<xsl:template match="/">
  <html>
    <xsl:apply-templates/>
  </html>
</xsl:template>
...
</xsl:stylesheet>

In the example, the version attribute of the xsl:output element indicates the version of the HTML Recommendation [HTML] to which the serialized result is to conform.

[Definition: The requested HTML version is the value of the html-version serialization parameter if present; otherwise the value of the version serialization parameter if present; otherwise 5.0.]

This document provides the normative definition of serialization for the HTML output method if the requested HTML version has the lexical form of a value of type decimal whose value is 1.0 or greater, but no greater than 5.0. For any other requested HTML version, the behavior is implementation-defined. In that case the implementation-defined behavior may supersede all other requirements of this recommendation.

An implementation is required to behave as specified in this document when the requested version is 5.0. If the requested version is greater than or equal to 1 but less than 5.0, then the processor may behave as if the requested version were 5.0.

It is entirely the responsibility of the supplier of the input tree to ensure that it conforms to the relevant HTML specification. It is not an error if the input tree is invalid HTML. Equally, it is entirely under the control of the supplier of the input tree whether the output conforms to HTML. If the result tree is valid HTML, the serializermust serialize the result in a way that conforms with the requested HTML version.

7.4 Writing Comments and Processing Instructions

Changes in 4.0 (next | previous)

  1. The way processing instructions are handled when generating HTML5 has changed, reflecting the fact that HTML5 no longer recognizes processing instructions.  [Issue 2344 PR 2372 20 January 2026]

Comment nodes are written as HTML comments, using the syntax <!--comment-->.

Prior to HTML5, the HTML output method must terminate processing instructions with > rather than ?>. It is a serialization error [err:SERE0015] to use the HTML output method when > appears within a processing instruction in input tree.

With HTML5, a processing instruction must be written as an HTML comment, in the form <!--?targetdata?-->. Here target is the name of the processing instruction, and data is its string value; they are separated by a single space, but this omitted in the case where data is zero-length. If theeither the name or the string value of the processing instruction contains two adjacent hyphens, they must be separated in the output by a single space. The space separator is omitted if the string value of the processing instruction is zero-length. For example, the processing instruction <?xml-stylesheet type="text/xsl" href="render.xsl"?> is serialized as <--?xml-stylesheet type="text/xsl" href="render.xsl"?-->

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]).

    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.

    • 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 anthe 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.2 JSON Lines

When the json-lines parameter is set to true, the serialized output is written in json-lines format, as defined in [JSON Lines].

If json-lines is set to true, then each item in the input value is serialized indepedently as a JSON text, and the resulting serializations are then concatenated using a single U+000A (NEWLINE) character as a separator.

If json-lines and indent are both set to true, then the serialization of each individual item may include added U+0020 (SPACE) and U+0009 (TAB) characters for formatting purposes, but it must not include U+000A (NEWLINE) or U+000D (CARRIAGE RETURN) characters.

The json-lines specification allows U+000D (CARRIAGE RETURN) characters to appear, and does not treat them as significant; however, they are likely to cause practical problems. For example processing such input using the XPath expression unparsed-text-lines($uri) ! parse-json() would fail. The serializer must not output any U+000D (CARRIAGE RETURN) characters, either at the end of a line or elsewhere.

The json-lines specification allows a terminating U+000A (NEWLINE) character after the last line. In the interests of interoperability, however, the serializer must not output such a terminator.

If the input value is anthe empty sequence, then it is serialized as a zero-length string (rather than as the string null).

The item-separator parameter has no effect.

10 Adaptive Output Method

Changes in 4.0 (next | previous)

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

  2. The output of QNames reflects the new syntax for QName literals.   [Issue 2059 PR TODO 23 June 2025]

  3. A JNode is represented as jnode(K: V) where K is its ·selector· property and V is its ·content· property.   [Issues 2087 2186 PRs 2114 2226 22 June 2025]

The Adaptive output method serializes the input tree into a human readable form for the purposes of debugging query results. The intention of this is to allow any input value to be serialized without raising a serialization error. Sequence normalization is not performed for this output method.

Each item in the supplied sequence is serialized individually as follows, with an occurrence of the chosen item-separator between successive items.

Character maps are applied (a) when nodes are serialized using the XML output method, and (b) to any value represented as a string enclosed in quotation marks.

Optionally, in all the above constructs, characters whose visual representation is ambiguous (for example tab or non-breaking-space) may be represented in the form of an XML numeric character reference (for example &#x9; or &#xa0;)

Note:

In many cases the serialization of an item conforms to the syntax of an XQuery expression whose result is that item. There are exceptions, however. For example, the syntax will not be valid XQuery in the case of free-standing attribute or namespace nodes, or QName values, or anonymous functions; and where it is valid XQuery, the result of evaluating the expression will not necessarily be identical to the original: for example, the distinction between strings and untypedAtomic items is lost.

If any value cannot be output because doing so would cause a serialization error, the behavior is implementation-defined.

If the output is sent to a destination that allows hyperlinks to be included in the generated text, then the serializer may include implementation-dependent hyperlinks to provide additional information for example:

E Glossary (Non-Normative)

array item

The term array item is defined in [XDM 4.0] section 8.3 Array Items.

atomize

The term atomization is defined in [XPath 4.0] section 2.6.3 Atomization.

character

The term character is defined in [XDM 4.0] section 4.1.5 XML and XSD Versions.

codepoint

The term codepoint is defined in [XDM 4.0] section 4.1.5 XML and XSD Versions.

content

The term content has the same meaning as the term ContentXML defined in 3.1 Start-Tags, End-Tags, and Empty-Element TagsXML of [XML10].

EMPTY

The following XHTML elements have an EMPTY content model: area, base, br, col, embed, hr, img, input, link, meta, basefont, frame, isindex, and param.

expanded QName

The term expanded QName is defined in [XPath 4.0] section 2 Basics. An expanded QName consists of an optional namespace URI and a local name. An expanded QName also retains its original namespace prefix (if any), to facilitate casting the expanded QName into a string.

expected-empty

An element node is expected to be empty if it is recognized as an HTML element and:

function item

The term function item is defined in [XDM 4.0] section 8.1 Function Items.

host language

A host language is another specification that includes, by reference, this specification and all of its requirements. A host language might be a programming language such as [XSLT 4.0] or [XQuery 4.0], or it might be an application programming interface (API) intended to be used by programs written in some other high-level programming language. The use of the term language is not intended to preclude the possibility that this specification might be referenced outside the context of a programming language specification.

immediate content

The immediate content of an element is the part of the content of the element that is not also in the content of a child element of that element.

implementation-defined

Implementation-defined indicates an aspect that may differ between serializers, but whose actual behavior must be specified either by another specification that sets conformance criteria for serialization (see 12 Conformance) or in documentation that accompanies the serializer.

implementation-dependent

Implementation-dependent indicates an aspect that may differ between serializers, and whose actual behavior is not required to be specified either by another specification that sets conformance criteria for serialization (see 12 Conformance) or in documentation that accompanies the serializer.

input tree

In general the output of the serializer will represent the items actually present in the input value, together with other items that are reachable from these, for example (in the case of nodes) their descendants. The complete set of items that are represented in the output of the serializer is referred to (without loss of generality) as the input tree.

input value

The XDM value supplied as input to the serializer is referred to as the input value.

map item

The term map item is defined in [XDM 4.0] section 8.2 Map Items.

MathML namespace

the MathML namespace namespace, https://www.w3.org/1998/Math/MathML.

node

The term node is defined in [XPath 4.0] section 2 Basics. There are seven kinds of nodes in the data model: document, element, attribute, text, namespace, processing instruction, and comment.

non-null namespace URI

An element or attribute that does not have a null namespace URI, is referred to as having a non-null namespace URI

null namespace URI

An expanded-QName whose namespace part is anthe empty sequence, or an element or attribute whose name expands to such an expanded-QName, is referred to as having a null namespace URI

Output declaration namespace

the Output declaration namespace, https://www.w3.org/2010/xslt-xquery-serialization

parameter document

An output:serialization-parameters element node used to hold the settings of serialization parameters is referred to as a parameter document

prefix normalization

During prefix normalization, any element node in the input tree that is in one of the XHTML namespace, the SVG namespace or the MathML namespace has its name replaced by the local part of its name. Such an element node is given a default namespace node whose value is the element’s namespace URI. Any namespace node for any of those three namespaces that was previously present on any element node in the input tree is also removed, unless the prefix that that namespace node declared is used as the prefix on the name of an attribute on that element or an ancestor of that element.

prior to HTML5

The term prior to HTML5 is used in this specification to qualify rules that apply only when the effective version of the html-version serialization parameter is less than 5.0.

recognized as an HTML element

An element node is recognized as an HTML element by the XHTML output method if either of the following conditions is true:

reconstructed tree

A reconstructed tree may be constructed by parsing the XML document and converting it into an document node as specified in [XDM 4.0].

requested HTML version

The requested HTML version is the value of the html-version serialization parameter if present; otherwise the value of the version serialization parameter if present; otherwise 5.0.

result tree

The result of the sequence normalization process is a result tree.

sequence

The term sequence is defined in [XPath 4.0] section 2 Basics. A sequence is an ordered collection of zero or more items.

sequence normalization

The purpose of sequence normalization is to create a sequence that can be serialized as a well-formed XML document or external general parsed entity, that also reflects the content of the input sequence to the extent possible.

serialization error

In some instances, the input tree cannot be successfully converted into a sequence of octets given the set of serialization parameter (3 Serialization Parameters) values specified. A serialization error is said to occur in such an instance.

serialized as an HTML element

An element node is serialized as an HTML element if

serializer

As is indicated in 12 Conformance, conformance criteria for serialization are determined by other specifications that refer to this specification. A serializer is software that implements some or all of the requirements of this specification in accordance with such conformance criteria.

string

The term string is defined in [XDM 4.0] section 4.1.5 XML and XSD Versions.

string value

The term string value is defined in [XDM 4.0] section 7.6.12 string-value Accessor. Every node has a string value. For example, the string value of an element is the concatenation of the string values of all its descendant text nodes.

SVG namespace

the SVG namespace, https://www.w3.org/2000/svg

to a JSON string

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

Unicode Normalization

Unicode Normalization is the process of removing alternative representations of equivalent sequences from textual data, to convert the data into a form that can be binary-compared for equivalence, as specified in [UAX #15: Unicode Normalization Forms]. For specific recommendations for character normalization on the World Wide Web, see [Character Model for the World Wide Web 1.0: Normalization].

URI attribute values

The values of attributes listed in D List of URI Attributes are URI attribute values. Attributes are not considered to be URI attributes simply because they are namespace declaration attributes or have the type annotation xs:anyURI.

URI Escaping

URI escaping consists of the following three steps applied in sequence to the content of URI attribute values:

void

The void elements of HTML5 are area, base, br, col, embed, hr, img, input, keygen, link, meta, param, source, track and wbr.

whitespace character

A space character, TAB character, CR character or NL character is referred to as a whitespace character.

with HTML5

The term with HTML5 is used in this specification to qualify rules that apply only when the effective version of the html-version serialization parameter is 5.0.

without regard to case

Where this specification indicates that two strings are to be compared without regard to case, the serializermust translate any characters in the range U+0041 (LATIN CAPITAL LETTER A, A) through U+005A (LATIN CAPITAL LETTER Z, Z) inclusive, to the corresponding lower-case letters in the range U+0061 (LATIN SMALL LETTER A, a) through U+007A (LATIN SMALL LETTER Z, z) only for the purposes of making the comparison. The comparison succeeds if the two strings are the same length and the code point of each character in the first string is equal to the code point of the character in the corresponding position in the second string.

XHTML namespace

the XHTML namespace namespace, https://www.w3.org/1999/xhtml

XML Island

The portion of the serialized document representing the result of serializing an element that is not to be serialized as an HTML element is known as an XML Island.

XML namespace

the XML namespace, https://www.w3.org/XML/1998/namespace