setAttribute method
Set the attribute value with the given fully qualified name
to value
.
If an attribute with the name already exist, its value is updated.
If the value is null
, the attribute is removed.
Both name
and namespace
can be a specific String; or '*'
to match
anything. If no namespace
is provided, the fully qualified name is
compared; otherwise only the local name is considered.
For example:
element.setAttribute('xsd:name', 'value')
updates the attribute with the fully qualified attribute namexsd:name
.element.setAttribute('name', 'value', namespace: '*')
updates the attribute with the local attribute namename
no matter the namespace.element.setAttribute('*', 'value', namespace: 'http://www.w3.org/2001/XMLSchema')
updates the attribute within the provided namespace URI.
Implementation
@override
void setAttribute(String name, String? value, {String? namespace}) {
final index = attributes.indexWhere(createNameLookup(name, namespace));
if (index < 0) {
if (value != null) {
final prefix =
namespace == null ? null : lookupNamespacePrefix(this, namespace);
attributes.add(XmlAttribute(XmlName(name, prefix), value));
}
} else {
if (value != null) {
attributes[index].value = value;
} else {
attributes.removeAt(index);
}
}
}