AddTopElement (XmlDoc function): Difference between revisions
m (minor formatting) |
m (insert < for < for self-closed element) |
||
Line 49: | Line 49: | ||
%doc = new | %doc = new | ||
%doc:[[LoadXml_(XmlDoc/XmlNode_function)|loadXml]]('<top><a> | %doc:[[LoadXml_(XmlDoc/XmlNode_function)|loadXml]]('<top><a><b/></a></top>') | ||
[[ | [[Notation conventions for methods#Callable methods|Call]] %doc:print | ||
<nowiki>print '*********************'</nowiki> | <nowiki>print '*********************'</nowiki> | ||
%doc2 = %doc:[[DeepCopy_(XmlDoc_function)|DeepCopy]] | %doc2 = %doc:[[DeepCopy_(XmlDoc_function)|DeepCopy]] | ||
<nowiki>%doc2:addTopElement('soap:Body', | <nowiki>%doc2:addTopElement('soap:Body', 'http://schemas.xmlsoap.org/soap/envelope/') | ||
%doc2:addTopElement('soap:Envelope', 'http://schemas.xmlsoap.org/soap/envelope/')</nowiki> | |||
%doc2:addTopElement('soap:Envelope', | |||
call %doc2:print | call %doc2:print | ||
Line 66: | Line 64: | ||
<p class="output"><nowiki><top> | <p class="output"><nowiki><top> | ||
<a> | <a> | ||
<b/> | |||
</a> | </a> | ||
</top> | </top> | ||
Line 74: | Line 72: | ||
<top> | <top> | ||
<a> | <a> | ||
<b/> | |||
</a> | </a> | ||
</top> | </top> | ||
Line 97: | Line 95: | ||
%doc = new | %doc = new | ||
%doc:loadXml('<top xmlns="http:mydata"><a> | %doc:loadXml('<top xmlns="http:mydata"><a><b/></a></top>') | ||
call %doc:print | call %doc:print | ||
<nowiki>print '*********************'</nowiki> | <nowiki>print '*********************'</nowiki> | ||
<nowiki>%doc2 = %doc:deepCopy | <nowiki>%doc2 = %doc:deepCopy | ||
%doc2:addTopElement('Body', | %doc2:addTopElement('Body', 'http://schemas.xmlsoap.org/soap/envelope/') | ||
%doc2:addTopElement('Envelope', 'http://schemas.xmlsoap.org/soap/envelope/')</nowiki> | |||
%doc2:addTopElement('Envelope', | |||
call %doc2:print | call %doc2:print | ||
Line 114: | Line 110: | ||
<p class="output"><nowiki><top xmlns="http:mydata"> | <p class="output"><nowiki><top xmlns="http:mydata"> | ||
<a> | <a> | ||
<b/> | |||
</a> | </a> | ||
</top> | </top> | ||
Line 122: | Line 118: | ||
<top xmlns="http:mydata"> | <top xmlns="http:mydata"> | ||
<a> | <a> | ||
<b/> | |||
</a> | </a> | ||
</top> | </top> |
Latest revision as of 20:58, 8 August 2018
Add new top element to XmlDoc (XmlDoc class)
[Introduced in Sirius Mods 7.7; requires Janus SOAP]
The AddTopElement 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 element namespace 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. |
name | 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 300 characters (127 characters prior to version 7.9, and 100 characters prior to version 7.7). |
uri | This optional, Unicode string, argument defaults to the null string:
|
MoveNamespace | 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 is available as of Sirius Mods version 7.8. |
Usage notes
- If a uri argument is specified, the XmlDoc's Namespace property value 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 below.
Examples
Add prefixed SOAP wrappers
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><b/></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 is:
<top> <a> <b/> </a> </top> ********************* <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <top> <a> <b/> </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:Body
element above would be deleted if theMoveNamespace=True
argument is used, although usually the redundant declaration is acceptable and MoveNamespace should be avoided.
Add unprefixed SOAP wrappers
The following example is similar, except that the SOAP elements use a default namespace declaration. AddTopElement only allows this when the old top element is in a default namespace (here http:mydata
). Note that this example requires Sirius Mods Version 7.9 or greater.
begin %doc is object xmlDoc %doc2 is object xmlDoc %doc = new %doc:loadXml('<top xmlns="http:mydata"><a><b/></a></top>') call %doc:print print '*********************' %doc2 = %doc:deepCopy %doc2:addTopElement('Body', 'http://schemas.xmlsoap.org/soap/envelope/') %doc2:addTopElement('Envelope', 'http://schemas.xmlsoap.org/soap/envelope/') call %doc2:print end
The result is:
<top xmlns="http:mydata"> <a> <b/> </a> </top> ********************* <Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/"> <Body xmlns="http://schemas.xmlsoap.org/soap/envelope/"> <top xmlns="http:mydata"> <a> <b/> </a> </top> </Body> </Envelope>
Request-cancellation errors
- The name argument violates the rules for an XML element name.
- name is prefixed, but the XmlDoc Namespace property value is None.
- name is prefixed and Namespace is On, but uri is missing.
- The uri argument is invalid.
- uri is present, but the Namespace property value 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 Names and namespaces.
- For more information about URIs, see Uniform Resource Identifier syntax.
- 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.
- The DeleteTopElement subroutine removes the top Element from an XmlDoc.
- These methods are useful for working with namespaces: