saxon:serialize

Returns the serialization of a node, that is its representation in XML angle-bracket form.

serialize($node as node(), $format as xs:string) ➔ xs:string

Arguments

 

$node

node()

The node to be serialized

 

$format

xs:string

The name of an xsl:output declaration

Result

xs:string

serialize($node as node(), $format as element(xsl:output)) ➔ xs:string

Arguments

 

$node

node()

The node to be serialized

 

$format

element(xsl:output)

An element node whose content has the form of an xsl:output declaration

Result

xs:string

Namespace

http://saxon.sf.net/

Notes on the Saxon implementation

Available since Saxon 7.1. The function is obsolescent; the standard fn:serialize() function in XPath 3.0 should be used in preference.

Changes made in response to bug 3815 remove the requirement that the output format name be supplied as a string literal.

Details

This function takes two arguments: the first is a node (generally a document or element node) to be serialized. The second argument defines the serialization properties. The second argument takes several possible forms:

  • When called within an XSLT stylesheet, the second argument may be the name of an xsl:output element in the stylesheet.
  • In non-XSLT environments, the second argument may be the name of the output method (xml, html, xhtml, text), written as a string literal. In this case the other serialization parameters are defaulted.
  • In all environments, the second argument may be an xsl:output element conforming to the rules defined in the XSLT specification. This element may be constructed dynamically, and may therefore be used to decide all the serialization properties dynamically.

For example, the function may be used as follows in XQuery:

declare namespace saxon="http://saxon.sf.net/"; declare namespace xsl="http://www.w3.org/1999/XSL/Transform"; <out>{ let $x := <a><b/><c>content</c><?pi?><!--comment--></a> return saxon:serialize($x, <xsl:output method="xml" omit-xml-declaration="yes" indent="yes" saxon:indent-spaces="1"/>) }</out>

The function serializes the specified document, or the subtree rooted at the specified element, according to the parameters specified, and returns the serialized document as a string.

This function is useful where the stylesheet or query wants to manipulate the serialized output, for example by embedding it as CDATA inside another XML document, or prefixing it with a DOCTYPE declaration, or inserting it into a non-XML output file.

Note that because the output is a string, the encoding parameter has no effect on the actual encoding, though it does affect what is written to the XML declaration.

See also:

fn:serialize()