InsertSubtreeBefore (XmlNode function): Difference between revisions
m (1 revision) |
m (1 revision) |
||
Line 1: | Line 1: | ||
{{Template:XmlNode:InsertSubtreeBefore subtitle}} | {{Template:XmlNode:InsertSubtreeBefore subtitle}} | ||
This callable function inserts a copy of a subtree of an XmlDoc as | This callable function inserts a copy of a subtree of an <var>XmlDoc</var> as | ||
the sibling immediately preceding the method XmlNode. | the sibling immediately preceding the method <var>XmlNode</var>. | ||
A subtree is | A subtree is | ||
a node, all of its attributes and namespace declarations, and | a node, all of its attributes and namespace declarations, and | ||
Line 11: | Line 11: | ||
<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 inserted. </td></tr> | <td>If specified, an <var>XmlNode</var> that is set to point to the top of the subtree that is inserted. </td></tr> | ||
<tr><th><i><b>nod</b></i></th> | <tr><th><i><b>nod</b></i></th> | ||
<td>XmlNode that points to the (non-Root) node before which the subtree copy is inserted. </td></tr> | <td><var>XmlNode</var> that points to the (non-Root) node before which the subtree copy is inserted. </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 inserted 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 inserted subtree. | ||
<p class="code">If a <tt>DefaultURI</tt> argument is specified, its value is used as the URI that is used for all unprefixed elements in the inserted subtree that are not already in the scope of a default namespace declaration within the inserted subtree. For more details, see the "Usage Notes" section, below. | <p class="code">If a <tt>DefaultURI</tt> argument is specified, its value is used as the URI that is used for all unprefixed elements in the inserted subtree that are not already in the scope of a default namespace declaration within the inserted subtree. For more details, see the "Usage Notes" section, below. | ||
This parameter is available as of ''Sirius Mods'' version 6.9. Prior to ''Sirius Mods'' version 7.6, ''string'' is an EBCDIC string.</td></tr> | This parameter is available as of ''Sirius Mods'' version 6.9. Prior to ''Sirius Mods'' version 7.6, ''string'' is an EBCDIC string.</td></tr> | ||
Line 25: | Line 25: | ||
==Usage notes== | ==Usage notes== | ||
<ul> | <ul> | ||
<li>Since the return value of InsertSubtreeBefore is frequently not needed, | <li>Since the return value of <var>InsertSubtreeBefore</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 | or in different <var>XmlDoc</var>s. | ||
<li>When copying between different | <li>When copying between different <var>XmlDoc</var>s under ''Sirius Mods'' version 6.8 or earlier, | ||
the | 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> | ||
Line 42: | Line 42: | ||
if certain XPath operations are performed after using this method, because | if certain XPath operations are performed after using this method, because | ||
Insert-Before methods are guaranteed to add a node out of document order. | Insert-Before methods are guaranteed to add a node out of document order. | ||
In general, processing an XmlDoc is likely to be more efficient if | In general, processing an <var>XmlDoc</var> is likely to be more efficient if | ||
you add nodes in document order (that is, top-to-bottom, left-to-right). | you add nodes in document order (that is, top-to-bottom, left-to-right). | ||
<li>A subtree that is a Text node may not be inserted adjacent to | <li>A subtree that is a Text node may not be inserted adjacent to | ||
(that is, as a sibling of) another Text node, unless the [[AdjacentText (XmlDoc property)|AdjacentText]] property setting is <tt>Combine</tt>. | (that is, as a sibling of) 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 inserted | may not later be the source node of a subtree to be inserted | ||
into a target XmlDoc in which <tt>AllowNull=False</tt>. | into 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 InsertSubtreeBefore must be <tt>On</tt>. | property of the method object of <var>InsertSubtreeBefore</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 copied by InsertSubtreeBefore retains the | each node copied by <var>InsertSubtreeBefore</var> retains the | ||
URI value it had in the source XmlDoc. | URI value it had in the source <var>XmlDoc</var>. | ||
See example [[??]] refid=insub2.. | See example [[??]] refid=insub2.. | ||
If a <tt>DefaultURI</tt> argument is present, its value becomes the URI | If a <tt>DefaultURI</tt> argument is present, its value becomes 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 inserted subtree. | a source <var>XmlDoc</var> element that is ''not'' part of the inserted subtree. | ||
See example [[??]] refid=insub3.. | See example [[??]] refid=insub3.. | ||
Line 66: | Line 66: | ||
See example [[??]] refid=insub4.. | See example [[??]] refid=insub4.. | ||
<li>You can apply the [[DefaultURI (XmlDoc/XmlNode function)|DefaultURI]] function | <li>You can apply the [[DefaultURI (XmlDoc/XmlNode function)|DefaultURI]] function | ||
to the InsertSubtreeBefore method object to cause an | to the <var>InsertSubtreeBefore</var> method object to cause an | ||
inserted subtree to "inherit" the default namespace of the target parent node. | inserted subtree to "inherit" the default namespace of the target parent node. | ||
See example [[??]] refid=insub5.. | See example [[??]] refid=insub5.. | ||
Line 76: | Line 76: | ||
a subtree (node "c") is inserted before node "a": | a subtree (node "c") is inserted before node "a": | ||
<p class="code">Begin | <p class="code">Begin | ||
%doc is Object XmlDoc | %doc is <var>Object</var> <var>XmlDoc</var> | ||
%doc = New | %doc = New | ||
Call %doc:LoadXml - | Call %doc:LoadXml - | ||
('<top><a><b>05</b></a><c><d att="value"></d></c></top>') | ('<top><a><b>05</b></a><c><d att="value"></d></c></top>') | ||
%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 = %doc:SelectSingleNode('top/c') | %n1 = %doc:SelectSingleNode('top/c') | ||
%n2 = %doc:SelectSingleNode('top/a') | %n2 = %doc:SelectSingleNode('top/a') | ||
Call %n2:InsertSubtreeBefore(%n1) | Call %n2:<var>InsertSubtreeBefore</var>(%n1) | ||
Call %doc:Print('top') | Call %doc:Print('top') | ||
End | End | ||
Line 115: | Line 115: | ||
<li>In the following request, a source node is inserted before a | <li>In the following request, a source node is inserted before a | ||
sibling node, | sibling 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 | ||
%top is Object XmlNode | %top is <var>Object</var> <var>XmlNode</var> | ||
%src is Object XmlNode | %src is <var>Object</var> <var>XmlNode</var> | ||
%sib is Object XmlNode | %sib is <var>Object</var> <var>XmlNode</var> | ||
%top = %outDoc:AddElement('top', , 'urn:top') | %top = %outDoc:AddElement('top', , 'urn:top') | ||
Line 127: | Line 127: | ||
%src = %inDoc:AddElement('sourceWrap', , 'urn:source') | %src = %inDoc:AddElement('sourceWrap', , 'urn:source') | ||
%src = %src:AddElement('source') | %src = %src:AddElement('source') | ||
%sib:InsertSubtreeBefore(%src) | %sib:<var>InsertSubtreeBefore</var>(%src) | ||
Print 'Source document:' | Print 'Source document:' | ||
%inDoc:Print | %inDoc:Print | ||
Line 154: | Line 154: | ||
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 | ||
InsertSubtreeBefore as in the next example. | <var>InsertSubtreeBefore</var> as in the next example. | ||
<li>in the following request fragment: | <li>in the following request fragment: | ||
a source node is copied to a target using a <tt>DefaultURI</tt> specification, | a source node is copied to a target 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">%top = %outDoc:AddElement('top', , 'urn:top') | <p class="code">%top = %outDoc:AddElement('top', , 'urn:top') | ||
%sib = %top:AddElement('sibling') | %sib = %top:AddElement('sibling') | ||
%src = %inDoc:AddElement('sourceWrap', , 'urn:source') | %src = %inDoc:AddElement('sourceWrap', , 'urn:source') | ||
%src = %src:AddElement('source') | %src = %src:AddElement('source') | ||
%sib:InsertSubtreeBefore(%src, DefaultURI='urn:top') | %sib:<var>InsertSubtreeBefore</var>(%src, DefaultURI='urn:top') | ||
%top:Print | %top:Print | ||
</p> | </p> | ||
Line 184: | Line 184: | ||
%sib = %top:AddElement('sibling') | %sib = %top:AddElement('sibling') | ||
%src = %inDoc:AddElement('source', , 'urn:source') | %src = %inDoc:AddElement('source', , 'urn:source') | ||
%sib:InsertSubtreeBefore(%src, DefaultURI='urn:top') | %sib:<var>InsertSubtreeBefore</var>(%src, DefaultURI='urn:top') | ||
%top:Print | %top:Print | ||
</p> | </p> | ||
Line 205: | Line 205: | ||
%src = %src:AddElement('source') | %src = %src:AddElement('source') | ||
%sib:InsertSubtreeBefore(%src, DefaultURI=%top:DefaultURI) | %sib:<var>InsertSubtreeBefore</var>(%src, DefaultURI=%top:DefaultURI) | ||
%top:Print | %top:Print | ||
</p> | </p> | ||
Line 218: | Line 218: | ||
(<tt>..</tt>) in the DefaultURI function argument, | (<tt>..</tt>) in the DefaultURI function argument, | ||
will also produce the same result: | will also produce the same result: | ||
<p class="code">%sib:InsertSubtreeBefore(%src, - | <p class="code">%sib:<var>InsertSubtreeBefore</var>(%src, - | ||
DefaultURI=%sib:DefaultURI('..')) | DefaultURI=%sib:DefaultURI('..')) | ||
</p> | </p> | ||
Line 228: | Line 228: | ||
<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 | <li>Source and target <var>XmlDoc</var>s have different Namespace settings. | ||
<li>Any of the errors which would occur based on insertion of the | <li>Any of the errors which would occur based on insertion of the | ||
top subtree | top subtree | ||
Line 252: | Line 252: | ||
<li>[[AddSubtree (XmlDoc/XmlNode function)|AddSubtree]] | <li>[[AddSubtree (XmlDoc/XmlNode function)|AddSubtree]] | ||
also copies a Subtree node. | also copies a Subtree node. | ||
InsertSubtreeBefore copies a subtree as the immediately preceding | <var>InsertSubtreeBefore</var> 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; | ||
AddSubtree copies a subtree as the last child of the node pointed to by the | AddSubtree copies a subtree as the last child of the node pointed to by the |
Revision as of 17:46, 25 January 2011
Insert copy of subtree before this node (XmlNode class)
[Requires Janus SOAP]
This callable function inserts a copy of a subtree of an XmlDoc as
the sibling immediately preceding the method XmlNode.
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
[%outNod =] nod:InsertSubtreeBefore( sourceSubtreeNode, [DefaultURI= unicode])
Syntax terms
%nod | If specified, an XmlNode that is set to point to the top of the subtree that is inserted. |
---|---|
nod | XmlNode that points to the (non-Root) node before which the subtree copy is inserted. |
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 inserted subtree.
If a DefaultURI argument is specified, its value is used as the URI that is used for all unprefixed elements in the inserted subtree that are not already in the scope of a default namespace declaration within the inserted subtree. For more details, see the "Usage Notes" section, below. This parameter is available as of Sirius Mods version 6.9. Prior to Sirius Mods version 7.6, string is an EBCDIC string. |
Usage notes
- Since the return value of InsertSubtreeBefore 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.
- There is a very small "one-time" processing cost if certain XPath operations are performed after using this method, because Insert-Before methods are guaranteed to add a node out of document order. In general, processing 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 inserted 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 inserted into a target XmlDoc in which AllowNull=False.
- If the DefaultURI argument is specified, the value of the Namespace property of the method object of InsertSubtreeBefore must be On.
- If the DefaultURI argument is not specified, each node copied by InsertSubtreeBefore retains the URI value it had in the source XmlDoc. See example ?? refid=insub2.. If a DefaultURI argument is present, its value becomes the URI of any unprefixed node whose namespace declaration is located at a source XmlDoc element that is not part of the inserted subtree. See example ?? refid=insub3.. The namespace URI of an unprefixed node is preserved if the namespace declaration is within the inserted subtree, whether the DefaultURI argument is specified or not. See example ?? refid=insub4..
- You can apply the DefaultURI function to the InsertSubtreeBefore method object to cause an inserted subtree to "inherit" the default namespace of the target parent node. See example ?? refid=insub5..
Examples
- In the following example,
a subtree (node "c") is inserted before node "a":
Begin %doc is Object XmlDoc %doc = New Call %doc:LoadXml - ('<top><a>05</a><c><d att="value"></d></c></top>') %n1 is Object XmlNode %n2 is Object XmlNode %n1 = %doc:SelectSingleNode('top/c') %n2 = %doc:SelectSingleNode('top/a') Call %n2:InsertSubtreeBefore(%n1) Call %doc:Print('top') End
Here is the serialized document before the subtree is inserted:
<top> <a> 05 </a> <c> <d att="value"/> </c> </top>
Here is the document after the subtree is inserted:
<top> <c> <d att="value"/> </c> <a> 05 </a> <c> <d att="value"/> </c> </top>
- In the following request, a source node is inserted before a
sibling 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 %top is Object XmlNode %src is Object XmlNode %sib is Object XmlNode %top = %outDoc:AddElement('top', , 'urn:top') %sib = %top:AddElement('sibling') %src = %inDoc:AddElement('sourceWrap', , 'urn:source') %src = %src:AddElement('source') %sib:InsertSubtreeBefore(%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: <top xmlns="urn:top"> <source xmlns="urn:source"/> <sibling/> </top>
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 InsertSubtreeBefore as in the next example.
- in the following request fragment:
a source node is copied to a target using a DefaultURI specification,
and the source node retains the URI value it had in the source XmlDoc:
%top = %outDoc:AddElement('top', , 'urn:top') %sib = %top:AddElement('sibling') %src = %inDoc:AddElement('sourceWrap', , 'urn:source') %src = %src:AddElement('source') %sib:InsertSubtreeBefore(%src, DefaultURI='urn:top') %top:Print
The URI of the source element after the subtree is inserted is the URI value specified in the DefaultURI argument:
<top xmlns="urn:top"> <source/> <sibling/> </top>
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.
- 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 inserted subtree:
%top = %outDoc:AddElement('top', , 'urn:top') %sib = %top:AddElement('sibling') %src = %inDoc:AddElement('source', , 'urn:source') %sib:InsertSubtreeBefore(%src, DefaultURI='urn:top') %top:Print
The result is:
<top xmlns="urn:top"> <source xmlns="urn:source"/> <sibling/> </top>
- In the following fragment,
the DefaultURI function
is used to provide a value for the DefaultURI argument so that
the inserted subtree "inherits" the default namespace of the
parent of the target sibling node:
For example, in the following User Language fragment:
%top = %outDoc:AddElement('top', , 'urn:top') %sib = %top:AddElement('sibling') %src = %inDoc:AddElement('sourceWrap', , 'urn:source') %src = %src:AddElement('source') %sib:InsertSubtreeBefore(%src, DefaultURI=%top:DefaultURI) %top:Print
The result is:
<top xmlns="urn:top"> <source/> <sibling/> </top>
The following method call, which uses the XPath notation for parent node (..) in the DefaultURI function argument, will also produce the same result:
%sib:InsertSubtreeBefore(%src, - DefaultURI=%sib:DefaultURI('..'))
Request-Cancellation Errors
- Nod is the Root 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 which would occur based on insertion of the
top subtree
node at the given target parent: for example,
- insertion of a duplicate attribute
- insertion of a Text node before a target Text node, when the AdjacentText property setting is Disallow
- insertion of a Text node before a target sibling that follows a 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
- AddSubtree also copies a Subtree node. InsertSubtreeBefore copies a subtree as the immediately preceding sibling of the node pointed to by the method object; AddSubtree copies a subtree as the last child of the node pointed to by the method object.
- For hints about inserting a node after a sibling node, see the "Example" section in InsertElementBefore.