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 2 3 4 5 6 7 8 9 10 11 12 |
<!-- http://www.w3.org is bound to n1 and n2 --> <x xmlns:n1="http://www.w3.org" xmlns:n2="http://www.w3.org" > <bad a="1" a="2" /> <bad n1:a="1" n2:a="2" /> </x> <!-- http://www.w3.org is bound to n1 and is the default --> <x xmlns:n1="http://www.w3.org" xmlns="http://www.w3.org" > <good a="1" b="2" /> <good a="1" n1:a="2" /> </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.Note
All 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 2 3 |
<root xmlns:pre1="http://www.pre.com" xmlns:pre2="http://www.pre.com"> <child1 pre1:a="123"/> </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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
PBDOM_BUILDER pbdom_buildr PBDOM_DOCUMENT pbdom_doc PBDOM_ATTRIBUTE pbdom_attr string strXML = "<root xmlns:pre1=~"http://www.pre.com~" xmlns:pre2=~"http://www.pre.com~"><child1 pre1:a=~"123~"/></root>" try pbdom_buildr = Create PBDOM_BUILDER pbdom_doc = pbdom_buildr.BuildFromString (strXML) // Create a PBDOM_ATTRIBUTE and set its properties pbdom_attr = Create PBDOM_ATTRIBUTE pbdom_attr.SetName ("a") pbdom_attr.SetNamespace ("pre2", & "http://www.pre.com", false) pbdom_attr.SetText("456") // Attempt to obtain the child1 element and // set the new attribute to it pbdom_doc.GetRootElement(). & GetChildElement("child1").SetAttribute(pbdom_attr) pbdom_doc.SaveDocument & ("pbdom_elem_set_attribute_1.xml") catch (PBDOM_EXCEPTION except) MessageBox ("PBDOM_EXCEPTION", except.GetMessage()) end try |
The XML output from SaveDocument looks like the following:
|
1 2 3 |
<root xmlns:pre1="http://www.pre.com" xmlns:pre2="http://www.pre.com"> <child1 pre2:a="456"/> </root> |