Print (XmlDoc/XmlNode subroutine)

From m204wiki
Revision as of 00:33, 7 December 2010 by Admin (talk | contribs) (1 revision)
Jump to navigation Jump to search

Print selected subtree

Print is a member of the XmlDoc and XmlNode classes.

This subroutine displays an XmlDoc subtree in a readable form, useful for debugging, for example.

Syntax

  Call nr:Print([Xpath], [options])

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.
  • AllowXmlDecl or NoXmlDecl Whether or not the XML declaration (<?xml&thinsp.version=...?>) is produced — if the XML version number (as returned by the ??Version) is not the null string, and the node selected by XPath is the Root. AllowXmlDecl is the default. AllowXmlDecl and NoXmlDecl may not both be specified.
  • CharacterEncodeAll 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. 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.
  • Indent n Element children (and, depending on the compaction option, below, in effect, attributes and the closing characters “/>” of an empty Element) are indented n spaces from the beginning of the Element's Start-Tag. If the Indent option is omitted, the default indent is 3 spaces. n is non-negative, and its maximum (as of Sirius Mods version 7.0) is 254.
  • NoEmptyElt An empty element is serialized with a start tag followed by an end tag. For example:
        <middleName>
        </middleName>
    

    If NoEmptyElt is not specified, the default is to serialize an empty element with an empty element tag:

        <middleName/>
    
  • 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. 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 Print method display of a particular XmlDoc is the following when no Print options are specified:
        <top>
           <middle>
              <empty/>
              <p:empty2 xmlns:p="uri:stuff"/>
           </middle>
        </top>
    

    Here is the display of the XmlDoc with the OmitNullElement option specified:

        <top>
           <middle>
           </middle>
        </top>
    

    But if you attempt to display only the empty subtree of the XmlDoc using OmitNullElement, the empty node is not suppressed, and the result is:

        <empty/>
    

    The OmitNullElement option is available as of Sirius Mods version 7.3.

  • One of the following mutually exclusive output formats:
    • Compact 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:
          <top>
             <in1 a="xyz" b="foo">
               content1
             </in1>
             <in2>content2</in2>
          </top>
      
    • Expanded A new line is started for each attribute, namespace declaration, and child. For example:
          <top>
             <in1
               a="xyz"
               b="foo"
             >
               content1
             </in1>
             <in2>
               content2
             </in2>
          </top>
      
    • AttributeCompact Attributes and namespace declarations are printed on the same line as the start tag. For example:
          <top>
             <in1 a="xyz" b="foo">
               content1
             </in1>
             <in2>
                content2
             </in2>
          </top>
      
    • ElementCompact An entire element is printed on one line, if it has no attributes nor namespace declarations and has no children other than possibly a Text child. For example:
          <top>
             <in1
                a="xyz"
                b="foo"
                >
               content1
             </in1>
             <in2>content2</in2>
          </top>
      
    • BothCompact 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:
          <top>
             <in1 a="xyz" b="foo">content1</in1>
             <in2>content2</in2>
          </top>
      
  • SortCanonical 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). 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.

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
    &#x9;
    CR
    &#xD;
    LF
    &#xA;

    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 &quot;. 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', '&#x2122;':U)
        %doc:Print
    

    The result of this fragment is:

        <top>&#x2122;</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('&#x2122;':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('&#x2122;':U)
        %doc:Print(, 'CharacterEncodeAll')
    

    The result of the above fragment is:

        <&#x2122;/>
    

    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><b>05</b></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>
          <b>05</b>
       </a>
       <a2/>
    </top>
    Non-default display:
    <top>
          <a>
                <b>
                      05
                </b>
          </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 Xml function (??Xml). 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: