XML::DOM::Attr(3)

πŸ’… XML::DOM::Attr β€” the vibe check on XML attributes

okay so bestie, welcome to XML::DOM::Attr, a page from the Perl User Contributed Perl Documentation section (that's man section 3, iykyk). This is literally the whole lore on what an attribute node is in XML::DOM, so let's get into it 🫢


🌸 NAME 🌸

XML::DOM::Attr β€” an XML attribute in XML::DOM. That's the whole name, that's the whole job. Main character energy from the jump πŸ‘‘


✨ DESCRIPTION ✨

XML::DOM::Attr extends XML::DOM::Node. So like... it's a Node, but make it attribute. πŸ’…

The Attr nodes that the XML::DOM::Parser builds always have one child node, and that child is a Text node holding the expanded string value β€” i.e. EntityReferences are always expanded, no exceptions, periodt. EntityReferences can still get added when you're modifying or creating a new Document, though. πŸŽ€

The Attr interface represents an attribute in an Element object. Typically the allowable values for the attribute are defined in a document type definition. (Very "your rules are in the DTD, not with me" of it.)

Attr objects inherit the Node interface, but here's the plot twist 😭 β€” since they're not actually child nodes of the element they describe, the DOM does not consider them part of the document tree. So the Node attributes parentNode, previousSibling, and nextSibling all have a undef value for Attr objects. Yeah. undef. All three of them. Don't come for me, that's just how the DOM sees it.

The DOM takes the view that attributes are properties of elements rather than having a separate identity from the elements they're associated with. Which honestly lowkey slaps, because it makes implementing stuff like default attributes associated with all elements of a given type more efficient. πŸ”₯

Furthermore β€” and this is important β€” Attr nodes may not be immediate children of a DocumentFragment. However, they can be associated with Element nodes contained within a DocumentFragment. In short: users and implementors of the DOM need to be aware that Attr nodes have some things in common with other objects inheriting the Node interface, but they also are quite distinct. It's giving "I'm in the group chat but I'm not like the other girls" πŸ’€

The attribute's effective value is determined as follows, and yes the order matters:

  1. if this attribute has been explicitly assigned any value, that value is the attribute's effective value;
  2. otherwise, if there is a declaration for this attribute, and that declaration includes a default value, then that default value is the attribute's effective value;
  3. otherwise, the attribute does not exist on this element in the structure model until it has been explicitly added.

Note that the nodeValue attribute on the Attr instance can also be used to retrieve the string version of the attribute's value(s). Handy, ngl. 🌈

In XML, where the value of an attribute can contain entity references, the child nodes of the Attr node provide a representation in which entity references are not expanded. These child nodes may be either Text or EntityReference nodes. And because the attribute type may be unknown, there are no tokenized attribute values. (No tokens, no cap. 🧚)


πŸŽ€ METHODS πŸŽ€

  • getValue β€” on retrieval, the value of the attribute is returned as a string. Character and general entity references are replaced with their values. Slay, it just hands you the actual string ✨
  • setValue (str) β€” DOM Spec: on setting, this creates a Text node with the unparsed contents of the string. Note the unparsed bestie, it doesn't get cute with it, it just makes the Text node. πŸ¦‹
  • getName β€” returns the name of this attribute. Short, sweet, iconic. πŸ‘‘

πŸ’€ reminder: attr nodes are not in the document tree, parentNode/previousSibling/nextSibling are undef, and they can't be immediate children of a DocumentFragment. Keep that in your notes app fr fr.

🌟 SEE ALSO 🌟

There's no SEE ALSO section on this page, bestie β€” this one's a standalone baddie. 😌


okay that's the whole page, you ate that reading πŸ’– β€” brought to you by perl v5.42.0, dated 2022-05-23, and this very manpage is XML::DOM::Attr(3). mwah, go forth and attribute responsibly 🫢🌸

πŸ“¦ where this page comes from