Please check the errata for any errors or issues reported since publication.
See also translations.
This document is also available in these non-normative formats: XML.
Copyright © 2000 W3C® (MIT, ERCIM, Keio, Beihang). W3C liability, trademark and document use rules apply.
This document defines serialization of an instance of the data model as defined in [XDM 4.0] into a sequence of octets. Serialization is designed to be a component that can be used by other specifications such as [XSLT 4.0] or [XQuery 4.0].
This section describes the status of this document at the time of its publication. Other documents may supersede this document. A list of current W3C publications and the latest revision of this technical report can be found in the W3C technical reports index at https://www.w3.org/TR/.
This document is governed by the 1 March 2017 W3C Process Document.
This is a Recommendation of the W3C. It was jointly developed by the W3C XSLT Working Group and the W3C XML Query Working Group, each of which is part of the XML Activity.
This Editor's Draft specifies XSLT and XQuery Serialization version 4.0, a fully compatible extension of Serialization version 3.1.
This specification is designed to be referenced normatively from other specifications defining a host language for it; it is not intended to be implemented outside a host language. The implementability of this specification has been tested in the context of its normative inclusion in host languages defined by the XQuery 3.1 and XSLT 3.0 specifications; see the XQuery 3.1 implementation report (and, in the future, the WGs expect that there will also be an XSLT 3.0 implementation report) for details.
No substantive changes have been made to this specification since its publication as a Proposed Recommendation.
Please report errors in this document using W3C's public Bugzilla system (instructions can be found at https://www.w3.org/XML/2005/04/qt-bugzilla). If access to that system is not feasible, you may send your comments to the W3C XSLT/XPath/XQuery public comments mailing list, public-qt-comments@w3.org. It will be very helpful if you include the string “[SER40]” in the subject line of your report, whether made in Bugzilla or in email. Please use multiple Bugzilla entries (or, if necessary, multiple email messages) if you have more than one comment to make. Archives of the comments and responses are available at https://lists.w3.org/Archives/Public/public-qt-comments/.
This document has been reviewed by W3C Members, by software developers, and by other W3C groups and interested parties, and is endorsed by the Director as a W3C Recommendation. It is a stable document and may be used as reference material or cited from another document. W3C's role in making the Recommendation is to draw attention to the specification and to promote its widespread deployment. This enhances the functionality and interoperability of the Web.
This document was produced by groups operating under the 5 February 2004 W3C Patent Policy. W3C maintains a public list of any patent disclosures (W3C XML Query Working Group) and a public list of any patent disclosures (W3C XSLT Working Group) made in connection with the deliverables of each group; these pages also include instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.
The publications of this community group are dedicated to our co-chair, Michael Sperberg-McQueen (1954–2024).
The serialization of maps retains the order of entries. [Issue 1651 PR 1703 14 January 2025]
The output of QNames reflects the new syntax for QName literals. [Issue 2059 PR TODO 23 June 2025]
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.
A JNode is serialized in the format jnode(K: V) where K is the adaptive serialization of the value of its ·selector· property V is the adaptive serialization of the value of its ·content· property.
For example, the JNode selected by the expression jtree({ "a": [4, 5] })/a is serialized as jnode("a":[4,5]).
A JNode is serialized as follows:
If the ·selector· property is absent (that is, for a parentless JNode), in the format jtree(V) where V is the adaptive serialization of the value of its ·content· property.
For example, the JNode selected by the expression jtree({ "a": [4, 5] }) is serialized as jtree({"a":[4,5]}).
If the ·selector· property is present, then in the format jnode(K: V) where K is the adaptive serialization of the value of its ·selector· property and V is the adaptive serialization of the value of its ·content· property.
For example, the JNode selected by the expression jtree({ "a": [4, 5] })/a is serialized as jnode("a":[4,5]).
A document, element, text, comment, or processing instruction node is serialized using the XML output method described in 5 XML Output Method.
An attribute or namespace node is serialized as if it had a containing element node. For example an attribute node might be serialized as the string xsi:type="xs:integer"; a namespace node might be serialized as xmlns:sns="http://example.com/sample-namespace".
Note:
This may result in output of QNames containing prefixes whose binding is not displayed.
An atomic itemXP is serialized as follows:
An instance of xs:boolean is serialized as true() or false().
An instance of xs:string, xs:untypedAtomic or xs:anyURI is serialized by enclosing the value in double quotation marks and doubling any quotes within the value; or optionally by enclosing the value in apostrophes and doubling any apostrophes within the value. The resulting value is then serialized using the Text output method described in 8 Text Output Method.
Note:
The Text output method will apply character expansion and encoding rules to this string as specified by the serialization parameters.
An instance of xs:integer or xs:decimal is serialized by converting the value to a string using the fn:string function.
An instance of xs:double is serialized by applying the function format-number(?, '0.0##########################e0') using the following default decimal format properties:
| Property name | Property value |
|---|---|
decimal-separator | U+002E (FULL STOP, PERIOD, .) |
exponent-separator | U+0065 (LATIN SMALL LETTER E, e) |
grouping-separator | U+002C (COMMA, ,) |
zero-digit | U+0030 (DIGIT ZERO, 0) |
digit | U+0023 (NUMBER SIGN, #) |
infinity | The string "INF" |
NaN | The string "NaN" |
minus-sign | U+002D (HYPHEN-MINUS, -) |
An instance of xs:NOTATION is serialized as a URI-qualified name (that is, in the form Q{uri}local).
An instance of xs:QName is serialized with a # character, followed by:
the local name if the name is in no namespace, or
the URI-qualified name otherwise (Q{uri}local).
An atomic item of any other type is serialized using the syntax of a constructor function: xs:TYPE("VAL") where TYPE is the name of the primitive type, and VAL is the result of applying the fn:string() function. For example, xs:date("2015-07-17"). The resulting string is then serialized using the Text output method described in 8 Text Output Method.
An array item is serialized using the syntax of a SquareArrayConstructorXP, that is as [member, member, ... ]. The members, which in general are sequences, are serialized in the form (item, item, ...) where the items are serialized by applying these rules recursively. The items are separated by commas (not by the item-separator character). The enclosing parentheses are optional if the sequence has length one.
Note:
The serializer should avoid outputting the parentheses if it is able to determine the length of the sequence before serializing the first item; but it is allowed to output parentheses around a singleton if this avoids buffering data in memory.
A map item is serialized using the syntax of a [XPath 4.0] section MapConstructor without the optional map keyword, that is in the format {key:value, key:value, ...}. The key/value pairs in the serialized output retain the entry orderDM of entries in the map. The key is serialized by applying the rules for serializing an atomic item. The values are serialized in the same way as the members of an array (see above).
A function item is serialized to the representation name#A where fn:name is a representation of the function name and A is the arity. If the function name is in one of the namespaces http://www.w3.org/2005/xpath-functions, http://www.w3.org/2005/xpath-functions/math, http://www.w3.org/2005/xpath-functions/map, http://www.w3.org/2005/xpath-functions/array or http://www.w3.org/2001/XMLSchema, then the name is output as a lexical QName using the conventional prefix fn, math, map, array, or xs as appropriate; if it is in any other namespace or in no namespace, then the name is output as a URI-qualified name (that is, Q{uri}local). If the function is anonymous, name is replaced by the string (anonymous-function).
Note:
The following examples illustrate this rule:
fn:exists#1 is serialized as function fn:exists#1
Q{http://www.w3.org/2005/xpath-functions}exists#1 is serialized as fn:exists#1
function($a) { $a } is serialized as (anonymous-function)#1
math:pi#0 is serialized as math:pi#0
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 	 or  )
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:
to allow the type of atomic itemsXP to be ascertained.
to allow the namespace binding of prefixes to be ascertained.
to provide further information about the cause of error indicators.