AddSubtree (XmlDoc/XmlNode function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
m (1 revision)
m (1 revision)
Line 2: Line 2:


This callable function copies a subtree of an
This callable function copies a subtree of an
XmlDoc to the method parent node, where it becomes
<var>XmlDoc</var> to the method parent node, where it becomes
the last child of that node.
the last child of that node.
A subtree is
A subtree is
Line 12: Line 12:
<table class="syntaxTable">
<table class="syntaxTable">
<tr><th><i><b>%nod</b></i></th>
<tr><th><i><b>%nod</b></i></th>
<td>If specified, an XmlNode that is set to point to the top of the subtree that is added. </td></tr>
<td>If specified, an <var>XmlNode</var> that is set to point to the top of the subtree that is added. </td></tr>
<tr><th><i><b>nr</b></i></th>
<tr><th><i><b>nr</b></i></th>
<td>An XmlDoc or XmlNode that refers to the Root or Element node parent of the added subtree. </td></tr>
<td>An <var>XmlDoc</var> or <var>XmlNode</var> that refers to the Root or Element node parent of the added subtree. </td></tr>
<tr><th><i><b>sourceSubtreeNod</b></i></th>
<tr><th><i><b>sourceSubtreeNod</b></i></th>
<td>XmlNode that points to the non-Root node that is the top of the subtree to be copied. </td></tr>
<td><var>XmlNode</var> that points to the non-Root node that is the top of the subtree to be copied. </td></tr>
<tr><th>DefaultURI=<i><b>string</b></i></th>
<tr><th>DefaultURI=<i><b>string</b></i></th>
<td>Optional and NameRequired Unicode string parameter that controls the URI of the default namespace of some of the Element nodes of the added subtree.
<td>Optional and NameRequired <var>Unicode</var> string parameter that controls the URI of the default namespace of some of the Element nodes of the added subtree.
<p class="code">If a <tt>DefaultURI</tt> argument is specified, its value becomes the URI that is used for all unprefixed elements in the added subtree that are not already in the scope of a default namespace declaration within the added subtree. For more details, see the "Usage Notes" and "Examples" sections, below.
<p class="code">If a <tt>DefaultURI</tt> argument is specified, its value becomes the URI that is used for all unprefixed elements in the added subtree that are not already in the scope of a default namespace declaration within the added subtree. For more details, see the "Usage Notes" and "Examples" sections, below.
This parameter is available as of ''Sirius Mods'' version 6.9.</td></tr>
This parameter is available as of ''Sirius Mods'' version 6.9.</td></tr>
Line 26: Line 26:
==Usage notes==
==Usage notes==
<ul>
<ul>
<li>Since the return value of AddSubtree is frequently not needed,
<li>Since the return value of <var>AddSubtree</var> is frequently not needed,
you may want to Call it instead of saving its return value.
you may want to Call it instead of saving its return value.
<li>The target parent and subtree nodes can either be in the same
<li>The target parent and subtree nodes can either be in the same
or in different XmlDocs.
or in different <var>XmlDoc</var>s.
<li>When copying between different XmlDocs under ''Sirius Mods'' version 6.8 or earlier,
<li>When copying between different <var>XmlDoc</var>s under ''Sirius Mods'' version 6.8 or earlier,
the XmlDocs' Namespace properties must be the same, that is, both
the <var>XmlDoc</var>s' Namespace properties must be the same, that is, both
<tt>On</tt> or both <tt>Off</tt>.
<tt>On</tt> or both <tt>Off</tt>.
As of ''Sirius Mods'' version 6.9, these conditions also permit such copying:
As of ''Sirius Mods'' version 6.9, these conditions also permit such copying:
<ul>
<ul>
<li>The source XmlDoc has Namespace=None and the target XmlDoc has
<li>The source <var>XmlDoc</var> has Namespace=None and the target <var>XmlDoc</var> has
Namespace=Ignore.
Namespace=Ignore.
<li>The source XmlDoc has Namespace=None and the target XmlDoc has
<li>The source <var>XmlDoc</var> has Namespace=None and the target <var>XmlDoc</var> has
Namespace=On, if the <tt>DefaultURI</tt> argument is specified.
Namespace=On, if the <tt>DefaultURI</tt> argument is specified.
</ul>
</ul>
<li>Processing of an XmlDoc is likely to be more efficient if
<li>Processing of an <var>XmlDoc</var> is likely to be more efficient if
you add nodes in document
you add nodes in document
order (that is, top-to-bottom, left-to-right).
order (that is, top-to-bottom, left-to-right).
Line 46: Line 46:
(that is, as a sibling of)
(that is, as a sibling of)
another Text node, unless the [[AdjacentText (XmlDoc property)|AdjacentText]] property setting is <tt>Combine</tt>.
another Text node, unless the [[AdjacentText (XmlDoc property)|AdjacentText]] property setting is <tt>Combine</tt>.
<li>A node that is added to an XmlDoc whose [[AllowNull (XmlDoc property)|AllowNull]] property setting is <tt>True</tt>,
<li>A node that is added to an <var>XmlDoc</var> whose [[AllowNull (XmlDoc property)|AllowNull]] property setting is <tt>True</tt>,
may not later be the source node of a subtree to be copied
may not later be the source node of a subtree to be copied
to a target XmlDoc in which <tt>AllowNull=False</tt>.
to a target <var>XmlDoc</var> in which <tt>AllowNull=False</tt>.
<li>If the <tt>DefaultURI</tt> argument is specified, the value of the Namespace
<li>If the <tt>DefaultURI</tt> argument is specified, the value of the Namespace
property of the method object of AddSubtree must be <tt>On</tt>.
property of the method object of <var>AddSubtree</var> must be <tt>On</tt>.
<li>If the <tt>DefaultURI</tt> argument is ''not'' specified,
<li>If the <tt>DefaultURI</tt> argument is ''not'' specified,
each node added by AddSubtree retains the
each node added by <var>AddSubtree</var> retains the
URI value it had in the source XmlDoc.
URI value it had in the source <var>XmlDoc</var>.
See the [[#adsub2|example]] below.
See the [[#adsub2|example]] below.


If a <tt>DefaultURI</tt> argument is present, its value is used as the URI
If a <tt>DefaultURI</tt> argument is present, its value is used as the URI
of any unprefixed node whose namespace declaration is located at
of any unprefixed node whose namespace declaration is located at
a source XmlDoc element that is ''not'' part of the added subtree.
a source <var>XmlDoc</var> element that is ''not'' part of the added subtree.
See the [[#adsub3|example]] below.
See the [[#adsub3|example]] below.


Line 66: Line 66:
See the [[#adsub4|example]] below.
See the [[#adsub4|example]] below.
<li>You can apply the [[DefaultURI (XmlDoc/XmlNode function)|DefaultURI]] function
<li>You can apply the [[DefaultURI (XmlDoc/XmlNode function)|DefaultURI]] function
to the AddSubtree method object to cause an
to the <var>AddSubtree</var> method object to cause an
added subtree to "inherit" the default namespace of that node.
added subtree to "inherit" the default namespace of that node.
See the [[#adsub5|example]] below.
See the [[#adsub5|example]] below.
Line 74: Line 74:
<ol>
<ol>
<li>In the following example,
<li>In the following example,
the top-level Element (and its descendants) from XmlDoc <tt>%doc2</tt> are
the top-level Element (and its descendants) from <var>XmlDoc</var> <tt>%doc2</tt> are
copied as the "body" of a SOAP request document in <tt>%doc1</tt>:
copied as the "body" of a SOAP request document in <tt>%doc1</tt>:
<p class="code">Begin
<p class="code">Begin
%doc1 is Object XmlDoc
%doc1 is <var>Object</var> <var>XmlDoc</var>
%doc1 = new
%doc1 = new
%n1 is Object XmlNode
%n1 is <var>Object</var> <var>XmlNode</var>
%n2 is Object XmlNode
%n2 is <var>Object</var> <var>XmlNode</var>
%n1 = %doc1:AddElement('SOAP-ENV:Envelope', ,  -
%n1 = %doc1:AddElement('SOAP-ENV:Envelope', ,  -
   'http://schemas.xmlsoap.org/soap/envelope/')
   'http://schemas.xmlsoap.org/soap/envelope/')
%n1 = %n1:AddElement('SOAP-ENV:Body')
%n1 = %n1:AddElement('SOAP-ENV:Body')
%doc2 is Object XmlDoc
%doc2 is <var>Object</var> <var>XmlDoc</var>
%doc2 = new
%doc2 = new
%doc2:LoadXml('<top><a><b>05</b></a></top>')
%doc2:LoadXml('<top><a><b>05</b></a></top>')
%n2 = %doc2:SelectSingleNode('top')
%n2 = %doc2:SelectSingleNode('top')
%n1:AddSubtree(%n2)
%n1:<var>AddSubtree</var>(%n2)
%doc1:Print
%doc1:Print
End
End
Line 115: Line 115:
<div id="adsub2"></div>
<div id="adsub2"></div>
<li>In the following request, a source node is added to a target node,
<li>In the following request, a source node is added to a target node,
and the source node retains the URI value it had in the source XmlDoc:
and the source node retains the URI value it had in the source <var>XmlDoc</var>:
<p class="code">Begin
<p class="code">Begin
%outDoc is Object XmlDoc Auto New
%outDoc is <var>Object</var> <var>XmlDoc</var> Auto New
%inDoc is Object XmlDoc Auto New
%inDoc is <var>Object</var> <var>XmlDoc</var> Auto New
%tar is Object XmlNode
%tar is <var>Object</var> <var>XmlNode</var>
%src is Object XmlNode
%src is <var>Object</var> <var>XmlNode</var>


%tar = %outDoc:AddElement('target', , 'urn:target')
%tar = %outDoc:AddElement('target', , 'urn:target')
%src = %inDoc:AddElement('sourceWrap', , 'urn:source')
%src = %inDoc:AddElement('sourceWrap', , 'urn:source')
%src = %src:AddElement('source')
%src = %src:AddElement('source')
%tar:AddSubtree(%src)
%tar:<var>AddSubtree</var>(%src)
Print 'Source document:'
Print 'Source document:'
%inDoc:Print
%inDoc:Print
Line 151: Line 151:


The result is different if a <tt>DefaultURI</tt> argument is specified for
The result is different if a <tt>DefaultURI</tt> argument is specified for
AddSubtree as in the next example.
<var>AddSubtree</var> as in the next example.
<div id="adsub3"></div>
<div id="adsub3"></div>
<li>In the following request fragment,
<li>In the following request fragment,
a source node is added to a target node using a <tt>DefaultURI</tt> specification,
a source node is added to a target node using a <tt>DefaultURI</tt> specification,
and the source node retains the URI value it had in the source XmlDoc:
and the source node retains the URI value it had in the source <var>XmlDoc</var>:


<p class="code">%tar = %outDoc:AddElement('target', , 'urn:target')
<p class="code">%tar = %outDoc:AddElement('target', , 'urn:target')
%src = %inDoc:AddElement('sourceWrap', , 'urn:source')
%src = %inDoc:AddElement('sourceWrap', , 'urn:source')
%src = %src:AddElement('source')
%src = %src:AddElement('source')
%tar:AddSubtree(%src, DefaultURI='urn:target')
%tar:<var>AddSubtree</var>(%src, DefaultURI='urn:target')
%tar:Print
%tar:Print
</p>
</p>
Line 181: Line 181:
<p class="code">%tar = %outDoc:AddElement('target', , 'urn:target')
<p class="code">%tar = %outDoc:AddElement('target', , 'urn:target')
%src = %inDoc:AddElement('source', , 'urn:source')
%src = %inDoc:AddElement('source', , 'urn:source')
%tar:AddSubtree(%src, DefaultURI='urn:target')
%tar:<var>AddSubtree</var>(%src, DefaultURI='urn:target')
%tar:Print
%tar:Print
</p>
</p>
Line 197: Line 197:
%src = %inDoc:AddElement('sourceWrap', , 'urn:source')
%src = %inDoc:AddElement('sourceWrap', , 'urn:source')
%src = %src:AddElement('source')
%src = %src:AddElement('source')
%tar:AddSubtree(%src, DefaultURI=%tar:DefaultURI)
%tar:<var>AddSubtree</var>(%src, DefaultURI=%tar:DefaultURI)
%tar:Print
%tar:Print
</p>
</p>
Line 213: Line 213:
<li><i>Nr</i> is the same as, or a descendant of,
<li><i>Nr</i> is the same as, or a descendant of,
<i>sourceSubtreeNod</i>.
<i>sourceSubtreeNod</i>.
<li>Source and target XmlDocs have different Namespace settings.
<li>Source and target <var>XmlDoc</var>s have different Namespace settings.
<li>Any of the errors that would occur based on insertion of the
<li>Any of the errors that would occur based on insertion of the
top subtree
top subtree
Line 232: Line 232:
<ul>
<ul>
<li>[[??]] reftxt=InsertSubtreeBefore refid=inssubt. also copies a subtree.
<li>[[??]] reftxt=InsertSubtreeBefore refid=inssubt. also copies a subtree.
AddSubtree copies a subtree as the last child of the node pointed to by the
<var>AddSubtree</var> copies a subtree as the last child of the node pointed to by the
method object;
method object;
InsertSubtreeBefore copies a subtree as the immediately preceding
InsertSubtreeBefore copies a subtree as the immediately preceding
sibling of the node pointed to by the method object.
sibling of the node pointed to by the method object.
</ul>
</ul>

Revision as of 17:46, 25 January 2011

Copy subtree to XmlDoc Root or to Element XmlNode (XmlDoc and XmlNode classes)

[Requires Janus SOAP]


This callable function copies a subtree of an XmlDoc to the method parent node, where it becomes the last child of that node. A subtree is a node, all of its attributes and namespace declarations, and all descendants of the node and their attributes and namespace declarations.

Syntax

[%nod =] nr:AddSubtree( sourceSubtreeNode, [DefaultURI= unicode])

Syntax terms

%nod If specified, an XmlNode that is set to point to the top of the subtree that is added.
nr An XmlDoc or XmlNode that refers to the Root or Element node parent of the added subtree.
sourceSubtreeNod XmlNode that points to the non-Root node that is the top of the subtree to be copied.
DefaultURI=string Optional and NameRequired Unicode string parameter that controls the URI of the default namespace of some of the Element nodes of the added subtree.

If a DefaultURI argument is specified, its value becomes the URI that is used for all unprefixed elements in the added subtree that are not already in the scope of a default namespace declaration within the added subtree. For more details, see the "Usage Notes" and "Examples" sections, below. This parameter is available as of Sirius Mods version 6.9.

Usage notes

  • Since the return value of AddSubtree is frequently not needed, you may want to Call it instead of saving its return value.
  • The target parent and subtree nodes can either be in the same or in different XmlDocs.
  • When copying between different XmlDocs under Sirius Mods version 6.8 or earlier, the XmlDocs' Namespace properties must be the same, that is, both On or both Off. As of Sirius Mods version 6.9, these conditions also permit such copying:
    • The source XmlDoc has Namespace=None and the target XmlDoc has Namespace=Ignore.
    • The source XmlDoc has Namespace=None and the target XmlDoc has Namespace=On, if the DefaultURI argument is specified.
  • 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).
  • A subtree that is a Text node may not be added adjacent to (that is, as a sibling of) another Text node, unless the AdjacentText property setting is Combine.
  • A node that is added to an XmlDoc whose AllowNull property setting is True, may not later be the source node of a subtree to be copied to a target XmlDoc in which AllowNull=False.
  • If the DefaultURI argument is specified, the value of the Namespace property of the method object of AddSubtree must be On.
  • If the DefaultURI argument is not specified, each node added by AddSubtree retains the URI value it had in the source XmlDoc. See the example below. If a DefaultURI argument is present, its value is used as the URI of any unprefixed node whose namespace declaration is located at a source XmlDoc element that is not part of the added subtree. See the example below. The namespace URI of an unprefixed node is preserved if the namespace declaration is within the copied subtree, whether the DefaultURI argument is specified or not. See the example below.
  • You can apply the DefaultURI function to the AddSubtree method object to cause an added subtree to "inherit" the default namespace of that node. See the example below.

Examples

  1. In the following example, the top-level Element (and its descendants) from XmlDoc %doc2 are copied as the "body" of a SOAP request document in %doc1:

    Begin %doc1 is Object XmlDoc %doc1 = new %n1 is Object XmlNode %n2 is Object XmlNode %n1 = %doc1:AddElement('SOAP-ENV:Envelope', , - 'http://schemas.xmlsoap.org/soap/envelope/') %n1 = %n1:AddElement('SOAP-ENV:Body') %doc2 is Object XmlDoc %doc2 = new %doc2:LoadXml('<top><a>05</a></top>') %n2 = %doc2:SelectSingleNode('top') %n1:AddSubtree(%n2) %doc1:Print End

    Here is "doc1" before the subtree is added:

    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> <SOAP-ENV:Body/> </SOAP-ENV:Envelope>

    Here is "doc1" after the subtree is added:

    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> <SOAP-ENV:Body> <top> <a> 05 </a> </top> </SOAP-ENV:Body> </SOAP-ENV:Envelope>


  2. In the following request, a source node is added to a target node, and the source node retains the URI value it had in the source XmlDoc:

    Begin %outDoc is Object XmlDoc Auto New %inDoc is Object XmlDoc Auto New %tar is Object XmlNode %src is Object XmlNode %tar = %outDoc:AddElement('target', , 'urn:target') %src = %inDoc:AddElement('sourceWrap', , 'urn:source') %src = %src:AddElement('source') %tar:AddSubtree(%src) Print 'Source document:' %inDoc:Print Print ' ' Print 'Target document including copied subtree:' %outDoc:Print End

    These lines result:

    Source document: <sourceWrap xmlns="urn:source"> <source/> </sourceWrap> Target document including copied subtree: <target xmlns="urn:target"> <source xmlns="urn:source"/> </target>

    The default namespace URI in scope at the source element (child of sourceWrap) is urn:source, and that is retained when the node is copied to its target.

    The result is different if a DefaultURI argument is specified for AddSubtree as in the next example.

  3. In the following request fragment, a source node is added to a target node using a DefaultURI specification, and the source node retains the URI value it had in the source XmlDoc:

    %tar = %outDoc:AddElement('target', , 'urn:target') %src = %inDoc:AddElement('sourceWrap', , 'urn:source') %src = %src:AddElement('source') %tar:AddSubtree(%src, DefaultURI='urn:target') %tar:Print

    The URI of the source element after the subtree is added is the URI value specified in the DefaultURI argument:

    <target xmlns="urn:target"> <source/> </target>

    This source element URI would not be used if the declaration of the default namespace in scope at the source element were inside the source subtree, as in the next example.

  4. In the following User Language fragment, the namespace URI of an unprefixed node is preserved (despite the DefaultURI argument), since the namespace declaration is within the added subtree:

    %tar = %outDoc:AddElement('target', , 'urn:target') %src = %inDoc:AddElement('source', , 'urn:source') %tar:AddSubtree(%src, DefaultURI='urn:target') %tar:Print

    The result is:

    <target xmlns="urn:target"> <source xmlns="urn:source"/> </target>

  5. In the following fragment, the DefaultURI function is used to provide a value for the DefaultURI argument so that the added subtree "inherits" the default namespace of the target node:

    %tar = %outdoc:AddElement('target', , 'urn:target') %src = %inDoc:AddElement('sourceWrap', , 'urn:source') %src = %src:AddElement('source') %tar:AddSubtree(%src, DefaultURI=%tar:DefaultURI) %tar:Print

    The result is:

    <target xmlns="urn:target"> <source/> </target>

Request-Cancellation Errors

  • Nr is neither the Root nor an Element node.
  • sourceSubtreeNod is a Root node.
  • Nr is the same as, or a descendant of, sourceSubtreeNod.
  • Source and target XmlDocs have different Namespace settings.
  • Any of the errors that would occur based on insertion of the top subtree node at the given target parent, for example, insertion of a duplicate attribute, or insertion of an adjacent Text node when the AdjacentText property setting is Disallow. See the "Add" function corresponding to the type of the subtree node (for those examples just cited, AddAttribute and AddText, respectively).
  • Insufficient free space exists in CCATEMP.


See also

  • ?? reftxt=InsertSubtreeBefore refid=inssubt. also copies a subtree. AddSubtree copies a subtree as the last child of the node pointed to by the method object; InsertSubtreeBefore copies a subtree as the immediately preceding sibling of the node pointed to by the method object.