View on GitHub


Raku bindings to the libxml2 native library

[Raku LibXML Project] / [LibXML Module] / Attr :: Map

class LibXML::Attr::Map

LibXML Mapped Attributes


use LibXML::Attr::Map;
use LibXML::Document;
use LibXML::Element;
my LibXML::Document $doc .= parse('<foo att1="AAA" att2="BBB"/>');
my LibXML::Element $node = $doc.root;
my LibXML::Attr::Map $atts = $node.attributes;

# -- Associative Interface --
say $atts.keys.sort;  # att1 att2
say $atts<att1>.Str ; # AAA
say $atts<att1>.gist; # att1="AAA"
$atts<att3> = "CCC";
say $node.Str; # <foo att1="AAA" att3="CCC"/>

# -- DOM Interface --
$atts.setNamedItem('style', 'font-weight: bold');
my LibXML::Attr $style = $atts.getNamedItem('style');


This class is roughly equivalent to the W3C DOM NamedNodeMap (and the Perl XML::LibXML::NamedNodeMap class). This implementation currently limits their use to manipulation of an element’s attributes.

It presents a tied hash-like mapping of attributes to attribute names.

Updating Attributes

Attributes can be created, updated or deleted associatively:

my LibXML::Attr::Map $atts = $node.attributes;
$atts<style> = 'fontweight: bold';
my LibXML::Attr $style = $atts<style>;
$atts<style>:delete; # remove the style

There are also some DOM (NamedNodeMap) compatible methods:

my LibXML::Attr $style .= new: :name<style>, :value('fontweight: bold');
$style = $atts.getNamedItem('style');


keys, pairs, kv, elems, values, list, AT-KEY, ASSIGN-KEY, DELETE-KEY

Similar to the equivalent Raku Hash methods.

method setNamedItem

method setNamedItem(
    LibXML::Attr:D $att
) returns LibXML::Attr

Adds or replaces node with the same name as $att

method getNamedItem

method getNamedItem(
    Str:D $name where { ... }
) returns LibXML::Attr

Gets an attribute by name

method removeNamedItem

method removeNamedItem(
    Str:D $name where { ... }
) returns LibXML::Attr

Remove the item with the name $name

method setNamedItemNS

method setNamedItemNS(
    Str $uri,
    LibXML::Attr:D $att
) returns Mu

Assigns $att name space to $uri. Adds or replaces an attribute with the same as $att

method getNamedItemNS

method getNamedItemNS(
    Str $uri,
    Str:D $name where { ... }
) returns LibXML::Attr

Lookup attribute by namespace and name

$map.getNamedItemNS($uri,$name) is similar to $map{$uri}{$name}.

method removeNamedItemNS

method removeNamedItemNS(
    Str $uri,
    Str:D $name where { ... }
) returns LibXML::Attr

Lookup and remove attribute by namespace and name

$map.removeNamedItemNS($uri,$name) is similar to $map{$uri}{$name}:delete.

2001-2007, Ltd.

2002-2006, Christian Glahn.

2006-2009, Petr Pajas.


This program is free software; you can redistribute it and/or modify it under the terms of the Artistic License 2.0