Serial (XmlDoc/XmlNode function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
m (1 revision)
m (xpath arg)
 
(44 intermediate revisions by 8 users not shown)
Line 1: Line 1:
{{Template:XmlDoc/XmlNode:Serial subtitle}}
{{Template:XmlDoc/XmlNode:Serial subtitle}}
<var>Serial</var> converts an <var>XmlDoc</var> subtree to the UTF-8 or EBCDIC text string representation of the subtree. This process is called '''serialization''', because the text representation of a document is called the '''serial''' form.


This function converts an <var>XmlDoc</var> subtree to the UTF-8 or EBCDIC
text string representation of the subtree.
(This process is called '''serialization''',
because the text representation of a document is called the '''serial'''
form.)
==Syntax==
==Syntax==
{{Template:XmlDoc/XmlNode:Serial syntax}}
{{Template:XmlDoc/XmlNode:Serial syntax}}
===Syntax terms===
===Syntax terms===
<table class="syntaxTable">
<table class="syntaxTable">
<tr><th>%utfOrEbcd</th>
<tr><th>%string</th>
<td>A string variable for the string serialization of the subtree, encoded either in UTF-8 or, if the <tt>EBCDIC</tt> option (see below) is used, in EBCDIC. </td></tr>
<td>A string variable for the string serialization of the subtree, encoded either in UTF-8 or, if the <var>EBCDIC</var> option (see below) is used, in EBCDIC.</td></tr>
 
<tr><th>nr</th>
<tr><th>nr</th>
<td>An <var>XmlDoc</var> or <var>XmlNode</var>, used as the context node for the <i>XPath</i> expression. If an <var>XmlDoc</var>, the Root node is the context node. </td></tr>
<td>An <var>XmlDoc</var> or <var>XmlNode</var>, used as the context node for the <var class="term">xpath</var> expression. If an <var>XmlDoc</var>, the <var>Root</var> node is the context node.</td></tr>
<tr><th>XPath</th>
<td>A <var>Unicode</var> string that is an XPath expression that results in a nodelist, the head of which is the top of the subtree to serialize. This is an optional argument; its default is a period (.), that is, the node referenced by the method object (<i>nr</i>).
<p class="code">Prior to ''Sirius Mods'' version 7.6, this is an EBCDIC string. </td></tr>
</p>
<tr><th>options</th>
<td>A blank delimited string that can contain one or more of the following: <ul> <li><b>EBCDIC</b>
<p class="code">This indicates that the serialization should be in EBCDIC rather than UTF-8. UTF-8 encoding is provided by default.
Selecting <tt>EBCDIC</tt> under ''Sirius Mods'' 7.6 or higher causes conversion via the <var>Unicode</var> tables of the subtree content, which is stored in <var>Unicode</var>. For more information about the <var>Unicode</var> tables, see [[??]] refid=u80..
<var>Serial</var>izing to UTF-8 involves no translation: the stored <var>Unicode</var> characters are merely encoded as UTF-8. <li><b>ExclCanonical</b>
This indicates that the output of the serialization will be in exclusive XML canonical form, as defined in the W3C "Exclusive XML Canonicalization" specification (http://www.w3.org/tr/xml-exc-c14n) , which is an extension of the "XML Canonicalization" specification (http://www.w3.org/TR/xml-c14n) . These specifications constrain serializations to facilitate processing such as digital signatures.
This option, added in ''Sirius Mods'' version 7.0, is described in greater detail in the list item for [[??]] refid=canotes reftxt=Canonicalization. in the "Usage Notes" section, below.
Specifying any of the <var>Serial</var> method CR, LF, CRLF, or Indent options when you also specify ExclCanonical is allowed. Although the resulting output will not be completely canonical, it may be what you require for the purposes of a digital signature, for example. The formatting addressed by those options is defined in the Exclusive Canonicalization specification and covered by the ExclCanonical option.
Similarly, the effect of the XmlDecl option contradicts the Exclusive Canonicalization specification. If you do specify the XmlDecl and ExclCanonical options together, however, the serialized XML Declaration is followed by a linefeed character. <br> <li><b>Indent</b> <i><b>n</b></i>
Inserts space characters (and line-ends, as described for the next option) into the serialized string such that if the string is broken at the line-ends and displayed as a tree, the display of each lower level in the subtree is indented ''n'' spaces from the previous level's starting point.
If serialized output with an Indent value of 2 is displayed as a tree, the spacing is as in the following: <pre>    <top>      <leaf1 xx="yy">value</leaf1>      <sub>        <leaf2>value</leaf2>      </sub>    </top> </pre>
One of the line-end options, below, must also be specified.
<i>n</i> is a non-negative integer, and its maximum value (as of ''Sirius Mods'' version 7.0) is 254. <li>One of the '''line-end options''' below, to provide line breaks in the output after any of the following is serialized: <ul> <li>An element start-tag, if it has any non-text node children <li>An element end tag <li>An empty element tag <li>A processing instruction (PI) <li>A comment <li>A text node, if it has any siblings </ul>
</p>
<table class="syntaxNested">
<tr><th>CR</th>
<td>Insert a carriage-return character as the line-end sequence in the above cases. </td></tr>
<tr><th>LF</th>
<td>Insert a linefeed character as the line-end sequence in the above cases. </td></tr>
<tr><th>CRLF</th>
<td>Insert a carriage-return character followed by a linefeed character as the line-end sequence in the above cases. </td></tr>
</table>
'''Note:''' If one of these line-end options is specified and an <tt>AddTrailingDelimiter=false</tt> argument is also specified, no line-end character is added at the end of the serialized subtree. <br> <li><b>NoEmptyElt</b>
<p class="code">Deprecated as of ''Sirius Mods'' version 7.0, this option ensures that all empty elements are serialized with a start tag followed by an end tag. For example: <pre>    <middleName></middleName> </pre>
If NoEmptyElt is not specified, the default is to serialize an empty element with an empty element tag; using the same example as above, this would be: <pre>    <middleName/> </pre>
The ExclCanonical option provides the same empty element serialization as NoEmptyElement. <li><b>OmitNullElement</b>
An Element node that has no children and no Attributes will not be serialized, unless it is the top level Element in the subtree being serialized. The serialization of a child-less and Attribute-less Element is omitted, even if the Element's serialization would contain Namespace declarations in its start tag.
If an Element node has no Attributes, but has (only) Element children (one or more), and all of its children are Attribute-less and child-less, then that parent Element is serialized, even though its content in the serialization is empty. That parent is serialized with a start tag and an end tag (and an inserted line separator, if called for by the serializing method's parameter options).
For example, if the <var>Serial</var> method display of a particular <var>XmlDoc</var> in tree format is the following when OmitNullElement is ''not'' specified: <pre>    <top>        <middle>          <empty/>          <p:empty2 xmlns:p="uri:stuff"/>        </middle>    </top> </pre>
Here is the display of the <var>XmlDoc</var> with the OmitNullElement option specified: <pre>    <top>        <middle>        </middle>    </top> </pre>
But if you attempt to display only the <tt>empty</tt> subtree of %d using OmitNullElement, the <tt>empty</tt> node is not suppressed, and the result is: <pre>    <empty/> </pre>
The OmitNullElement option is available as of ''Sirius Mods'' version 7.3. <li><b>SortCanonical</b>
Deprecated as of ''Sirius Mods'' version 7.0, SortCanonical serializes namespace declarations (based on the prefix being declared) and attributes (based on the namespace URI followed by the local name) in sorted order. This can be useful, for instance, when using <var>Serial</var> to serialize a portion of an XML document for a signature.
The sort order for namespace declarations and attributes is from lowest to highest, and it uses the <var>Unicode</var> code ordering (for example, numbers are lower than letters).
Added in ''Sirius Mods'' version 6.9, this option is superseded by the ExclCanonical option. <br> :noteh <li><b>WithComments</b>
This indicates that all Comment nodes in the specified subtree are to be included in the serialized output. '''Note:''' This option, added in ''Sirius Mods'' version 7.0, is only a supplement to the ExclCanonical option: specifying WithComments without specifying ExclCanonical has no effect. Specifying ExclCanonical without specifying WithComments causes all Comment nodes to be suppressed from the result. <li><b>XmlDecl</b>
This indicates that the serialized <var>XmlDoc</var> will contain the "XML Declaration" (<?xml version=...?>), if the value of the Version property (??[[Version (XmlDoc property)|Version]]) is a non-null string, and if the <var>XmlDoc</var> is not empty.
XmlDecl may only be specified if the top of the subtree being serialized is the Root node.
The XmlDecl option is new in ''Sirius Mods'' version 6.7. </ul> </td></tr>
</p>
<tr><th>AddTrailingDelimiter=<i><b>bool</b></i></th>
<td>This Boolean, name required parameter determines whether a final line-end character is added to the serialization when one of the <var>Serial</var> method line-end options (<tt>LF</tt>, <tt>CR</tt>, or <tt>CRLF</tt>) is specified.
<p class="code">The default value of AddTrailingDelimiter is <tt>True</tt>, so <var>Serial</var> specified with a line-end option adds a trailing line-end by default. If AddTrailingDelimiter is specified as <tt>False</tt>, no final line-end character is added.
Specifying the AddTrailingDelimiter argument without also specifying one of the line-end options has no effect on the resulting serialization.
AddTrailingDelimiter is new as of ''Sirius Mods'' version 7.0. It may be useful if a digital signature must be created which includes line-end characters between XML tags, but the <var>XmlDoc</var> does not contain those line-end Text nodes.</td></tr>
</p>
</table>


==Usage notes==
<tr><th>xpath</th>
<ul>
<td>A <var>Unicode</var> string that is an [[XPath#XPath_syntax|Xpath expression]] that results in a nodelist, the head of which is the top of the subtree to serialize. Any other nodes in the nodelist are ignored.
<li>To obtain a <var>Longstring</var> that is the UTF-8
<p>
serialization of an entire <var>XmlDoc</var>, including the "XML declaration,"
This is an optional argument; its default is a period (<tt>.</tt>), that is, the node referenced by the method object(<var class="term">nr</var>). </p>
use ??[[Xml (XmlDoc function)|Xml]].
<p>
<li>The ''options'' argument values may be specified in any case.
Prior to <var class="product">[[Sirius Mods]]</var> Version 7.6, this is an EBCDIC string.</p></td></tr>
For example,
<tt>XmlDecl</tt> and <tt>xmldecl</tt> are interchangeable.
<li>Line-end/whitespace characters:
<ul>
<li>Using one of the line-end character options (CR, LF, CRLF) produces output
that is similar to the BothCompact option of the Print method
(??[[Print (XmlDoc/XmlNode subroutine)|Print]]).
<li>If one of the line-end
(<tt>CR</tt>, <tt>LF</tt>, <tt>CRLF</tt>) options
is specified, and an element to be serialized has the
<tt>xml:space="preserve"</tt> attribute, then
within the serialization of that element and its descendants, no line-end
characters are inserted.


In addition, the <tt>xml:space="default"</tt> attribute has no effect
<tr><th><div id="options"></div>options</th>
under these options:
<td>A blank delimited string that can contain one or more of the following options (but no repeats).  
specified by itself, it does not influence serialization, nor
<p class="note">'''Note:''' These options are described in greater detail in [[XmlDoc API serialization options]]. </p>
does it cause resumption of readability line-ends or indents if they were
suspended by a containing <tt>xml:space="preserve"</tt>.
<li>As of version 6.7, the <var>Serial</var> method uses the hexadecimal
character references specified in the XML Canonicalization specification
(:hp0 color=SirLink.http://www.w3.org/TR/xml-c14n:ehp0.)  to
display the following whitespace characters:
<ul>
<ul>
<li>For Attribute nodes: tab, carriage return (CR), and linefeed (LF)
<li><b>CharacterEncodeAll</b><br>
<li>For Text nodes: carriage return
If the <var>EBCDIC</var> option is specified, use character encoding in all contexts (that is, not only in <var>Attribute</var> or <var>Element</var> values) to display Unicode characters that do not translate to EBCDIC. This option is available starting with version 8.0 of the <var class="product">Sirius Mods</var>.
</ul>


Since the character references are not subject to the standard XML whitespace
<li><b>EBCDIC</b>
normalization ([[??]] refid=normwhi.),
<br>Produces serialized output in EBCDIC text rather than the default encoding, UTF-8.  
a serialized document (or subtree) that is then deserialized
will retain this whitespace.


These character references are used:
<li><b>ExclCanonical</b>
<dl>
<br>Produces serialized output in exclusive XML canonical form, as defined in the W3C "Exclusive XML Canonicalization" specification  (http://www.w3.org/tr/xml-exc-c14n).
<dt>tab
<dd>&amp;#x9;
<dt>CR
<dd>&amp;#xD;
<dt>LF
<dd>&amp;#xA;
</dl>


The EBCDIC and corresponding ASCII encodings of the characters is:
<li><b>Indent <i>n</i></b>
<dl>
<br>Inserts space characters and line-ends into the serialized string such that if the string is broken at the line-ends and displayed as a tree, the display of each lower level in the subtree is indented ''n'' spaces from the previous level's starting point. You must also specify <code>CR</code>, <code>LF</code>, or <code>CRLF</code> (see below). </li>
<dt>&thinsp.
<dd>EBCDIC    ASCII
<dt>tab
<dd>X'05'          X'09'
<dt>CR
<dd>X'0D'           X'0D'
<dt>LF
<dd>X'25'          X'0A'
</dl>
</ul>
<li>As of ''Sirius Mods'' version 7.6, Attribute values are always serialized
within double-quotation-mark (<tt>"</tt>) delimiters,
and a double-quotation mark character in an attribute value is serialized
as <tt>&amp;quot;</tt>.
Prior to version 7.6, this convention was not strictly observed.
<li>Canonicalization:


Canonicalization refers to
<li><b>CR</b> (carriage-return), <b>LF</b> (linefeed), or <b>CRLF</b> (carriage-return followed by a linefeed)
a particular serialization of an XML document that is
<br>Inserts one of these line-end characters to provide line breaks in the serialized output. </li>
unique, yet still a logically equivalent representation
of the document.
Exclusive canonicalization is canonicalization augmented by rules for
preserving or excluding the namespace context (declaration) of nodes when
''only a portion of an XML document'' is serialized.


Therefore, if a portion (subtree) of an XML document is exclusively
<div id="noempty"></div>
canonicalized, it is
<li><b>NoEmptyElt</b>
serialized uniquely and is "substantially independent of its XML context"
<br>This deprecated option serializes all empty elements with a start tag followed by an end tag. The default is to serialize an empty element with an empty element tag (as in <code><middleName/></code>).
(that is, contains all essential and no extraneous information from its
<p>
ancestor nodes).
<var>NoEmptyElt</var> is deprecated in order to deter users from using it to serialize HTML: The recommended approach for HTML is shown on the <var>[[NoEmptyElement (XmlNode property)#browserExample|NoEmptyElement]]</var> property page &mdash; some tags (<code>&#x3c;div></code>) <b>require</b> separate start and end tags, while other tags (<code>&#x3c;br></code>) <b>do not allow</b> separate start and end tags. </p></li>
This independence makes the subtree suitable for working with digital signatures.


Some of the many requirements for canonicalization are provided automatically
<li><b>OmitNullElement</b>
by specifying the <var>Serial</var> method with no options specified.
<br>An <var>Element</var> node that has no children and no <var>Attributes</var> will not be serialized, unless it is the top level <var>Element</var> in the subtree being serialized. </li>
For example, UTF-8 encoding and exclusion of the XML declaration, if any,
are provided by default by <var>Serial</var>.
Specifying <tt>ExclCanonical</tt>, which is new as of ''Sirius Mods'' version 7.0,
adds the following features to the no-option default:
<ul>
<li>Sorting of namespace declarations (based on the prefix
being declared) and of attributes (based on the namespace URI followed by the
local name).
The sort order is from lowest
to highest, and it uses the <var>Unicode</var> code ordering (for example, numbers
are lower than letters).
<li>For empty elements, serialization with both a start tag and an end tag,
instead of using a single "empty element tag."
<li>The suppression of any Comment nodes that may be present in the subtree.
Comment nodes are suppressed unless the <tt>WithComments</tt> option is
specified along with ExclCanonical.


For an example, see item [[??]] refid=namspx5..
<li><b>SortCanonical</b>
<li>Special namespace declaration handling: A namespace declaration is produced
<br>This deprecated option serializes namespace declarations and attributes in sorted order (from lowest to highest with Unicode code ordering).  It is superseded by the <var>ExclCanonical</var> option. </li>
only if it is utilized by an element or attribute in the subtree.
The declaration is produced in the
start-tag of an element that uses it (or has an attribute using
it), unless the parent of the element is in the subtree and the
declaration is in scope at the parent.


For examples, see items [[??]] refid=namspx1. and [[??]] refid=namspx2..
<li><b>UTF-8</b>
<li>''Attribute values'' are always serialized within
<br>Produces serialized output in UTF-8. This is the default. </li>
double-quotation-mark (<tt>"</tt>) delimiters,
and a double-quotation mark character in an attribute value is serialized
as <tt>&amp;quot;</tt>.


With or without the ExclCanonical option,
<li><b>WithComments</b>
these special characters in attribute values are serialized
<br>Includes in the serialized output all <var>Comment</var> nodes in the specified subtree.
as entity and hexadecimal character references:
<p class="note">'''Note:''' Specifying <var>WithComments</var> without specifying <var>ExclCanonical</var> has no effect. Specifying <var>ExclCanonical</var> without specifying <var>WithComments</var> suppresses all <var>Comment</var> nodes from the result.</p> </li>
<ul>
<li>The ampersand (&) is serialized as <tt>&amp;amp;</tt>
<li>The less-than symbol (<) is serialized as <tt>&amp;lt;</tt>
<li>The carriage return (CR) character is serialized as <tt>&amp;#xD;</tt>
<li>The linefeed (LF) character is serialized as <tt>&amp;#xA;</tt>
<li>The tab character is serialized as <tt>&amp;#x9;</tt>
</ul>


For examples, see item [[??]] refid=namspx6..
<li><b>XmlDecl</b>
 
<br>Ensures that the serialization will contain the "XML Declaration" (<code><?xml version=...?></code>), if the value of the <var>[[Version (XmlDoc property)|Version]]</var> property is a non-null string, if the <var>XmlDoc</var> is not empty, and if the top of the subtree being serialized is the <var>Root</var> node. </li>
<li>Within ''Text nodes'', the following characters are
serialized as entity and hexadecimal character references:
 
If you specify <var>Serial</var> with no options:
<ul>
<li>The less-than symbol (<) is serialized as <tt>&amp;lt;</tt>
<li>The ampersand (&) is serialized as <tt>&amp;amp;</tt>
<li>The carriage return (CR) character is serialized as <tt>&amp;#xD;</tt>
</ul>
</ul>
</td></tr>


If you specify the ExclCanonical option, the following is ''also'' true:
<tr><th><var>AddTrailingDelimiter</var></th>
<ul>
<td>This [[Methods#Named parameters|name required]] parameter is a <var>[[Enumerations#Using Boolean enumerations|Boolean]]</var> value, which determines whether a final line-end character is added to the serialization when one of the <var>Serial</var> method line-end options (<var>LF</var>, <var>CR</var>, or <var>CRLF</var>) is specified.
<li>The greater-than symbol (>) is serialized as <tt>&amp;gt;</tt>
<p>
</ul>
The default value of <var>AddTrailingDelimiter</var> is <var>True</var>, so <var>Serial</var> specified with a line-end option adds a trailing line-end by default. If <var>AddTrailingDelimiter</var> is <var>False</var>, no final line-end character is added.</p>
<p>
Specifying the <var>AddTrailingDelimiter</var> argument without also specifying one of the line-end options has no effect on the resulting serialization.</p>
<p>
<var>AddTrailingDelimiter</var> was introduced as of <var class="product">Sirius Mods</var> Version 7.0. It may be useful if a digital signature must be created which includes line-end characters between XML tags, but the <var>XmlDoc</var> does not contain those line-end <var>Text</var> nodes.</p></td></tr>
</table>


For examples, see item [[??]] refid=namspx6..
==Usage notes==
<li>If serializing the Root of an <var>XmlDoc</var>, a linefeed character
is inserted ''between'' the children of the Root.
This character is represented exactly
by <tt>X'25'</tt> if the <tt>EBCDIC</tt> option of <var>Serial</var> is used; otherwise
it is represented by <tt>X'0A'</tt>.
'''Note:'''
No linefeed is inserted if the <var>XmlDoc</var> has one PI or Comment node and
does not have an Element node.
In this case (which is allowed by [[Janus SOAP]]), the XML document is not well-formed
and therefore the canonicalization specifications ignore it.
<li>If the subtree to be serialized is a single node that is either of these:
<ul>
<ul>
<li>A PI child of the Root
<li>The <var class="term">options</var> argument values may be specified in any case. For example, <code>XmlDecl</code> and <code>xmldecl</code> are interchangeable.
<li>A single node that is a Comment child of the Root and
<li>As of <var class="product">Sirius Mods</var> Version 7.6, <var>Attribute</var> values are always serialized within double-quotation-mark (<tt>"</tt>) delimiters, and a double-quotation mark character in an <var>Attribute</var> value is serialized as <code>&amp;quot;</code>. Prior to <var class="product">Sirius Mods</var> Version 7.6, this convention was not strictly observed.
the <tt>WithComments</tt> option is specified
</ul>
</ul>


Then a linefeed character is added after the PI or Comment if
there is a following Element sibling, or is added before the PI or Comment
if there is a preceding Element sibling.
'''Note:'''
No linefeed is inserted if the <var>XmlDoc</var> does not have an Element node.
In this case (which is allowed by [[Janus SOAP]]), the XML document is not well-formed
and therefore the canonicalization specifications ignore it.
</ul>
Qualifications/exceptions:
<ul>
<li>The canonicalization specifications, especially
exclusive canonicalization, include references to the
serialization of a ''subset'' of a document.
The ExclCanonical option is based not on a subset but on a ''subtree''.
<li>Although the ExclCanonical and SortCanonical options use
the "<var>Unicode</var>" sort sequence,
this is currently limited to <var>Unicode</var> values less than 256 (as
of version &NUNCVSN. of [[Janus SOAP]]),
so it is accomplished with an 8-byte EBCDIC to 8-byte
<var>Unicode</var> table, which is (for all intents and purposes) merely an EBCDIC-to-ASCII
translation.
<li>The specifications support an argument to canonicalization that
is a list of namespace declarations that are to be "forced" into the
serialization.
The ExclCanonical option does not provide this support.
</ul>
A series of examples of the effects of the ExclCanonical option
begins with item [[??]] refid=namspx1..
</ul>
==Examples==
==Examples==
<ol>
<ol>
<li>In the following example, the <var>Serial</var> method EBCDIC
<li>In the following example, the <var>Serial</var> method EBCDIC formatting of a document is shown. A <var>Print</var> statement display of the default UTF-8 formatting of <var>Serial</var> is a string that is not readily decipherable.
formatting of a document is shown.
<p class="code">begin
A Print statement display of the default UTF-8 formatting of <var>Serial</var> is a
  %doc is object xmlDoc
string that is not readily decipherable.
  %sl is object [[Stringlist_class|stringlist]]
<p class="code">Begin
  %doc = new
%doc is <var>Object</var> <var>XmlDoc</var>
  %sl = new
%sl is <var>Object</var> <var>Stringlist</var>
%doc = New
%sl = New
 
text to %sl
<top>
<a>
  <b>05</b>
</a>
<c>
  <d att="val"/>
</c>
</p>
</top>
end text


Call %doc:LoadXml(%sl)
  text to %sl
Print '<var>Serial</var> method output follows:'
  <nowiki><top>
Print %doc:<var>Serial</var>('top', 'ebcdic')
      <a>
End
        <b>05</b>
      </a>
      <c>
        <d att="val"/>
      </c>
  </top></nowiki>
  end text
 
  [[Notation_conventions_for_methods#Callable_methods|Call]] %doc:[[LoadXml_(XmlDoc/XmlNode_function)|loadXml]](%sl)
  print 'Serial method output follows:'
  print %doc:serial('top', 'ebcdic')
end
</p>
</p>
The example results follow:
The example results follow:
<p class="code"><var>Serial</var> method output follows:
<p class="output">Serial method output follows:
<top><a><b>05</b></a><c><d att="val"/></c></top>
<nowiki><top><a><b>05</b></a><c><d att="val"/></c></top></nowiki>
</p>
</p>
<li>This and the remaining examples show various aspects of
<li>In the following fragment, the <var>Serial</var> method EBCDIC formatting of a document with untranslatable Unicode is shown.
the <tt>ExclCanonical</tt> option.
<p class="code">%doc2:[[AddElement_(XmlDoc/XmlNode_function)|addElement]]('circumference', -
The examples use the <tt>EBCDIC</tt> option to display
  '2 * &amp;#x3C0; * r':U)
the result.
print %doc2:serial(, 'ebcdic')
If using ExclCanonical for digital signature processing, you probably
should omit the EBCDIC option and use the default encoding, UTF-8.
 
Under exclusive canonicalization, a namespace is not serialized if it is not
necessary.
In this example, the subtree to be serialized is displayed in blue font
in the request code that follows:
<p class="code">Begin
%doc is <var>Object</var> <var>XmlDoc</var>
%doc = New
%l is longstring
%sl is object stringlist
%sl = New
text to %sl
<top>
:hp2 color=blue.<a xmlns:p3="urn:p3" xmlns:p2="urn:p2" xmlns:p1="urn:p1">
      <p1:b/>
      <p2:b/>
</a>'''
</p>
</p>
</top>
The result follows (the Unicode codepoint for the Greek letter &#x03c0; has the hexadecimal value 03C0):
end text
<p class="output"><circumference>2 * &amp;#x03C0; * r</circumference>
 
Call %doc:LoadXml(%sl)
Print 'Exclcan via ParseLines:'
%sl = New
%l=%doc:<var>Serial</var>('top/a', 'EBCDIC exclcanonical indent 2 lf')
%sl:Parselines(%l)
%sl:Print
End
</p>
 
The exclusive canonical serialization (displayed, after being parsed from string
to <var>Stringlist</var>, with line breaks and indent for the sake of clarity)
omits the declaration for <tt>p3</tt>,
because it is not utilized in the serialized subtree:
<p class="code"><a>
<p1:b xmlns:p1="urn:p1"></p1:b>
<p2:b xmlns:p2="urn:p2"></p2:b>
</a>
</p>
</p>
</ol>


An element '''utilizes''' an in-scope
==Request-cancellation errors==
namespace declaration in either of these cases:
This list is not exhaustive: it does <i>not</i> include all the errors that are request  cancelling.
<ul>
<ul>
<li>The element is prefixed and the declaration is of that prefix.
<li><var class="term">xpath</var> argument is invalid.
<li>The element is unprefixed and it is a default namespace declaration.
<li>Result of <var class="term">xpath</var> is empty.
<li>An <var class="term">options</var> setting is invalid.
<li>Insufficient free space exists in CCATEMP.
</ul>
</ul>


An attribute '''utilizes''' an in-scope
==See also==
namespace declaration if
<ul>
the attribute is prefixed and the declaration is of that prefix.
<li>The subroutine that serializes an <var>XmlDoc</var> and sends it as a Web response is <var>[[WebSend (XmlDoc subroutine)|WebSend]]</var>. </li>


In the preceding example, there was no alternative to removing the non-utilized
<li>Additional serializing methods include:
declaration for <tt>p3</tt>, but if it were utilized by a
<ul>
descendant element "lower" in the document tree, it would be
<li><var>[[Xml (XmlDoc function)|Xml]]</var>
moved to that element.
<li><var>[[Print (XmlDoc/XmlNode subroutine)|Print]]</var>
 
<li><var>[[AddXml (HttpRequest subroutine)|AddXml]]</var> (<var>[[HttpRequest class|HttpRequest]]</var> class)
Another application of the utilization rule is shown in the next example.
</ul> </li>
 
<li>Under exclusive canonicalization, namespaces are imported to where
they are needed.
 
Using the same type of request as in example [[??]] refid=namspx1 page=no. above,
the <tt>w</tt> element is the subtree to serialize (display form, blue font):
<p class="code"><a xmlns:p3="urn:p3" xmlns:p2="urn:p2" xmlns:p1="urn:p1">
:hp2 color=blue.
<w>
<p1:b/>
<p2:b/>
</p>
</w>
'''
</a>
</p>
 
Exclusive canonical serialization (display form), which gets required namespace
declarations from an ancestor of the serialized subtree:
<p class="code"><w>
<p1:b xmlns:p1="urn:p1"></p1:b>
<p2:b xmlns:p2="urn:p2"></p2:b>
</w>
</p>
<li>PIs and Comments
 
Using the same type of request as in example [[??]] refid=namspx1 page=no. above,
this is the subtree to be serialized (display form):
<p class="code"><a>
<!-- Comment 1 -->
<w>
  <?pi-without-data?>
</w>
</a>
</p>
 
Exclusive canonical serialization (display form),
which omits the Comment node:
<p class="code"><a>
<w>
  <?pi-without-data?>
</w>
</a>
</p>
'''Note:'''
To include the Comment node, specify also the <tt>WithComments</tt>
option of <var>Serial</var>.
 
<li>Character references
 
Using the same type of request as in example [[??]] refid=namspx1 page=no. above,
this is the subtree to serialize (display form):
<p class="code"><doc>
<comp>val>"0" val&amp;lt;"10"</comp>
<comp expr='val>"0"'></comp>
<norm attr=' &amp;apos;  &amp;#xD;&amp;#xA;&amp;#x9; &amp;apos; '/>
<white>&amp;#x9;&amp;#xD;&amp;#xA;</white>
</doc>
</p>


This is the result from <var>Serial</var> method ''with no options'' specified
<li>See the description of [[XmlDoc_API_serialization_options#EBCDIC_serialization_of_untranslatable_Unicode_characters|Unicode to EBCDIC conversion]] performed by <var>Serial</var> with the <code>EBCDIC</code> option. </li>
(display form, and the <tt><white></tt> element has a line that wraps
to emphasize the non-visible linefeed character it contains):
<p class="code"><doc>
<comp>val>"0" val&amp;lt;"10"</comp>
<comp expr='val>"0"'></comp>
<norm attr=" '  &amp;#xD;&amp;#xA;&amp;#x9; ' "/>
<white> &amp;#xD;
</p>
</white>
</doc>
</p>


The exclusive canonical serialization follows (display form,
<li>To deserialize a string, use <var>[[LoadXml (XmlDoc/XmlNode function)|LoadXml]]</var> or <var>[[WebReceive (XmlDoc function)|WebReceive]]</var>. </li>
wrapped <tt><white></tt> element line has no indent).
<p class="code"><doc>
<comp>val:hp2 color=blue.&amp;gt;'''"0" val&amp;lt;"10"</comp>
<comp expr=:hp2 color=blue."'''val>:hp2 color=blue.&amp;quot;'''0:hp2 color=blue.&amp;quot;'''"></comp>
<norm attr=" '  &amp;#xD;&amp;#xA;&amp;#x9; ' ">:hp2 color=blue.</norm>'''
<white> &amp;#xD;
</p>
</white>
</doc>
</p>


The differences from no-option <var>Serial</var> (blue font) include:
<li>For more information about using XPath expressions, see [[XPath]]. </li>
<ul>
<li>The greater-than symbol (>) within a text node is serialized
as <tt>&amp;gt;</tt>.
<li>Attribute values are enclosed in double-quotation marks (<tt>"</tt>).
<li>A double-quotation mark in an attribute value is serialized
as <tt>&amp;quot;</tt>.
<li>An empty element is serialized with two tags (a start tag
followed by an end tag), not with a single empty-element tag.
</ul>
</ol>


===Request-Cancellation Errors===
<li>For additional discussion about serialization, see [[XmlDoc API#Transport: receiving and sending XML|Transport: receiving and sending XML]].</li>
<ul>
<li><i>XPath</i> is invalid.
<li>Result of <i>XPath</i> is empty.
<li><i>Options</i> are invalid.
<li>Insufficient free space exists in CCATEMP.
</ul>
</ul>
==See also==
{{Template:XmlDoc/XmlNode:Serial footer}}
{{Template:XmlDoc/XmlNode:Serial footer}}
<ul>
<li>The subroutine that serializes an <var>XmlDoc</var> and sends it as a
Web response is ??[[WebSend (XmlDoc subroutine)|WebSend]], described below.
<li>Additional serializing methods include:
<ul>
<li>[[Xml (XmlDoc function)|Xml]]
<li>[[Print (XmlDoc/XmlNode subroutine)|Print]]
<li>AddXml (HttpRequest class, described in the [[Janus Sockets]]R.)
</ul>
<li>To deserialize a string,
use ??[[LoadXml (XmlDoc/XmlNode function)|LoadXml]]
or ??[[WebReceive (XmlDoc function)|WebReceive]].
<li>For more information about using XPath expressions, see [[XPath]].
</ul>

Latest revision as of 17:58, 18 February 2015

Serialize selected subtree as string (XmlDoc and XmlNode classes)

Serial converts an XmlDoc subtree to the UTF-8 or EBCDIC text string representation of the subtree. This process is called serialization, because the text representation of a document is called the serial form.

Syntax

%string = nr:Serial[( [xpath], [options], [AddTrailingDelimiter= boolean])] Throws XPathError

Syntax terms

%string A string variable for the string serialization of the subtree, encoded either in UTF-8 or, if the EBCDIC option (see below) is used, in EBCDIC.
nr An XmlDoc or XmlNode, used as the context node for the xpath expression. If an XmlDoc, the Root node is the context node.
xpath A Unicode string that is an Xpath expression that results in a nodelist, the head of which is the top of the subtree to serialize. Any other nodes in the nodelist are ignored.

This is an optional argument; its default is a period (.), that is, the node referenced by the method object(nr).

Prior to Sirius Mods Version 7.6, this is an EBCDIC string.

options
A blank delimited string that can contain one or more of the following options (but no repeats).

Note: These options are described in greater detail in XmlDoc API serialization options.

  • CharacterEncodeAll
    If the EBCDIC option is specified, use character encoding in all contexts (that is, not only in Attribute or Element values) to display Unicode characters that do not translate to EBCDIC. This option is available starting with version 8.0 of the Sirius Mods.
  • EBCDIC
    Produces serialized output in EBCDIC text rather than the default encoding, UTF-8.
  • ExclCanonical
    Produces serialized output in exclusive XML canonical form, as defined in the W3C "Exclusive XML Canonicalization" specification (http://www.w3.org/tr/xml-exc-c14n).
  • Indent n
    Inserts space characters and line-ends into the serialized string such that if the string is broken at the line-ends and displayed as a tree, the display of each lower level in the subtree is indented n spaces from the previous level's starting point. You must also specify CR, LF, or CRLF (see below).
  • CR (carriage-return), LF (linefeed), or CRLF (carriage-return followed by a linefeed)
    Inserts one of these line-end characters to provide line breaks in the serialized output.
  • NoEmptyElt
    This deprecated option serializes all empty elements with a start tag followed by an end tag. The default is to serialize an empty element with an empty element tag (as in <middleName/>).

    NoEmptyElt is deprecated in order to deter users from using it to serialize HTML: The recommended approach for HTML is shown on the NoEmptyElement property page — some tags (<div>) require separate start and end tags, while other tags (<br>) do not allow separate start and end tags.

  • OmitNullElement
    An Element node that has no children and no Attributes will not be serialized, unless it is the top level Element in the subtree being serialized.
  • SortCanonical
    This deprecated option serializes namespace declarations and attributes in sorted order (from lowest to highest with Unicode code ordering). It is superseded by the ExclCanonical option.
  • UTF-8
    Produces serialized output in UTF-8. This is the default.
  • WithComments
    Includes in the serialized output all Comment nodes in the specified subtree.

    Note: Specifying WithComments without specifying ExclCanonical has no effect. Specifying ExclCanonical without specifying WithComments suppresses all Comment nodes from the result.

  • XmlDecl
    Ensures that the serialization will contain the "XML Declaration" (<?xml version=...?>), if the value of the Version property is a non-null string, if the XmlDoc is not empty, and if the top of the subtree being serialized is the Root node.
AddTrailingDelimiter This name required parameter is a Boolean value, which determines whether a final line-end character is added to the serialization when one of the Serial method line-end options (LF, CR, or CRLF) is specified.

The default value of AddTrailingDelimiter is True, so Serial specified with a line-end option adds a trailing line-end by default. If AddTrailingDelimiter is False, no final line-end character is added.

Specifying the AddTrailingDelimiter argument without also specifying one of the line-end options has no effect on the resulting serialization.

AddTrailingDelimiter was introduced as of Sirius Mods Version 7.0. It may be useful if a digital signature must be created which includes line-end characters between XML tags, but the XmlDoc does not contain those line-end Text nodes.

Usage notes

  • The options argument values may be specified in any case. For example, XmlDecl and xmldecl are interchangeable.
  • As of Sirius Mods Version 7.6, Attribute values are always serialized within double-quotation-mark (") delimiters, and a double-quotation mark character in an Attribute value is serialized as &quot;. Prior to Sirius Mods Version 7.6, this convention was not strictly observed.

Examples

  1. In the following example, the Serial method EBCDIC formatting of a document is shown. A Print statement display of the default UTF-8 formatting of Serial is a string that is not readily decipherable.

    begin %doc is object xmlDoc %sl is object stringlist %doc = new %sl = new text to %sl <top> <a> <b>05</b> </a> <c> <d att="val"/> </c> </top> end text Call %doc:loadXml(%sl) print 'Serial method output follows:' print %doc:serial('top', 'ebcdic') end

    The example results follow:

    Serial method output follows: <top><a><b>05</b></a><c><d att="val"/></c></top>

  2. In the following fragment, the Serial method EBCDIC formatting of a document with untranslatable Unicode is shown.

    %doc2:addElement('circumference', - '2 * &#x3C0; * r':U) print %doc2:serial(, 'ebcdic')

    The result follows (the Unicode codepoint for the Greek letter π has the hexadecimal value 03C0):

    <circumference>2 * &#x03C0; * r</circumference>

Request-cancellation errors

This list is not exhaustive: it does not include all the errors that are request cancelling.

  • xpath argument is invalid.
  • Result of xpath is empty.
  • An options setting is invalid.
  • Insufficient free space exists in CCATEMP.

See also