Setting the name and namespace of a PBDOM_ATTRIBUTE
The W3C “Namespaces in XML” specification (in section 5.3)
places restrictions on setting the name and namespace of a PBDOM_ATTRIBUTE. No
tag can contain two attributes with identical names, or with qualified
names that have the same local name and have prefixes that are bound
to identical namespace names.
The specification provides the following examples of illegal
and legal attributes:
1 |
<!-- http://www.w3.org is bound to n1 and n2 --><br><x xmlns:n1="http://www.w3.org" <br>   xmlns:n2="http://www.w3.org" ><br>   <bad a="1"  a="2" /><br>   <bad n1:a="1"  n2:a="2" /><br></x> |
1 |
<!-- http://www.w3.org is bound to n1 and is the default --><br><x xmlns:n1="http://www.w3.org" <br>   xmlns="http://www.w3.org" ><br>   <good a="1"  b="2" /><br>   <good a="1"  n1:a="2" /><br></x> |
In the first example, <bad a="1" a="2" />
violates
the rule that no tag can contain two attributes with identical names.
In the second tag, the attributes have the same local name but different
prefixes, so that their names are different. However, their prefixes
point to the same namespace URI, http://www.w3.org,
so it is illegal to place them inside the same owner element.
PBDOM scenarios
The following scenarios illustrate how PBDOM conforms to these requirements.
-
When the PBDOM_ATTRIBUTE SetName method
is invoked:If the PBDOM_ATTRIBUTE pbdom_attr1 has
an owner PBDOM_ELEMENT that contains an existing PBDOM_ATTRIBUTE with
the same name that is to be set for pbdom_attr1 and
has the same namespace URI as pbdom_attr1,
the EXCEPTION_INVALID_NAME exception is thrown. -
When the PBDOM_ATTRIBUTE SetNamespace method
is invoked:If the PBDOM_ATTRIBUTE pbdom_attr1 has
an owner PBDOM_ELEMENT that contains an existing PBDOM_ATTRIBUTE with
the same name as pbdom_attr1 and the
same namespace URI that is to be set for pbdom_attr1,
the EXCEPTION_INVALID_NAME exception is thrown. -
When the PBDOM_ELEMENT SetAttribute(pbdom_attribute pbdom_attribute_ref)
method is invoked:If the PBDOM_ELEMENT already contains an attribute
that has the same name and namespace URI as the input PBDOM_ATTRIBUTE,
the existing attribute is replaced by the input PBDOM_ATTRIBUTE.
The existing attribute is thus removed (detached) from the owner
element. -
When the PBDOM_ELEMENT SetAttributes(pbdom_attribute pbdom_attribute_array[])
method is invoked:If any two PBDOM_ATTRIBUTE objects in the array have
the same name and namespace URI, the EXCEPTION_INVALID_NAME exception
is thrown. If there is no name or namespace conflict within the array,
all the existing attributes of the PBDOM_ELEMENT are replaced by
the PBDOM_ATTRIBUTE objects in the array.NoteAll the above scenarios apply to PBDOM_ATTRIBUTE
objects that are contained in the NONAMESPACE namespace. -
When the PBDOM_ELEMENT SetAttribute(string strName, string strValue)
method is invoked:A new PBDOM_ATTRIBUTE with the specified name and
value is created and set into the PBDOM_ELEMENT. If the
PBDOM_ELEMENT already contains an attribute that has the
same name and that is contained within the NONAMESPACE namespace,
it is removed (detached) from the PBDOM_ELEMENT. -
When the PBDOM_ELEMENT SetAttribute(string strName, string strValue, string strNamespacePrefix, string strNamespaceUri, boolean bVerifyNamespace)
method is invoked:A new PBDOM_ATTRIBUTE with the specified name, value,
and namespace information is created and set into the PBDOM_ELEMENT.
If the PBDOM_ELEMENT already contains a PBDOM_ATTRIBUTE
that has the same name and namespace URI as the input namespace
URI, it is removed (detached) from the PBDOM_ELEMENT.
Example
The following example demonstrates the impact of
setting a PBDOM_ATTRIBUTE for a PBDOM_ELEMENT
where the PBDOM_ELEMENT already contains an attribute of
the same name and namespace URI as the input PBDOM_ATTRIBUTE.
The example creates a PBDOM_DOCUMENT based on the
following document:
1 |
<root xmlns:pre1="http://www.pre.com" xmlns:pre2="http://www.pre.com"><br>   <child1 pre1:a="123"/><br></root> |
Then it creates a PBDOM_ATTRIBUTE object and set
its name to a
and its prefix
and URI to pre2
and http://www.pre.com
.
The bVerifyNamespace argument is set to FALSE because
this PBDOM_ATTRIBUTE has not been assigned an owner PBDOM_ELEMENT
yet, so that the verification for a predeclared namespace would
fail. The text value is set to 456
.The child1 element
already contains an attribute named a that
belongs to the namespace http://www.pre.com,
as indicated by the prefix pre1. The new PBDOM_ATTRIBUTE
uses the prefix pre2, but it represents the
same namespace URI, so setting the new PBDOM_ATTRIBUTE
to child1 successfully replaces the existing pre1:a with
the new PBDOM_ATTRIBUTE pre2:a.
1 |
PBDOM_BUILDER pbdom_buildr<br>PBDOM_DOCUMENT pbdom_doc<br>PBDOM_ATTRIBUTE pbdom_attr<br> <br>string strXML = "<root xmlns:pre1=~"http://www.pre.com~" xmlns:pre2=~"http://www.pre.com~"><child1 pre1:a=~"123~"/></root>"<br> <br> |
1 |
try<br> <br>  pbdom_buildr = Create PBDOM_BUILDER<br>  pbdom_doc = pbdom_buildr.BuildFromString (strXML)<br>  <br>  // Create a PBDOM_ATTRIBUTE and set its properties<br>  pbdom_attr = Create PBDOM_ATTRIBUTE<br>  pbdom_attr.SetName ("a")<br>  pbdom_attr.SetNamespace ("pre2", &<br>     "http://www.pre.com", false)<br>  pbdom_attr.SetText("456")<br>  <br>  // Attempt to obtain the child1 element and <br>  // set the new attribute to it<br>  pbdom_doc.GetRootElement(). &<br>    GetChildElement("child1").SetAttribute(pbdom_attr)<br>  <br>  pbdom_doc.SaveDocument &<br>     ("pbdom_elem_set_attribute_1.xml")<br> <br>catch (PBDOM_EXCEPTION except)<br>  MessageBox ("PBDOM_EXCEPTION", except.GetMessage())<br>end try |
The XML output from SaveDocument looks
like the following :
1 |
<root xmlns:pre1="http://www.pre.com" xmlns:pre2="http://www.pre.com"><br>   <child1 pre2:a="456"/><br></root> |