DOMEntity.attributes

Returns a dynamic array of attributes for a start tag where each attribute is represented as a
$(PHOBOS_REF_ALTTEXT Tuple, Tuple, std, typecons)!( $(LREF2 SliceOfR, EntityRange), $(D_STRING "name"), $(LREF2 SliceOfR, EntityRange), $(D_STRING "value"), $(REF_ALTTEXT TextPos, TextPos, dxml, parser), $(D_STRING "pos")).

Supported EntityTypes:
elementStart
elementEmpty
struct DOMEntity(R)
@property
attributes
()

Examples

1 import std.algorithm.comparison : equal;
2 import std.algorithm.iteration : filter;
3 import std.range.primitives : empty;
4 import dxml.parser : TextPos;
5 
6 {
7     auto xml = "<root/>";
8     auto root = parseDOM(xml).children[0];
9     assert(root.type == EntityType.elementEmpty);
10     assert(root.attributes.empty);
11 
12     static assert(is(ElementType!(typeof(root.attributes)) ==
13                      typeof(root).Attribute));
14 }
15 {
16     auto xml = "<root a='42' q='29' w='hello'/>";
17     auto root = parseDOM(xml).children[0];
18     assert(root.type == EntityType.elementEmpty);
19 
20     auto attrs = root.attributes;
21     assert(attrs.length == 3);
22 
23     assert(attrs[0].name == "a");
24     assert(attrs[0].value == "42");
25     assert(attrs[0].pos == TextPos(1, 7));
26 
27     assert(attrs[1].name == "q");
28     assert(attrs[1].value == "29");
29     assert(attrs[1].pos == TextPos(1, 14));
30 
31     assert(attrs[2].name == "w");
32     assert(attrs[2].value == "hello");
33     assert(attrs[2].pos == TextPos(1, 21));
34 }
35 // Because the type of name and value is SliceOfR, == with a string
36 // only works if the range passed to parseXML was string.
37 {
38     auto xml = filter!"true"("<root a='42' q='29' w='hello'/>");
39     auto root = parseDOM(xml).children[0];
40     assert(root.type == EntityType.elementEmpty);
41 
42     auto attrs = root.attributes;
43     assert(attrs.length == 3);
44 
45     assert(equal(attrs[0].name, "a"));
46     assert(equal(attrs[0].value, "42"));
47     assert(attrs[0].pos == TextPos(1, 7));
48 
49     assert(equal(attrs[1].name, "q"));
50     assert(equal(attrs[1].value, "29"));
51     assert(attrs[1].pos == TextPos(1, 14));
52 
53     assert(equal(attrs[2].name, "w"));
54     assert(equal(attrs[2].value, "hello"));
55     assert(attrs[2].pos == TextPos(1, 21));
56 }

See Also

Meta