AddNamespace (XmlNode subroutine): Difference between revisions

From m204wiki
Jump to navigation Jump to search
m (move footer template to actual end (otherwise subsequent intervening text gets omitted), tags and links)
Line 1: Line 1:
{{Template:XmlNode:AddNamespace subtitle}}
{{Template:XmlNode:AddNamespace subtitle}}
 
This subroutine adds a namespace declaration to an <var>Element</var> node.
This subroutine adds a namespace declaration to an Element node.


==Syntax==
==Syntax==
Line 8: Line 7:
<table class="syntaxTable">
<table class="syntaxTable">
<tr><th>nod</th>
<tr><th>nod</th>
<td>An <var>XmlNode</var> that points to the Element node where the namespace declaration is added. </td></tr>
<td>An <var>XmlNode</var> that points to the <var>Element</var> node where the namespace declaration is added. </td></tr>
<tr><th>prefix</th>
<tr><th>prefix</th>
<td>The prefix that is being declared. It must conform to the XML syntax rules for a namespace prefix; the maximum length of the prefix is 127 characters (100 characters prior to version 7.7 of [[Janus SOAP]]). </td></tr>
<td>The prefix that is being declared. It must conform to the XML syntax rules for a namespace prefix; the maximum length of the prefix is 127 characters (100 characters prior to Version 7.7 of <var class="product">[[Janus SOAP|"Janus Soap"]]</var>). </td></tr>
<tr><th>uri</th>
<tr><th>uri</th>
<td>An absolute Uniform Resource Identifier, identifying the namespace the <var class="term">prefix</var> is declared within.</td></tr>
<td>An absolute Uniform Resource Identifier, identifying the namespace the <var class="term">prefix</var> is declared within.</td></tr>
Line 17: Line 16:
==Usage notes==
==Usage notes==
<ul>
<ul>
<li>If a URI argument is specified, the <var>XmlDoc</var>'s [[Namespace (XmlDoc property)|Namespace]] property must be <tt>On</tt>.
<li>If a <var class="term">URI</var> argument is specified, the <var>XmlDoc</var>'s [[Namespace (XmlDoc property)|Namespace]] property must be <code>On</code>.
<li>As of ''Sirius Mods'' version 7.3, the <var>AddNamespace</var> method arguments may
<li>As of <var class="product">[[Sirius Mods|"Sirius Mods"]]</var> Version 7.3, the <var>AddNamespace</var> method arguments may include only non-null EBCDIC characters that translate to <var>Unicode</var>.
include only non-null EBCDIC characters that translate to <var>Unicode</var>.
As of <var class="product">"Sirius Mods"</var> version 7.6, these argument strings are <var>Unicode</var> or are converted to <var>Unicode</var>. For more information about the effects of this Version 7.6
As of ''Sirius Mods'' version 7.6, these argument strings are <var>Unicode</var> or are
converted to <var>Unicode</var>.
For more information about the effects of this version 7.6
change, see [[XmlDoc API#Strings and Unicode with the XmlDoc API|"Strings and Unicode with the XmlDoc API"]].
change, see [[XmlDoc API#Strings and Unicode with the XmlDoc API|"Strings and Unicode with the XmlDoc API"]].
<li>Namespace declarations are serialized
<li>Namespace declarations are serialized in the order in which they are added.
in the order in which they are added.
<li>Adding the same declaration multiple times on an element is allowed.
<li>Adding the same declaration multiple times on an element is allowed.
<li><div id="keepNamespace"></div>To ensure that URIs of already added nodes are preserved:
<li><div id="keepNamespace"></div>To ensure that URIs of already added nodes are preserved:
<ul>
<ul>
<li>A namespace declaration
<li>A namespace declaration may not be added to an <var>Element</var> that has the same prefix (including no prefix, in the case of the default namespace declaration) if the Element's URI differs from the <var>AddNamespace</var> URI.
may not be added to an Element that has the same prefix (including no
<li>A namespace declaration may not be added to an <var>Element</var> that has an attribute with the same prefix if the attribute's URI differs from the <var>AddNamespace</var> URI.
prefix, in the case of the default namespace declaration) if the
<ul><li>Prior to Version 7.7, a namespace declaration may not be added to an <var>Element</var> that has <var>Element</var>  children.  See the [[#Namespace conflict|"Namespace conflict"]] example below.
Element's URI differs from the <var>AddNamespace</var> URI.
<li>Starting with version 7.7, this restriction does not apply when a non-default namespace declaration is being added to the top-level <var>Element</var> of the XmlDoc. See the [[#Add to top element|"Add to top element"]] example below.
<li>A namespace declaration
<li>Starting with version 7.9, a default namespace declaration can be added to a prefixed <var>Element</var> if all of its <var>Element</var> children themselves have default namespace declarations. See the [[#Add default namespace|"Add default namespace"]] example below.
may not be added to an Element that has an attribute with the same prefix
if the attribute's URI differs from the <var>AddNamespace</var> URI.
<li><ul><li>Prior to version 7.7, a namespace declaration may not be added to an Element that has Element
children.  See the [[#Namespace conflict|"Namespace conflict"]] example below.
<li>Starting with version 7.7, this restriction
does not apply when a non-default namespace declaration is being added to the top-level Element of the XmlDoc.
See the [[#Add to top element|"Add to top element"]] example below.
<li>Starting with version 7.9, a default namespace declaration can be added to a prefixed Element if all of its Element children themselves have default namespace declarations.
See the [[#Add default namespace|"Add default namespace"]] example below.
</ul>
</ul>
</ul>
</ul>
<li>All well-formed <var>XmlDoc</var>s implicitly include this namespace declaration
<li>All well-formed <var>XmlDoc</var>s implicitly include this namespace declaration in scope at the top-level document element:
in scope at the top-level document element:
<p class="code">xmlns:xml="http://www.w3.org/XML/1998/namespace"
<p class="code">xmlns:xml="http://www.w3.org/XML/1998/namespace"
</p>
</p>
The "xml" prefix is currently valid only for the special attributes
The "xml" prefix is currently valid only for the special attributes <code>xml:space</code> and <code>xml:lang</code>. You are ''not'' allowed to:
<tt>xml:space</tt> and <tt>xml:lang</tt>.
You are ''not'' allowed to:
<ul>
<ul>
<li>Use any other "xml" prefixed element or attribute names
<li>Use any other "xml" prefixed element or attribute names
<li>Add a namespace declaration
<li>Add a namespace declaration for the "xml" prefix for a URI other than <nowiki>"http://www.w3.org/xml/1998/namespace"</nowiki>;
for the "xml" prefix
<li>Add a namespace declaration for <nowiki>"http://www.w3.org/xml/1998/namespace"</nowiki> that has any other prefix besides "xml"
for a URI other than <nowiki>"http://www.w3.org/xml/1998/namespace"</nowiki>;
<li>Add a namespace declaration for <nowiki>"http://www.w3.org/xml/1998/namespace"</nowiki>
that has any other prefix besides "xml"
</ul>
</ul>
</ul>
</ul>
Line 64: Line 44:
==Example==
==Example==
===Add namespace to "leaf" element===
===Add namespace to "leaf" element===
In the following example, the <var>AddNamespace</var> method adds a namespace to a node
In the following example, the <var>AddNamespace</var> method adds a namespace to a node that has an existing namespace. Subsequent elements are added to that node using each of the eligible namespace prefixes.
that has an existing namespace. Subsequent elements are added to that node
<p class="code">begin
using each of the eligible namespace prefixes.
  %doc is object xmlDoc
<p class="code"><nowiki>Begin
  %doc = new
%doc is Object XmlDoc
  %doc:[[LoadXml_(XmlDoc/XmlNode_function)|loadXml]]('<top  -
%doc = New
      xmlns:SOAP="<nowiki>http://SCHEMAS.XMLSOAP.ORG/soap/"</nowiki>>  -
%doc:LoadXml('<top  -
      <a><SOAP:b></SOAP:b></a></top>')
  xmlns:SOAP="http://SCHEMAS.XMLSOAP.ORG/soap/">  -
  %n1 is object xmlNode
  <a><SOAP:b></SOAP:b></a></top>')
  %n2 is object xmlNode
%n1 is Object XmlNode
  %n3 is object xmlNode
%n2 is Object XmlNode
  %n1 = %doc:[[SelectSingleNode_(XmlDoc/XmlNode_function)|selectSingleNode]]('top/*/*')
%n3 is Object XmlNode
  call %n1:addNamespace('sirius', '<nowiki>http://sirius-software.com</nowiki>')
%n1 = %doc:SelectSingleNode('top/*/*')
  print 'The URI of node SOAP:b is: ' %n1:URI
Call %n1:AddNamespace('sirius', 'http://sirius-software.com')
  %n2 = %n1:[[AddElement_(XmlDoc/XmlNode_function)|addElement]]('sirius:c')
Print 'The URI of node SOAP:b is: ' %n1:URI
  %n3 = %n1:addElement('SOAP:c')
%n2 = %n1:AddElement('sirius:c')
  call %doc:print
%n3 = %n1:AddElement('SOAP:c')
  print 'The URI of node sirius:c is: ' %n2:URI
Call %doc:Print
  print 'The URI of node SOAP:c is: ' %n3:URI
Print 'The URI of node sirius:c is: ' %n2:URI
end
Print 'The URI of node SOAP:c is: ' %n3:URI
</p>
End
</nowiki></p>


The example result follows:
The example result follows:
Line 104: Line 82:
====Namespace conflict====
====Namespace conflict====
Here is an example which shows a <var>AddNamespace</var> which is disallowed to preserve a URI:
Here is an example which shows a <var>AddNamespace</var> which is disallowed to preserve a URI:
<p class="code">%top = %doc:AddElement('p1:top', , 'u:p1')
<p class="code">   %top = %doc:AddElement('p1:top', , 'u:p1')


%lev2 = %top:AddElement('p2:lev2', , 'u:p2')
  %lev2 = %top:AddElement('p2:lev2', , 'u:p2')


%lev2:AddElement('p1:lev3')
  %lev2:AddElement('p1:lev3')
%doc:Print
  %doc:Print


%lev2:AddNamespace('p1', 'u:p3')
  %lev2:AddNamespace('p1', 'u:p3')
</p>
</p>
The result of the above fragment is:
The result of the above fragment is:
<p class="output"><nowiki><p1:top xmlns:p1="u:p1">       
<p class="output"><p1:top xmlns:p1="u:p1">       
   <p2:lev2 xmlns:p2="u:p2">   
   <p2:lev2 xmlns:p2="u:p2">   
       <p1:lev3/>               
       <p1:lev3/>               
   </p2:lev2>                   
   </p2:lev2>                   
</p1:top>                    
</p1:top>  
<nowiki>               
***  1  CANCELLING REQUEST: MSIR.0750: Class XmlNode, subroutine AddNamespace:
***  1  CANCELLING REQUEST: MSIR.0750: Class XmlNode, subroutine AddNamespace:
         can't expose descendants to new non-default namespace in line ...
         can't expose descendants to new non-default namespace in line ...
</nowiki></p>
</nowiki>
</p>
If the namespace declaration <code>xmlns:p1="u:p3"</code> were simply inserted in the start tag of <code><p2:lev2...></code>, that would change the URI of element <code>p1:lev3</code>, and so it is prohibited.
If the namespace declaration <code>xmlns:p1="u:p3"</code> were simply inserted in the start tag of <code><p2:lev2...></code>, that would change the URI of element <code>p1:lev3</code>, and so it is prohibited.


====Add to top element====
====Add to top element====
Since the top element of an <var>XmlDoc</var> cannot "block" a namespace declaration, the general restriction against <var>AddNamespace</var> on an Element with child Elements was removed in version 7.7, for the top element, when the namespace being added has a prefix:
Since the top element of an <var>XmlDoc</var> cannot "block" a namespace declaration, the general restriction against <var>AddNamespace</var> on an <var>Element</var> with child <var>Elements</var> was removed in Version 7.7, for the top element, when the namespace being added has a prefix:
<p class="code">%top = %doc:AddElement('top')
<p class="code">%top = %doc:AddElement('top')


%top:AddElement('p2:lev2', , 'u:p2')
%top:AddElement('p2:lev2', , 'u:p2')
Line 134: Line 114:
</p>
</p>
The result of the above fragment is:
The result of the above fragment is:
<p class="output"><nowiki><top xmlns:p1="u:px">             
<p class="output"><top xmlns:p1="u:px">             
   <p2:lev2 xmlns:p2="u:p2"/>     
   <p2:lev2 xmlns:p2="u:p2"/>     
</top>
</top>
</nowiki></p>
</p>
====Add default namespace====
====Add default namespace====
Since a default namespace declaration is "blocked" if all children of the Element have default namespace declarations, the general restriction against <var>AddNamespace</var> on an Element with child Elements was removed in version 7.7, if a default namespace is being added in such a situation:
Since a default namespace declaration is "blocked" if all children of the <var>Element</var> have default namespace declarations, the general restriction against <var>AddNamespace</var> on an <var>Element</var> with child <var>Elements</var> was removed in version 7.7, if a default namespace is being added in such a situation:
<p class="code">%top = %doc:AddElement('p1:top', , 'u:p1')
<p class="code">%top = %doc:AddElement('p1:top', , 'u:p1')


Line 148: Line 128:
</p>
</p>
The result of the above fragment is:
The result of the above fragment is:
<p class="output"><nowiki><p1:top xmlns:p1="u:p1" xmlns="u:y">   
<p class="output"><p1:top xmlns:p1="u:p1" xmlns="u:y">   
   <lev2 xmlns="u:x"/>                 
   <lev2 xmlns="u:x"/>                 
</p1:top>                             
</p1:top>                             
</nowiki></p>
</p>


==Request-Cancellation Errors==
==Request-Cancellation Errors==
<ul>
<ul>
<li><i>Nod</i> does not point to an Element node.
<li><var class="term">nod</var> does not point to an <var>Element</var> node.
<li>Element pointed to by <i>nod</i> contains one or more
<li>Element pointed to by <var class="term">nod</var> contains one or more child <var>Element</var> nodes.
child Element nodes.
<li><var class="term">prefix</var> is an invalid prefix.
<li><i>Prefix</i> is an invalid prefix.
<li><var class="term">URI</var> is an invalid URI.
<li><i>URI</i> is an invalid URI.
<li><var class="term">prefix</var> is the same as the prefix of the <var>Element</var> that <var class="term">nod</var> points to, but <var class="term">URI</var> is different from the namespace URI of the <var>Element</var>.
<li><i>Prefix</i> is the same as the prefix of the Element that
<li>A declaration for the same <var class="term">prefix</var> with a different <var class="term">URI</var> is already present (not merely in scope) at the <var>Element</var> pointed to by <var class="term">nod</var>.
<i>nod</i> points to, but <i>URI</i> is different from the namespace URI of
<li>The <var>XmlDoc</var>'s <var>Namespace</var> property is not <code>On</code>.
the Element.
<li>A declaration for the same <i>prefix</i> with a different
<i>URI</i> is already present (not merely in scope) at the Element pointed to by
<i>nod</i>.
<li>The <var>XmlDoc</var>'s Namespace property is not On.
</ul>
</ul>


==See also==
==See also==
{{Template:XmlNode:AddNamespace footer}}
<ul>
<ul>
<li>For more information about
<li>For more information about namespace declarations, see [[XML processing in Janus SOAP#Names and namespaces|"Names and namespaces"]].
namespace declarations, see [[XML processing in Janus SOAP#Names and namespaces|Names and namespaces]].
<li>For more information about URIs, see [[XML processing in Janus SOAP#Uniform Resource Identifier syntax|"Uniform Resource Identifier syntax"]].
<li>For more information about URIs, see [[XML processing in Janus SOAP#Uniform Resource Identifier syntax|Uniform Resource Identifier syntax]].
<li>For more information about namespaces and adding nodes, see [[XmlDoc API#Namespaces with Add* and Insert* methods]].
<li>For more information about namespaces and adding nodes,
see [[XmlDoc API#Namespaces with Add* and Insert* methods]].
<li>These methods are also useful for working with namespaces:
<li>These methods are also useful for working with namespaces:
<ul>
<ul>
Line 184: Line 155:
</ul>
</ul>
</ul>
</ul>
{{Template:XmlNode:AddNamespace footer}}

Revision as of 03:48, 26 April 2011

Add namespace declaration to Element node (XmlNode class)

[Requires Janus SOAP]

This subroutine adds a namespace declaration to an Element node.

Syntax

nod:AddNamespace( prefix, uri)

Syntax terms

nod An XmlNode that points to the Element node where the namespace declaration is added.
prefix The prefix that is being declared. It must conform to the XML syntax rules for a namespace prefix; the maximum length of the prefix is 127 characters (100 characters prior to Version 7.7 of "Janus Soap").
uri An absolute Uniform Resource Identifier, identifying the namespace the prefix is declared within.

Usage notes

  • If a URI argument is specified, the XmlDoc's Namespace property must be On.
  • As of "Sirius Mods" Version 7.3, the AddNamespace method 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 with the XmlDoc API".
  • Namespace declarations are serialized in the order in which they are added.
  • Adding the same declaration multiple times on an element is allowed.
  • To ensure that URIs of already added nodes are preserved:
    • A namespace declaration may not be added to an Element that has the same prefix (including no prefix, in the case of the default namespace declaration) if the Element's URI differs from the AddNamespace URI.
    • A namespace declaration may not be added to an Element that has an attribute with the same prefix if the attribute's URI differs from the AddNamespace URI.
      • Prior to Version 7.7, a namespace declaration may not be added to an Element that has Element children. See the "Namespace conflict" example below.
      • Starting with version 7.7, this restriction does not apply when a non-default namespace declaration is being added to the top-level Element of the XmlDoc. See the "Add to top element" example below.
      • Starting with version 7.9, a default namespace declaration can be added to a prefixed Element if all of its Element children themselves have default namespace declarations. See the "Add default namespace" example below.
  • All well-formed XmlDocs implicitly include this namespace declaration in scope at the top-level document element:

    xmlns:xml="http://www.w3.org/XML/1998/namespace"

    The "xml" prefix is currently valid only for the special attributes xml:space and xml:lang. You are not allowed to:

    • Use any other "xml" prefixed element or attribute names
    • Add a namespace declaration for the "xml" prefix for a URI other than "http://www.w3.org/xml/1998/namespace";
    • Add a namespace declaration for "http://www.w3.org/xml/1998/namespace" that has any other prefix besides "xml"

Example

Add namespace to "leaf" element

In the following example, the AddNamespace method adds a namespace to a node that has an existing namespace. Subsequent elements are added to that node using each of the eligible namespace prefixes.

begin %doc is object xmlDoc %doc = new %doc:loadXml('<top - xmlns:SOAP="http://SCHEMAS.XMLSOAP.ORG/soap/"> - <a><SOAP:b></SOAP:b></a></top>') %n1 is object xmlNode %n2 is object xmlNode %n3 is object xmlNode %n1 = %doc:selectSingleNode('top/*/*') call %n1:addNamespace('sirius', 'http://sirius-software.com') print 'The URI of node SOAP:b is: ' %n1:URI %n2 = %n1:addElement('sirius:c') %n3 = %n1:addElement('SOAP:c') call %doc:print print 'The URI of node sirius:c is: ' %n2:URI print 'The URI of node SOAP:c is: ' %n3:URI end

The example result follows:

The URI of node SOAP:b is: http://SCHEMAS.XMLSOAP.ORG/soap/ <top xmlns:SOAP="http://SCHEMAS.XMLSOAP.ORG/soap/"> <a> <SOAP:b xmlns:sirius="http://sirius-software.com"> <sirius:c/> <SOAP:c/> </SOAP:b> </a> </top> The URI of node sirius:c is: http://sirius-software.com The URI of node SOAP:c is: http://SCHEMAS.XMLSOAP.ORG/soap/

When the method object Element node has children

As mentioned in the Usage notes above, URIs of already added nodes are preserved.

Namespace conflict

Here is an example which shows a AddNamespace which is disallowed to preserve a URI:

%top = %doc:AddElement('p1:top', , 'u:p1') %lev2 = %top:AddElement('p2:lev2', , 'u:p2') %lev2:AddElement('p1:lev3') %doc:Print %lev2:AddNamespace('p1', 'u:p3')

The result of the above fragment is:

<p1:top xmlns:p1="u:p1"> <p2:lev2 xmlns:p2="u:p2"> <p1:lev3/> </p2:lev2> </p1:top> *** 1 CANCELLING REQUEST: MSIR.0750: Class XmlNode, subroutine AddNamespace: can't expose descendants to new non-default namespace in line ...

If the namespace declaration xmlns:p1="u:p3" were simply inserted in the start tag of <p2:lev2...>, that would change the URI of element p1:lev3, and so it is prohibited.

Add to top element

Since the top element of an XmlDoc cannot "block" a namespace declaration, the general restriction against AddNamespace on an Element with child Elements was removed in Version 7.7, for the top element, when the namespace being added has a prefix:

%top = %doc:AddElement('top') %top:AddElement('p2:lev2', , 'u:p2') %top:AddNamespace('p1', 'u:px') %doc:Print

The result of the above fragment is:

<top xmlns:p1="u:px"> <p2:lev2 xmlns:p2="u:p2"/> </top>

Add default namespace

Since a default namespace declaration is "blocked" if all children of the Element have default namespace declarations, the general restriction against AddNamespace on an Element with child Elements was removed in version 7.7, if a default namespace is being added in such a situation:

%top = %doc:AddElement('p1:top', , 'u:p1') %top:AddElement('lev2', , 'u:x') %top:AddNamespace(, 'u:y') %doc:Print

The result of the above fragment is:

<p1:top xmlns:p1="u:p1" xmlns="u:y"> <lev2 xmlns="u:x"/> </p1:top>

Request-Cancellation Errors

  • nod does not point to an Element node.
  • Element pointed to by nod contains one or more child Element nodes.
  • prefix is an invalid prefix.
  • URI is an invalid URI.
  • prefix is the same as the prefix of the Element that nod points to, but URI is different from the namespace URI of the Element.
  • A declaration for the same prefix with a different URI is already present (not merely in scope) at the Element pointed to by nod.
  • The XmlDoc's Namespace property is not On.

See also