SetAttribute Syntax 1
Description
Adds a predefined PBDOM_ATTRIBUTE object to a PBDOM_ELEMENT
object. Any existing attribute with the same name and namespace URI is
overwritten.
Syntax
|
1 |
pbdom_element_name.SetAttribute(pbdom_attribute pbdom_attribute_ref) |
|
Argument |
Description |
|---|---|
|
pbdom_element_name |
The name of a PBDOM_ELEMENT object |
|
pbdom_attribute_ref |
The PBDOM_ATTRIBUTE object to be set for this |
Return value
PBDOM_ELEMENT. The PBDOM_ELEMENT object modified to contain the
specified PBDOM_ATTRIBUTE.
Throws
EXCEPTION_INVALID_ARGUMENT — The input PBDOM_ATTRIBUTE is
invalid. This can happen if it has not been initialized properly or it
is a null object reference.
EXCEPTION_USE_OF_UNNAMED_PBDOM_OBJECT — The input
PBDOM_ATTRIBUTE has not been given a user-defined name.
EXCEPTION_PBDOM_OBJECT_ALREADY_HAS_OWNER — The input
PBDOM_ATTRIBUTE already has an owner element.
Examples
-
The SetAttribute method is invoked for the following
element:1<image></image>The SetAttribute method is invoked from the following
PowerScript code, where elem_image represents the image element
from the preceding XML:123attr_src.SetName("src")attr_src.SetValue("logo.gif")elem_image.SetAttribute(attr_src)The following XML results:
1<image src="logo.gif"></image> -
The following example demonstrates the impact of setting a
PBDOM_ATTRIBUTE for a PBDOM_ELEMENT object where the PBDOM_ELEMENT
object 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 sets 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 object 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 |
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 |
When saved and converted to an XML document, the document 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 |
Usage
This method allows the caller to add a predefined
PBDOM_ATTRIBUTE object to a PBDOM_ELEMENT object. If this
PBDOM_ELEMENT object already contains an existing attribute with the
same name and namespace URI as the input PBDOM_ATTRIBUTE, the existing
attribute is replaced by the input PBDOM_ATTRIBUTE.
If a PBDOM_ATTRIBUTE has been created to represent the original
attribute, it is still valid after the call, but the attribute that it
represents has been detached from the original owner element. Calling
GetOwnerElementObject on this PBDOM_ATTRIBUTE returns a null
value.
See also