Class Attribute
Stay organized with collections
Save and categorize content based on your preferences.
Page Summary
-
An Attribute object represents an XML attribute and is used in conjunction with XmlService.
-
Attributes have methods to get and set their name, namespace, and value.
-
The provided example demonstrates how to parse XML, retrieve attributes, and add a new attribute with a combined value.
A representation of an XML attribute.
// Reads the first and last name of each person and adds a new attribute with // the full name. letxml='<roster>'+ '<person first="John" last="Doe"/>'+ '<person first="Mary" last="Smith"/>'+ '</roster>'; constdocument=XmlService.parse(xml); constpeople=document.getRootElement().getChildren('person'); for(leti=0;i < people.length;i++){ constperson=people[i]; constfirstName=person.getAttribute('first').getValue(); constlastName=person.getAttribute('last').getValue(); person.setAttribute('full',`${firstName}${lastName}`); } xml=XmlService.getPrettyFormat().format(document); Logger.log(xml);
Methods
| Method | Return type | Brief description |
|---|---|---|
get | String | Gets the local name of the attribute. |
get | Namespace|null | Gets the namespace for the attribute. |
get | String | Gets the value of the attribute. |
set | Attribute | Sets the local name of the attribute. |
set | Attribute | Sets the namespace for the attribute. |
set | Attribute | Sets the value of the attribute. |
Detailed documentation
getName()
Gets the local name of the attribute. If the attribute has a namespace prefix, use getNamespace() .getPrefix() to get the prefix.
Return
String — The local name of the attribute.
getNamespace()
getValue()
Gets the value of the attribute.
Return
String — The value of the attribute.
setName(name)
Sets the local name of the attribute. To set a namespace prefix for the attribute, use setNamespace(namespace) in conjunction with XmlService.getNamespace(prefix, uri) .
Parameters
| Name | Type | Description |
|---|---|---|
name | String | The local name to set. |
Return
Attribute — The attribute, for chaining.
setNamespace(namespace)
setValue(value)
Sets the value of the attribute.
Parameters
| Name | Type | Description |
|---|---|---|
value | String | The value to set. |
Return
Attribute — The attribute, for chaining.