Print (XmlDoc/XmlNode subroutine): Difference between revisions
m (1 revision) |
m (1 revision) |
||
Line 1: | Line 1: | ||
{{Template:XmlDoc/XmlNode:Print subtitle}} | {{Template:XmlDoc/XmlNode:Print subtitle}} | ||
This subroutine displays an XmlDoc subtree in a readable form, | This subroutine displays an <var>XmlDoc</var> subtree in a readable form, | ||
useful for debugging, for example. | useful for debugging, for example. | ||
==Syntax== | ==Syntax== | ||
Line 8: | Line 8: | ||
<table class="syntaxTable"> | <table class="syntaxTable"> | ||
<tr><th>nr</th> | <tr><th>nr</th> | ||
<td>An XmlDoc or XmlNode, used as the context node for the <i>XPath</i> expression. If an XmlDoc, 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 <i>XPath</i> expression. If an <var>XmlDoc</var>, the Root node is the context node. </td></tr> | ||
<tr><th>XPath</th> | <tr><th>XPath</th> | ||
<td>A Unicode string that is an XPath expression that results in a nodelist, the head of which is the top of the subtree to print. An optional argument, its default is a period (.), that is, the node referenced by the method object (<i>nr</i>). </td></tr> | <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 print. An optional argument, its default is a period (.), that is, the node referenced by the method object (<i>nr</i>). </td></tr> | ||
<tr><th>options</th> | <tr><th>options</th> | ||
<td>A blank delimited string that can contain one or more of the following options. No option may be repeated. <ul> <li><b>AllowXmlDecl</b> or <b>NoXmlDecl</b> | <td>A blank delimited string that can contain one or more of the following options. No option may be repeated. <ul> <li><b>AllowXmlDecl</b> or <b>NoXmlDecl</b> | ||
<p class="code">Whether or not the XML declaration (<?xml&thinsp.version=...?>) is produced — if the XML version number (as returned by the [[Version (XmlDoc property)|Version]] is not the null string, and the node selected by <i>XPath</i> is the Root. AllowXmlDecl is the default. AllowXmlDecl and NoXmlDecl may not both be specified. <li><b>CharacterEncodeAll</b> | <p class="code">Whether or not the XML declaration (<?xml&thinsp.version=...?>) is produced — if the XML version number (as returned by the [[Version (XmlDoc property)|Version]] is not the null string, and the node selected by <i>XPath</i> is the Root. AllowXmlDecl is the default. AllowXmlDecl and NoXmlDecl may not both be specified. <li><b>CharacterEncodeAll</b> | ||
Use character encoding in all contexts to display Unicode characters that do not translate to EBCDIC. If this option is not specified (as of ''Sirius Mods'' 7.6), only non-translatable Unicode characters in Attribute or Element values are displayed as character references. | Use character encoding in all contexts to display <var>Unicode</var> characters that do not translate to EBCDIC. If this option is not specified (as of ''Sirius Mods'' 7.6), only non-translatable <var>Unicode</var> characters in Attribute or Element values are displayed as character references. | ||
For more information about this option, see the "Usage Notes" item [[??]] reftxt=* refid=ucharef.. | For more information about this option, see the "Usage Notes" item [[??]] reftxt=* refid=ucharef.. | ||
The CharacterEncodeAll option is available as of ''Sirius Mods'' version 7.6. <li><b>Indent</b> <i><b>n</b></i> | The CharacterEncodeAll option is available as of ''Sirius Mods'' version 7.6. <li><b>Indent</b> <i><b>n</b></i> | ||
Line 22: | Line 22: | ||
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. | 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). | 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 Print method display of a particular XmlDoc is the following when no Print options are specified: <pre> <top> <middle> <empty/> <p:empty2 xmlns:p="uri:stuff"/> </middle> </top> </pre> | For example, if the <var>Print</var> method display of a particular <var>XmlDoc</var> is the following when no <var>Print</var> options are specified: <pre> <top> <middle> <empty/> <p:empty2 xmlns:p="uri:stuff"/> </middle> </top> </pre> | ||
Here is the display of the XmlDoc with the OmitNullElement option specified: <pre> <top> <middle> </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 the XmlDoc using OmitNullElement, the <tt>empty</tt> node is not suppressed, and the result is: <pre> <empty/> </pre> | But if you attempt to display only the <tt>empty</tt> subtree of the <var>XmlDoc</var> 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>One of the following mutually exclusive '''output formats''': <ul> <li><b>Compact</b> | The OmitNullElement option is available as of ''Sirius Mods'' version 7.3. <li>One of the following mutually exclusive '''output formats''': <ul> <li><b>Compact</b> | ||
This is the default. An element's entire start tag is printed on a single line, which includes attributes and namespace declarations. If it has no children or has a single Text child, and does '''not''' have attributes nor namespace declarations, then the Text child is serialized on the same line as the start and end tags. For example: <pre> <top> <in1 a="xyz" b="foo"> content1 </in1> <in2>content2</in2> </top> </pre> <li><b>Expanded</b> | This is the default. An element's entire start tag is printed on a single line, which includes attributes and namespace declarations. If it has no children or has a single Text child, and does '''not''' have attributes nor namespace declarations, then the Text child is serialized on the same line as the start and end tags. For example: <pre> <top> <in1 a="xyz" b="foo"> content1 </in1> <in2>content2</in2> </top> </pre> <li><b>Expanded</b> | ||
Line 32: | Line 32: | ||
The most compacted format, this combines the effect of AttributeCompact and ElementCompact. It displays on one line an element that has no children or that has a single Text child. For example: <pre> <top> <in1 a="xyz" b="foo">content1</in1> <in2>content2</in2> </top> </pre> </ul> <li><b>SortCanonical</b> | The most compacted format, this combines the effect of AttributeCompact and ElementCompact. It displays on one line an element that has no children or that has a single Text child. For example: <pre> <top> <in1 a="xyz" b="foo">content1</in1> <in2>content2</in2> </top> </pre> </ul> <li><b>SortCanonical</b> | ||
This indicates that namespace declarations (based on the prefix being declared) and attributes (based on the namespace URI followed by the local name) are serialized in sorted order. This can be useful, for instance, to serialize a portion of an XML document for a signature. | This indicates that namespace declarations (based on the prefix being declared) and attributes (based on the namespace URI followed by the local name) are serialized in sorted order. This can be useful, for instance, 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 Unicode code ordering (for example, numbers are lower than letters). | 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 as a step towards support for canonicalization, this option is superseded by the Serial method ExclCanonical option ([[??]] refid=exclcan.), which provides full support for canonicalized serialization. </ul></td></tr> | Added in ''Sirius Mods'' version 6.9 as a step towards support for canonicalization, this option is superseded by the Serial method ExclCanonical option ([[??]] refid=exclcan.), which provides full support for canonicalized serialization. </ul></td></tr> | ||
</p> | </p> | ||
Line 42: | Line 42: | ||
For example, you can use | For example, you can use | ||
either <tt>NoXmlDecl</tt> or <tt>noxmldecl</tt>, interchangeably. | either <tt>NoXmlDecl</tt> or <tt>noxmldecl</tt>, interchangeably. | ||
<li>As a debugging aide, Print may be issued for the root node of an | <li>As a debugging aide, <var>Print</var> may be issued for the root node of an | ||
XmlDoc that is not well-formed, that is, does not contain an Element. | <var>XmlDoc</var> that is not well-formed, that is, does not contain an Element. | ||
<li>The Print method output is '''not''' affected if one of the output | <li>The <var>Print</var> method output is '''not''' affected if one of the output | ||
format options or | format options or | ||
<tt>Indent</tt> is specified, and an element to be serialized has the | <tt>Indent</tt> is specified, and an element to be serialized has the | ||
<tt>xml:space="preserve"</tt> or <tt>xml:space="default"</tt> attribute. | <tt>xml:space="preserve"</tt> or <tt>xml:space="default"</tt> attribute. | ||
<li>As of version 6.7, the Print method uses the hexadecimal | <li>As of version 6.7, the <var>Print</var> method uses the hexadecimal | ||
character references specified in the XML Canonicalization specification | character references specified in the XML Canonicalization specification | ||
(:hp0 color=SirLink.http://www.w3.org/TR/xml-c14n:ehp0.) to | (:hp0 color=SirLink.http://www.w3.org/TR/xml-c14n:ehp0.) to | ||
Line 84: | Line 84: | ||
as <tt>&quot;</tt>. | as <tt>&quot;</tt>. | ||
Prior to version 7.6, this convention was not strictly observed. | Prior to version 7.6, this convention was not strictly observed. | ||
<li>As of ''Sirius Mods'' version 7.6, XmlDoc content is stored in Unicode. | <li>As of ''Sirius Mods'' version 7.6, <var>XmlDoc</var> content is stored in <var>Unicode</var>. | ||
To serialize to EBCDIC, the Print method uses the Unicode tables | To serialize to EBCDIC, the <var>Print</var> method uses the <var>Unicode</var> tables | ||
(described further in [[??]] refid=u80.) to convert the XmlDoc content. | (described further in [[??]] refid=u80.) to convert the <var>XmlDoc</var> content. | ||
One feature of the conversion from Unicode is that | One feature of the conversion from <var>Unicode</var> is that | ||
the Print method displays non-translatable Unicode characters | the <var>Print</var> method displays non-translatable <var>Unicode</var> characters | ||
stored in Attribute or Element values as character references. | stored in Attribute or Element values as character references. | ||
For example: | For example: | ||
<p class="code">%doc:AddElement('top', '&#x2122;':U) | <p class="code">%doc:AddElement('top', '&#x2122;':U) | ||
%doc:Print | %doc:<var>Print</var> | ||
</p> | </p> | ||
Line 101: | Line 101: | ||
However, with default serialization options, when an untranslatable | However, with default serialization options, when an untranslatable | ||
Unicode character occurs in a context other than Element or Attribute value | <var>Unicode</var> character occurs in a context other than Element or Attribute value | ||
(that is, a name, comment, or PI), character encoding is '''not''' used. | (that is, a name, comment, or PI), character encoding is '''not''' used. | ||
Because it is an element name, for example, the following statements result in a | Because it is an element name, for example, the following statements result in a | ||
request cancellation: | request cancellation: | ||
<p class="code">%doc:AddElement('&#x2122;':U) | <p class="code">%doc:AddElement('&#x2122;':U) | ||
%doc:Print | %doc:<var>Print</var> | ||
</p> | </p> | ||
The Print method fails, attempting | The <var>Print</var> method fails, attempting | ||
to translate the element name, the U+2122 character, to EBCDIC. | to translate the element name, the U+2122 character, to EBCDIC. | ||
This request cancellation can be prevented by using | This request cancellation can be prevented by using | ||
the <tt>CharacterEncodeAll</tt> option: | the <tt>CharacterEncodeAll</tt> option: | ||
<p class="code">%doc:AddElement('&#x2122;':U) | <p class="code">%doc:AddElement('&#x2122;':U) | ||
%doc:Print(, 'CharacterEncodeAll') | %doc:<var>Print</var>(, 'CharacterEncodeAll') | ||
</p> | </p> | ||
Line 121: | Line 121: | ||
</p> | </p> | ||
'''Note:''' | '''Note:''' | ||
The result of a Print with CharacterEncodeAll can be misleading. | The result of a <var>Print</var> with CharacterEncodeAll can be misleading. | ||
Request cancellation is avoided, but it produces multiple EBCDIC characters | Request cancellation is avoided, but it produces multiple EBCDIC characters | ||
where only a single Unicode character is stored. | where only a single <var>Unicode</var> character is stored. | ||
The XmlDoc, <tt>%doc</tt>, above is not a legal XML document, | The <var>XmlDoc</var>, <tt>%doc</tt>, above is not a legal XML document, | ||
because the ampersand (<tt>&</tt>) is not a legal name character. | because the ampersand (<tt>&</tt>) is not a legal name character. | ||
Similarly, for | Similarly, for | ||
an untranslatable Unicode character added to a document | an untranslatable <var>Unicode</var> character added to a document | ||
with AddComment or AddPI: printing with <tt>CharacterEncodeAll</tt> | with AddComment or AddPI: printing with <tt>CharacterEncodeAll</tt> | ||
produces a stream of characters that informs about | produces a stream of characters that informs about | ||
Line 138: | Line 138: | ||
===Example=== | ===Example=== | ||
In the following example, the Print method is called first | In the following example, the <var>Print</var> method is called first | ||
in its default form with no arguments, | in its default form with no arguments, | ||
then with explicit values for its options: | then with explicit values for its options: | ||
<p class="code">Begin | <p class="code">Begin | ||
Print 'Print method' | <var>Print</var> '<var>Print</var> method' | ||
Print '***********' | <var>Print</var> '***********' | ||
%doc is Object XmlDoc | %doc is <var>Object</var> <var>XmlDoc</var> | ||
%doc = New | %doc = New | ||
Call %doc:LoadXml('<top><a><b>05</b></a><a2/></top>') | Call %doc:LoadXml('<top><a><b>05</b></a><a2/></top>') | ||
%doc:Version ='1.0' | %doc:Version ='1.0' | ||
Call %doc:Print | Call %doc:<var>Print</var> | ||
Print 'Non-default display:' | <var>Print</var> 'Non-default display:' | ||
Call %doc:Print('/', 'NoXmlDecl NoEmptyElt Indent 6 Expanded') | Call %doc:<var>Print</var>('/', 'NoXmlDecl NoEmptyElt Indent 6 Expanded') | ||
End | End | ||
</p> | </p> | ||
Line 186: | Line 186: | ||
<ul> | <ul> | ||
<li>For more information about using XPath expressions, see [[XPath]]. | <li>For more information about using XPath expressions, see [[XPath]]. | ||
<li>To more accurately serialize an XmlDoc, as opposed to providing | <li>To more accurately serialize an <var>XmlDoc</var>, as opposed to providing | ||
a "displayable" form, use the [[Serial (XmlDoc/XmlNode function)|Serial]] function. | a "displayable" form, use the [[Serial (XmlDoc/XmlNode function)|Serial]] function. | ||
The Print subroutine, like its analogs [[??]] reftxt=Audit refid=audit. | The <var>Print</var> subroutine, like its analogs [[??]] reftxt=Audit refid=audit. | ||
and [[??]] reftxt=Trace refid=trace., | and [[??]] reftxt=Trace refid=trace., | ||
inserts line breaks and performs indentation. | inserts line breaks and performs indentation. |
Revision as of 17:46, 25 January 2011
Print selected subtree (XmlDoc and XmlNode classes)
This subroutine displays an XmlDoc subtree in a readable form,
useful for debugging, for example.
Syntax
nr:Print[( [xpath], [options])] Throws XPathError
Syntax terms
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 print. An optional argument, its default is a period (.), that is, the node referenced by the method object (nr). |
options | A blank delimited string that can contain one or more of the following options. No option may be repeated.
|
Usage notes
- Options may be specified in any case. For example, you can use either NoXmlDecl or noxmldecl, interchangeably.
- As a debugging aide, Print may be issued for the root node of an XmlDoc that is not well-formed, that is, does not contain an Element.
- The Print method output is not affected if one of the output format options or Indent is specified, and an element to be serialized has the xml:space="preserve" or xml:space="default" attribute.
- As of version 6.7, the Print 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 tab, carriage return, and linefeed characters
for the following XML document node types:
- Attribute
- Text
These character references are used:
- tab
- 	
- CR
- 
- LF
- 

The EBCDIC and corresponding ASCII encodings of the characters is:
- &thinsp.
- EBCDIC ASCII
- tab
- X'05' X'09'
- CR
- X'0D' X'0D'
- LF
- X'25' X'0A'
- 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 ". Prior to version 7.6, this convention was not strictly observed.
- As of Sirius Mods version 7.6, XmlDoc content is stored in Unicode.
To serialize to EBCDIC, the Print method uses the Unicode tables
(described further in ?? refid=u80.) to convert the XmlDoc content.
One feature of the conversion from Unicode is that
the Print method displays non-translatable Unicode characters
stored in Attribute or Element values as character references.
For example:
%doc:AddElement('top', '™':U) %doc:Print
The result of this fragment is:
<top>™</top>
However, with default serialization options, when an untranslatable Unicode character occurs in a context other than Element or Attribute value (that is, a name, comment, or PI), character encoding is not used. Because it is an element name, for example, the following statements result in a request cancellation:
%doc:AddElement('™':U) %doc:Print
The Print method fails, attempting to translate the element name, the U+2122 character, to EBCDIC. This request cancellation can be prevented by using the CharacterEncodeAll option:
%doc:AddElement('™':U) %doc:Print(, 'CharacterEncodeAll')
The result of the above fragment is:
<™/>
Note: The result of a Print with CharacterEncodeAll can be misleading. Request cancellation is avoided, but it produces multiple EBCDIC characters where only a single Unicode character is stored.
The XmlDoc, %doc, above is not a legal XML document, because the ampersand (&) is not a legal name character. Similarly, for an untranslatable Unicode character added to a document with AddComment or AddPI: printing with CharacterEncodeAll produces a stream of characters that informs about a single character reference but, if deserialized, would result in multiple stored characters. The standard XML syntax does not recognize character references as such in names, Comments, and PIs.
Example
In the following example, the Print method is called first in its default form with no arguments, then with explicit values for its options:
Begin Print 'Print method' Print '***********' %doc is Object XmlDoc %doc = New Call %doc:LoadXml('<top><a>05</a><a2/></top>') %doc:Version ='1.0' Call %doc:Print Print 'Non-default display:' Call %doc:Print('/', 'NoXmlDecl NoEmptyElt Indent 6 Expanded') End
The example results follow:
<?xml version="1.0"?> <top> <a> 05 </a> <a2/> </top> Non-default display: <top> <a> 05 </a> <a2> </a2> </top>
Request-Cancellation Errors
- XPath is invalid.
- Result of (XPath) is empty.
- Options are invalid.
- Insufficient free space exists in CCATEMP.
See also
- For more information about using XPath expressions, see XPath.
- To more accurately serialize an XmlDoc, as opposed to providing a "displayable" form, use the Serial function. The Print subroutine, like its analogs ?? reftxt=Audit refid=audit. and ?? reftxt=Trace refid=trace., inserts line breaks and performs indentation.
- Additional serializing methods include:
- WebSend
- AddXml (HttpRequest class, described in the Janus SocketsR.)