PBDOM_ELEMENT:
SetAttribute Syntax 1 method
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 |
<span>pbdom_element_name.</span>SetAttribute(pbdom_attribute <span>pbdom_attribute_ref</span>) |
Argument |
Description |
---|---|
pbdom_element_name |
The name of a PBDOM_ELEMENT |
pbdom_attribute_ref |
The PBDOM_ATTRIBUTE object to |
Return Values
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:
1 |
attr_src.SetName("src")<br>attr_src.SetValue("logo.gif")<br>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 |
<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 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 |
PBDOM_BUILDER pbdom_buildr<br>PBDOM_DOCUMENT pbdom_doc<br>PBDOM_ATTRIBUTE pbdom_attr<br>string strXML = "<root xmlns:pre1=~"http://www.pre.com~" xmlns:pre2=~"http://www.pre.com~"><child1 pre1:a=~"123~"/></root>"<br> <br>try<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 |
When saved and converted to an XML document, the document
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 |
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.