Changes

Jump to navigation Jump to search
2,858 bytes added ,  06:47, 29 November 2019
no edit summary
This page is part of the [[XQuery|XQuery Portal]].
Serialization parameters define how XQuery items and XML nodes are textually output, i.e., ''serialized''. (For input, see [[Parsers]].)
They have been formalized in the [http://www.w3.org/TR/xslt-xquery-serialization-30 W3C XQuery Serialization 3.0] document.
In BaseX, they can be specified in several ways:
Serialization parameters define how XQuery items and XML nodes will be ''serialized'' (i.e., returned to the client or an API, usually in textual form). The official parameters are defined in the [http://www.w3.org/TR/xslt-xquery-serialization-31 W3C XQuery Serialization 3.1] document. In BaseX, they can be specified by: * by including them in the [[XQuery_3.0#Serialization|prolog of the XQuery expression]],;* by specifying them in the XQuery functions [[File_Module#file:write|file:write()]] or [[XQuery_3.0#Functions|fn:serialize()]],*: . The serialization parameters can either be are specifiedas** as children of an {{Code|&lt;output:serialization-parameters/&gt;}} element, as defined for the [http://www.w3.org/TR/xpath-functions-30/#func-serialize fn:serialize()] function;, or as** as map, which contains all key/value pairs:**: <pre class="brush:xml"code>map { "method" := "xml", "cdata-section-elements" := "div", ... }</precode>;* by using the {{Code|-s}} flag of the BaseX [[Command-Line Options#BaseX Standalone|command-line]] clients,;* by setting the [[Options#SERIALIZER{{Option|SERIALIZER]] }} option before running a query,;* by setting the [[Options#EXPORTER{{Option|EXPORTER]] }} option before exporting a database, ; or* by setting them as [[REST#Parameters|REST]] query parameters<br/>. Due to the wide range of ways how parameters can be supplied, we deliberately ignored one rule of the specification, which requires non-official features to be defined in a non-null namespace URI. In the following, we will indicate which features are specific to our implementation.
=Parameters=
The following table gives a brief summary of all serialization parameters recognized are supported by BaseX. For (further details, please refer to can be looked up in the official specification.):
{| class="wikitable sortable" width="100%"
|- valign="top"
! width="140" | Parameter
! width="60%" | Description
! Allowed
! Default
|- valign="top"
| {{Code|method}}
| Specifies the serialization method:* . {{Code|xml}}, {{Code|xhtml}}, {{Code|html}}, {{Code|text}} and {{Code|textadaptive}} are adopted from part of the official specification.* For more details on {{Code|jsonbasex}} is specific to BaseX and can be used to output XML nodes as JSON objects (see the [[JSON Module]] for more details).* , {{Code|csv}} is BaseX-specific and can be used to output XML nodes as CSV data (see the [[CSV Module]] for more details).* {{Code|rawjson}} is BaseX-specific, too: Binary data types are output in their ''raw'' form, i.e., without modifications. For all other types, the items’ string values are returned. No indentation takes place, and and no characters are encoded via entitiessee [[XQuery Extensions#Serialization|XQuery Extensions]].| {{Code|xml}}, {{Code|xhtml}}, {{Code|html}}, {{Code|text}}, {{Code|json}}, {{Code|adaptive}}, {{Code|csv}}, {{Code|rawbasex}}| {{Code|xmlbasex}}
|- valign="top"
| {{Code|version}}
| Specifies the version of the serialization method.
| {{Code|xml/xhtml}}: {{Code|1.0}}, {{Code|1.1}}<br/>{{Code|html}}: {{Code|4.0}}, {{Code|4.01}}, {{Code|5.0}}<br/>
| {{Code|1.0}}
|- valign="top"
| {{Code|item-separator}}
| Determines a string to be used as item separator. If a separator is specified, the default separation of atomic values with single whitespaces will be skipped.
| ''arbitrary strings'', {{Code|\n}}, {{Code|\r\n}}, {{Code|\r}}
| ''empty''
|- valign="top"
|
| {{Code|application/xml}}
|- valign="top"
| {{Code|parameter-document}}
| Parses the value as XML document with additional serialization parameters (see the [http://www.w3.org/TR/xslt-xquery-serialization-31/#serparams-in-xdm-instance Serialization Specification] for more details).
|
|
|- valign="top"
| {{Code|use-character-maps}}
| Defines character mappings (not supported). May only occur in documents parsed with {{Code|parameter-document}}.
|
|
|- valign="top"
| {{Code|include-content-type}}
| Includes Inserts a {{Code|meta}} content-type element into the head element if the result is output as HTML<br />Example: <code>&lt;head&gt;&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;&lt;/head&gt;</code>. The head element must already exist or nothing will be added. Any existing {{Code|meta}} content-type elements will be removed.
| {{Code|yes}}, {{Code|no}}
| {{Code|noyes}}
|}
BaseX provides some additional, implementation-specific serialization parameters:
{| class="wikitable sortable" width="100%"
|- valign="top"
! width="140" | Parameter
! width="60%" | Description
! Allowed
! Default
|- valign="top"
| {{Code|csv}}
| Defines the way how data is serialized as CSV. {{Version|7.8}}
| see [[CSV Module]]
|
|- valign="top"
| {{Code|json}}
| Defines the way how data is serialized as JSON. {{Version|7.8}}
| see [[JSON Module]]
|
|- valign="top"
| {{Code|format}}
| Turns output formatting on/off, including the conversion of special characters to entities and insertion of item separators.
| {{Code|yes}}, {{Code|no}}
| {{Code|yes}}
|- valign="top"
| {{Code|tabulator}}
| ''positive number''
| {{Code|2}}
|- valign="top"
| {{Code|wrap-prefix}},<br />{{Code|wrap-uri}}
| Specifies a prefix and/or URI for wrapping the query results.
|
|
|- valign="top"
| {{Code|newline}}
| {{Code|\n}}, {{Code|\r\n}}, {{Code|\r}}
| ''system dependent''
|- valign="top"
| {{Code|limit}}
| Stops serialization after the specified number of bytes has been serialized. If a negative number is specified, everything will be output.
| ''positive number''
| {{Code|-1}}
|- valign="top"
| {{Code|binary}}
| Indicates if items of binary type are output in their native byte representation. Only applicable to the <code>base</code> serialization method.
| {{Code|yes}}, {{Code|no}}
| {{Code|yes}}
|}
The {{Code|csv}} and {{Code|json}} parameters are supplied with a list of options. Option names and values are combined with <code>=</code>, several options are separated by <code>,</code>:
'''Query''':
<pre class="brush:xquery">
(: The output namespace declaration is optional, because it is statically declared in BaseX) :)
declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";
declare option output:method "csv";
declare option output:csv "header=yes, separator=semicolon";
</record>
</csv>
</pre>
 
If {{Code|fn:serialize}} is called, output-specific parameters can be supplied via nested options:
 
<pre class="brush:xquery">
serialize(
<csv>
<record>
<Name>John</Name>
<City>Newton</City>
</record>
<record>
<Name>Jack</Name>
<City>Oldtown</City>
</record>
</csv>,
map {
'method': 'csv',
'csv': map { 'header': 'yes', 'separator': ';' }
}
)
</pre>
John;Newton
Jack;Oldtown
</pre>
 
=Character mappings=
 
Character maps allow a specific character in the instance of the data model to be replaced with a specified string of characters during serialization. The string that is substituted is output "as is," and the serializer performs no checks that the resulting document is well-formed. This may only occur in documents parsed with {{Code|parameter-document}}. If a character is mapped, then it is not subjected to XML or HTML escaping. For details refer to section [https://www.w3.org/TR/2015/CR-xslt-xquery-serialization-31-20151217/#character-maps 11 Character maps] in the [http://www.w3.org/TR/xslt-xquery-serialization-31 W3C XQuery Serialization 3.1] document
 
This example maps the Unicode U+00A0 NO-BREAK SPACE as &amp;#160; (without the serialization parameter, the Unicode character would be output):
 
'''Example query''':
<pre class="brush:xquery">
declare option output:parameter-document "map.xml";
<x>&amp;#xA0;</x>
</pre>
 
'''Example parameter-document''':
<pre class="brush:xml">
<serialization-parameters
xmlns="http://www.w3.org/2010/xslt-xquery-serialization">
<use-character-maps>
<character-map character="&amp;#160;" map-string="&amp;amp;#160;"/>
</use-character-maps>
</serialization-parameters>
</pre>
=Changelog=
 
;Version 9.2
 
* Updated: New default value for {{Code|include-content-type}} is {{Code|yes}}.
 
;Version 8.4
 
* Added: Serialization parameter {{Code|binary}}.
* Updated: New serialization method <code>basex</code>. By default, items of binary type are now output in their native byte representation. The method <code>raw</code> was removed.
 
;Version 8.0
 
* Added: Support for {{Code|use-character-maps}} and {{Code|parameter-document}}.
* Added: Serialization method {{Code|adaptive}}.
* Updated: {{Code|adaptive}} is new default method (before: {{Code|xml}}).
* Removed: {{Code|format}}, {{Code|wrap-prefix}}, {{Code|wrap-uri}}.
 
;Version 7.8.2
 
* Added: {{Code|limit}}: Stops serialization after the specified number of bytes has been serialized.
;Version 7.8
* Added: {{Code|csv}} and {{Code|json}} serialization parameters.* Removed: {{Code|separator}} option (use {{Code|item-separator}} instead).
;Version 7.7.2
* Added: {{Code|csv}} serialization method.* Added: temporary serialization methods {{Code|csv-header}}, {{Code|csv-separator}}, {{Code|json-unescape}}, {{Code|json-spec}}, {{Code|json-format}}.
;Version 7.5
* Added: official {{Code|item-separator}} and {{Code|html-version}} parameter.
* Updated: <code>method=html5</code> removed; serializers updated with the [http://www.w3.org/TR/2013/WD-xslt-xquery-serialization-30-20130108/ latest version of the specification], using <code>method=html</code> and <code>version=5.0</code>.
;Version 7.2
* Added: {{Code|separator}} parameter.
;Version 7.1
* Added: {{Code|newline}} parameter.
;Version 7.0
* Added: Serialization parameters added to [[REST API]]; JSON/JsonML/raw methods.
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu