AddTopElement (XmlDoc function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
m (1 revision)
m (1 revision)
Line 3: Line 3:
This callable function
This callable function
adds an Element node as the new top-level element of
adds an Element node as the new top-level element of
the XmlDoc that is the object method.
the <var>XmlDoc</var> that is the object method.
This makes all the former
This makes all the former
children of the XmlDoc Root node become children of the new element.
children of the <var>XmlDoc</var> Root node become children of the new element.
AddTopElement also lets you
<var>AddTopElement</var> also lets you
specify the elementNamespace URI for the added element.
specify the elementNamespace URI for the added element.
==Syntax==
==Syntax==
Line 13: Line 13:
<table class="syntaxTable">
<table class="syntaxTable">
<tr><th>%nod</th>
<tr><th>%nod</th>
<td>If specified, an XmlNode that is set to point to the new top Element node that is added. </td></tr>
<td>If specified, an <var>XmlNode</var> that is set to point to the new top Element node that is added. </td></tr>
<tr><th>doc</th>
<tr><th>doc</th>
<td>An XmlDoc object variable. </td></tr>
<td>An <var>XmlDoc</var> object variable. </td></tr>
<tr><th>elementName</th>
<tr><th>elementName</th>
<td>The name (a Unicode string) to be given to the added Element. If the name is prefixed, a <i>URI</i> argument must be provided.
<td>The name (a <var>Unicode</var> string) to be given to the added Element. If the name is prefixed, a <i>URI</i> argument must be provided.
<p class="code">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]]). </td></tr>
<p class="code">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]]). </td></tr>
</p>
</p>
<tr><th>URI</th>
<tr><th>URI</th>
<td>This optional, Unicode string, argument defaults to the null string: <ul> <li>If ''elementName'' contains a prefix, then ''URI'' specifies the URI (Uniform Resource Identifier) associated with that prefix for the element, so it must be a non-null string that is a valid URI. <li>If ''elementName'' does not contain a prefix, ''URI'' must be omitted or must be the null string. This rule means that this URI argument is more restrictive than the URI argument of the [[AddElement (XmlDoc/XmlNode function)|AddElement]] method. </ul> </td></tr>
<td>This optional, <var>Unicode</var> string, argument defaults to the null string: <ul> <li>If ''elementName'' contains a prefix, then ''URI'' specifies the URI (Uniform Resource Identifier) associated with that prefix for the element, so it must be a non-null string that is a valid URI. <li>If ''elementName'' does not contain a prefix, ''URI'' must be omitted or must be the null string. This rule means that this URI argument is more restrictive than the URI argument of the [[AddElement (XmlDoc/XmlNode function)|AddElement]] method. </ul> </td></tr>
<tr><th>MoveNamespace=boolean</th>
<tr><th>MoveNamespace=boolean</th>
<td>This optional, name required argument is used to avoid a redundant namespace declaration on the old top element of the XmlDoc. If MoveNamespace=True and a URI argument (with a non-null string) is provided, then if the "old" top element contains a namespace declaration which is the same as the declaration inserted due to the URI argument, the declaration is deleted from the old top element.
<td>This optional, name required argument is used to avoid a redundant namespace declaration on the old top element of the <var>XmlDoc</var>. If MoveNamespace=True and a URI argument (with a non-null string) is provided, then if the "old" top element contains a namespace declaration which is the same as the declaration inserted due to the URI argument, the declaration is deleted from the old top element.
<p class="code">Note: You should use MoveNamespace=True only when some consumer of the XML document cares about not having the redundant declaration. This would be an unusual situation, because the information content of the document does not change. Using MoveNamespace=True might trigger a scan of the entire XmlDoc, which AddTopElement does not normally do.</td></tr>
<p class="code">Note: You should use MoveNamespace=True only when some consumer of the XML document cares about not having the redundant declaration. This would be an unusual situation, because the information content of the document does not change. Using MoveNamespace=True might trigger a scan of the entire <var>XmlDoc</var>, which <var>AddTopElement</var> does not normally do.</td></tr>
</p>
</p>
</table>
</table>
Line 30: Line 30:
==Usage notes==
==Usage notes==
<ul>
<ul>
<li>If a <i>URI</i> argument is specified, the XmlDoc's Namespace property
<li>If a <i>URI</i> argument is specified, the <var>XmlDoc</var>'s Namespace property
must be <tt>On</tt>.
must be <tt>On</tt>.
<li>When you need to change the structure of an XML document and
<li>When you need to change the structure of an XML document and
that need can be accomplished with AddTopElement, it is a
that need can be accomplished with <var>AddTopElement</var>, it is a
more efficient approach than using AddSubtree.
more efficient approach than using AddSubtree.
See the example in "Examples," below.
See the example in "Examples," below.
Line 43: Line 43:
wrappers" to it:
wrappers" to it:
<p class="code">Begin
<p class="code">Begin
%doc is Object XmlDoc
%doc is <var>Object</var> <var>XmlDoc</var>
%doc2 is Object XmlDoc
%doc2 is <var>Object</var> <var>XmlDoc</var>
%doc = New
%doc = New


Line 52: Line 52:


%doc2 = %doc:DeepCopy
%doc2 = %doc:DeepCopy
%doc2:AddTopElement('soap:Body', -
%doc2:<var>AddTopElement</var>('soap:Body', -
  'http://schemas.xmlsoap.org/soap/envelope/')
  'http://schemas.xmlsoap.org/soap/envelope/')
%doc2:AddTopElement('soap:Envelope', -
%doc2:<var>AddTopElement</var>('soap:Envelope', -
  'http://schemas.xmlsoap.org/soap/envelope/')
  'http://schemas.xmlsoap.org/soap/envelope/')


Line 82: Line 82:
In the example:
In the example:
<ul>
<ul>
<li>The order of the AddTopElement invocations is the
<li>The order of the <var>AddTopElement</var> invocations is the
reverse of the final order of the elements, that is, the second
reverse of the final order of the elements, that is, the second
invocation creates the first element in the final document.
invocation creates the first element in the final document.
<li>If you have no need to preserve the original
<li>If you have no need to preserve the original
XmlDoc, then both AddTopElement invocations
<var>XmlDoc</var>, then both <var>AddTopElement</var> invocations
can use <tt>%doc</tt> as the object method, and the DeepCopy can be omitted.
can use <tt>%doc</tt> as the object method, and the DeepCopy can be omitted.
<li>The redundant
<li>The redundant
Line 97: Line 97:
<li>Argument <i>elementName</i> violates the rules for an
<li>Argument <i>elementName</i> violates the rules for an
XML element name.
XML element name.
<li><i>elementName</i> is prefixed, but the XmlDoc
<li><i>elementName</i> is prefixed, but the <var>XmlDoc</var>
Namespace property is <tt>None</tt>.
Namespace property is <tt>None</tt>.
<li><i>elementName</i> is prefixed and Namespace
<li><i>elementName</i> is prefixed and Namespace

Revision as of 17:46, 25 January 2011

Add new top element to XmlDoc (XmlDoc class)

[Introduced in Sirius Mods 7.7; requires Janus SOAP]


This callable function adds an Element node as the new top-level element of the XmlDoc that is the object method. This makes all the former children of the XmlDoc Root node become children of the new element. AddTopElement also lets you specify the elementNamespace URI for the added element.

Syntax

[%nod =] doc:AddTopElement( name, [uri], [MoveNamespace= boolean])

Syntax terms

%nod If specified, an XmlNode that is set to point to the new top Element node that is added.
doc An XmlDoc object variable.
elementName The name (a Unicode string) to be given to the added Element. If the name is prefixed, a URI argument must be provided.

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).

URI This optional, Unicode string, argument defaults to the null string:
  • If elementName contains a prefix, then URI specifies the URI (Uniform Resource Identifier) associated with that prefix for the element, so it must be a non-null string that is a valid URI.
  • If elementName does not contain a prefix, URI must be omitted or must be the null string. This rule means that this URI argument is more restrictive than the URI argument of the AddElement method.
MoveNamespace=boolean This optional, name required argument is used to avoid a redundant namespace declaration on the old top element of the XmlDoc. If MoveNamespace=True and a URI argument (with a non-null string) is provided, then if the "old" top element contains a namespace declaration which is the same as the declaration inserted due to the URI argument, the declaration is deleted from the old top element.

Note: You should use MoveNamespace=True only when some consumer of the XML document cares about not having the redundant declaration. This would be an unusual situation, because the information content of the document does not change. Using MoveNamespace=True might trigger a scan of the entire XmlDoc, which AddTopElement does not normally do.

Usage notes

  • If a URI argument is specified, the XmlDoc's Namespace property must be On.
  • When you need to change the structure of an XML document and that need can be accomplished with AddTopElement, it is a more efficient approach than using AddSubtree. See the example in "Examples," below.

Examples

The following request makes a copy of an XML document, then adds "SOAP wrappers" to it:

Begin %doc is Object XmlDoc %doc2 is Object XmlDoc %doc = New %doc:LoadXml('<top><a></a></top>') Call %doc:Print Print '*********************' %doc2 = %doc:DeepCopy %doc2:AddTopElement('soap:Body', - 'http://schemas.xmlsoap.org/soap/envelope/') %doc2:AddTopElement('soap:Envelope', - 'http://schemas.xmlsoap.org/soap/envelope/') Call %doc2:Print End

The result (with new top element lines wrapped due to space limitations) is:

<top> <a> </a> </top> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap&colon.Body xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <top> <a> </a> </top> </soap:Body> </soap:Envelope>

In the example:

  • The order of the AddTopElement invocations is the reverse of the final order of the elements, that is, the second invocation creates the first element in the final document.
  • If you have no need to preserve the original XmlDoc, then both AddTopElement invocations can use %doc as the object method, and the DeepCopy can be omitted.
  • The redundant namespace declaration on the soap&colon.Body element above would be deleted if the MoveNamespace=True argument is used, although usually the redundant declaration is acceptable and MoveNamespace should be avoided.

Request-Cancellation Errors

  • Argument elementName violates the rules for an XML element name.
  • elementName is prefixed, but the XmlDoc Namespace property is None.
  • elementName is prefixed and Namespace is On, but URI is missing.
  • URI is invalid.
  • URI is present, but the Namespace property is not On, or, URI is the null string and elementName 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..
  • AddElement and InsertElementBefore also add 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: