AddElement (XmlDoc/XmlNode function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
mNo edit summary
 
mNo edit summary
Line 252: Line 252:
Note that as of
Note that as of
''Sirius Mods'' version 7.6, this check can no longer be bypassed using the InvalidChar
''Sirius Mods'' version 7.6, this check can no longer be bypassed using the InvalidChar
method — see [[InvalidChar (XmlDoc property)#Usage Notes|Usage Notes for the InvalidChar property]].
method — see [[InvalidChar (XmlDoc property)#Usage notes|Usage Notes for the InvalidChar property]].
<li><i>URI</i> is invalid.
<li><i>URI</i> is invalid.
<li><i>URI</i> is present, but the
<li><i>URI</i> is present, but the

Revision as of 21:22, 16 February 2011

Add Element to XmlDoc Root or to Element XmlNode (XmlDoc and XmlNode classes)

[Requires Janus SOAP]


This callable function adds an Element node as the last child of the Root node or Element node referenced by the method object. AddElement also lets you add a Text node as the child of the added Element, and lets you specify the namespace URI for the added element.

Syntax

[%nod =] nr:AddElement( name, [value], [uri])

Syntax terms

%nod If specified, an XmlNode that is set to point to the Element node that is added.
nr An XmlDoc or XmlNode that refers to the Root or Element node parent of the added Element.
name The name of the added Element; if prefixed, the prefix must be declared, or a uri argument must be provided.

Multiple Elements with the same name may be added to a single parent Element.

The name must conform to the XML syntax rules for an element name; the maximum length of each of the prefix part and local name part is 127 characters (100 characters prior to version 7.7 of Janus SOAP).
value This optional argument is the content of a Text node that is added as a child of the added element. This content is stored without any normalization, entity substitution, etc. Since Text nodes cannot contain the null string, this argument is ignored if it is the null string.
uri This optional argument is the URI of the added element's namespace, or it is an explicit disabling of any namespace association for the element. See "Usage Notes," below.

Usage notes

  • If a URI argument is specified, the XmlDoc's Namespace property must be On, and URI must either be the null string, or it must have the format of an absolute URI. The effect depends on whether the name value includes a prefix:
    • If name has a prefix, URI must not be the null string, and URI is the declared namespace URI associated with that prefix and added element.
    • If name does not have a prefix:
      • If URI is the null string, the added element does not have a default namespace. Any in-scope default namespace the added element might have inherited, is not inherited.
      • If URI is not the null string, the added element has URI as the default namespace.

    If a non-null string URI argument is provided, and the namespace declaration resulting from it is not in scope for the added element, then a namespace declaration is added at that element.

  • If the URI argument is not present, the added element's namespace is determined by the prefix and in-scope declarations:
    • If the name is prefixed, the namespace is given by the in-scope declaration of the prefix.
    • If the name has no prefix, the namespace is the default namespace, if any, in scope. If there is no default namespace in scope, the added element has no namespace.
  • As of Sirius Mods version 7.3, the AddElement arguments may include only non-null EBCDIC characters that translate to Unicode. As of Sirius Mods version 7.6, these argument strings are Unicode or are converted to Unicode. For more information about the effects of this version 7.6 change, see Strings and Unicode. For a Unicode-specific Add Element example, see item ?? refid=addunic..
  • When the return value of AddElement is not needed, you may want to Call it instead of saving its return value; the return value is typically needed only to add attributes or non-Text children of the Element.
  • Processing of an XmlDoc is likely to be more efficient if you add nodes in document order (that is, top-to-bottom, left-to-right).

Examples

  1. In the following example, an Element node is added as the last child (that is, located after element "b") of element "a":

    Begin %doc is Object XmlDoc %doc = New %doc:LoadXml('<top><a><b>05</b></a></top>') %n1 is Object XmlNode %n1 = %doc:SelectSingleNode('top/a') %n1:AddElement('c', '505') %doc:Print('top') End

    The example result follows:

    <top> <a> <b>05</b> <c>505</c> </a> </top>

  2. The following statements add an Element node with a Text child and then add an Attribute node to the Element:

    %n1 = %n1:AddElement('weight', '10') %n1:AddAttribute('unit', 'gram')

  3. For a Sirius Mods version 7.6 or higher request, the following example shows techniques for adding an Element node that contains a Unicode "copyright" character, and it also includes a statement to add a "trademark" character. The Add Element statements use a hex character reference or an entity reference to represent the copyright character, and they use a character reference for the trademark. Those references must first be decoded so Add Element will interpret the value string as a single character and not as several individual characters.
    • The first AddElement invocation uses the EbcdicToUnicode function (?? refid=fue2u.) and parameter setting that decode and convert a character reference to Unicode.
    • The second AddElement invocation uses the U function (?? refid=umeth.), which is a compile-time equivalent of EbcdicToUnicode(CharacterDecode=True).
    • The third and fourth AddElement invocations show what happens when character decoding is not specified; the fourth also illustrates that EBCDIC to Unicode conversion is automatically performed as needed. The value string is converted character by character, and the ampersand (&) is translated on output to a character reference (&amp;).
    • The fifth AddElement invocation uses the U function to decode and convert to Unicode the entity reference (?? refid=entrefs.) for the copyright character.
    • The sixth AddElement invocation uses the U function to decode and convert to Unicode the character reference for the trademark character. Since this character does not translate to an EBCDIC character, the Print method displays its XML hexadecimal character reference.

    Begin %doc is Object XmlDoc Auto New %doc:LoadXml('<top/>') %n is Object XmlNode %n = %doc:SelectSingleNode('top') %n:AddElement('a1', - '&#xA9;':EbcdicToUnicode(CharacterDecode=True)) %n:AddElement('a2', '&#xA9;':U) %n:AddElement('a3', '&#xA9;':EbcdicToUnicode %n:AddElement('a4', '&#xA9;') %n:AddElement('a5', '&copy;':U) %n:AddElement('a6', '&#x2122;':U) %doc:Print End

    The example result follows:

    <top> <a1>&copy.</a1> <a2>&copy.</a2> <a3>&amp;#xA9;</a3> <a4>&amp;#xA9;</a4> <a5>&copy.</a5> <a6>&#x2122;</a6> </top>


  4. The following example contains several AddElement invocations and shows the effects of the URI argument:
    • An initial invocation of AddElement adds the first element of the document. The method object in this case is an XmlDoc. This invocation fails if the document already contains an element node.
    • A second AddElement invocation, this time with a node as the method object, adds an element as the child of the first element. This second element has a prefixed name but no URI value. Since the prefix matches that of the parent element, the second element receives the namespace URI of the first.
    • A third AddElement invocation adds an element (to the first). Although the AddElement arguments include no name prefix and no URI, the added element may still be associated with a namespace — the default namespace of its nearest ancestor. In this case, however, no default namespace is in scope (the parent's namespace is associated with its prefix), so this child has no namespace association at all.
    • A fourth AddElement invocation adds an element (to the first). The function arguments specify a non-prefixed name and also a URI value. This new URI is the added element's default namespace.
    • A fifth AddElement invocation adds an element to the element added by the previous AddElement. The function arguments include no name prefix and no URI value, so the added element receives the in-scope default namespace which, in this case, is also the URI of its parent.

    Begin %doc is Object XmlDoc %doc = New %n1 is Object XmlNode %n2 is Object XmlNode %n3 is Object XmlNode %n4 is Object XmlNode %n5 is Object XmlNode %n1 = %doc:AddElement('SOAP-ENV:Envelope',, - 'http://schemas.xmlsoap.org/soap/envelope/') %n2 = %n1:AddElement('SOAP-ENV:Stuff2') %n3 = %n1:AddElement('Stuff3') %n4 = %n1:AddElement('Stuff4', , - 'http://sirius-software.com') %n5 = %n4:AddElement('Stuff5') %doc:Print Print 'Stuff2 URI is ' %n2:URI Print 'Stuff3 URI is ' %n3:URI Print 'Stuff4 URI is ' %n4:URI Print 'Stuff5 URI is ' %n5:URI End

    The example result follows:

    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> <SOAP-ENV:Stuff2/> <Stuff3/> <Stuff4 xmlns="http://sirius-software.com"> <Stuff5/> </Stuff4> </SOAP-ENV:Envelope> Stuff2 URI is http://schemas.xmlsoap.org/soap/envelope/ Stuff3 URI is Stuff4 URI is http://sirius-software.com Stuff5 URI is http://sirius-software.com

Request-Cancellation Errors

  • Nr is neither the Root nor an Element node.
  • Nr is the Root node which already has an Element child.
  • Argument name violates the rules for an XML element name.
  • Name is prefixed, but the XmlDoc Namespace property is None.
  • Name prefixed and Namespace On, but URI missing and no declaration for prefix in scope.
  • Value violates the rules for an XML element value (that is, it contains an invalid character). Note that as of Sirius Mods version 7.6, this check can no longer be bypassed using the InvalidChar method — see Usage Notes for the InvalidChar property.
  • URI is invalid.
  • URI is present, but the Namespace property is not On, or, URI is the null string and name is prefixed.
  • Insufficient free space exists in CCATEMP.

See also

  • For more information about namespace declarations, see ?? refid=namsp..
  • For more information about URIs, see ?? refid=nsuri..
  • InsertElementBefore also adds an Element node. AddElement adds an Element as the last child of the node pointed to by the method object; InsertElementBefore adds an Element as the immediately preceding sibling of the node pointed to by the method object.
  • These methods are useful for working with namespaces: