blob: 5e0b8a930bf93cde54570a72c30c648cba87e866 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
A terse introduction to E4X
Public Domain
The inline XML literals in this code are part of E4X, a standard
XML processing interface for ECMAScript. In addition to syntax
for XML literals, E4X provides a new kind of native object,
"xml", and a syntax, similar to XPath, for accessing and
modifying the tree. Here is a brief synopsis of the kind of
usage you'll see herein:
> let xml =
<foo bar="baz" baz="qux">
<bar>
<baz id="1"/>
</bar>
<baz id="2"/>
</foo>;
// Select all bar elements of the root foo element
> xml.bar
<bar><baz id="1"/></bar>
// Select all baz elements anywhere beneath the root
> xml..baz
<baz id="1"/>
<baz id="2"/>
// Select all of the immediate children of the root
> xml.*
<bar><baz id="1"/></bar>
<baz id="2"/>
// Select the bar attribute of the root node
> xml.@bar
baz
// Select all id attributes in the tree
> xml..@id
1
2
// Select all attributes of the root node
> xml.@*
baz
quz
// Add a quux elemend beneath the first baz
> xml..baz[0] += <quux/>
<baz id="1"/>
<quux/>
> xml
<foo bar="baz" baz="qux">
<bar>
<baz id="1"/>
<quux/>
</bar>
<baz id="2"/>
</foo>
// and beneath the second
> xml.baz[1] = <quux id="1"/>
> xml
<foo bar="baz" baz="qux">
<bar>
<baz id="1"/>
<quux/>
</bar>
<baz id="2"/>
<quux id="1"/>
</foo>
// Replace bar's subtree with a foo element
> xml.bar.* = <foo id="1"/>
> xml
<foo bar="baz" baz="qux">
<bar>
<foo id="1"/>
</bar>
<baz id="2"/>
<quux id="1"/>
</foo>
// Add a bar below bar
> xml.bar.* += <bar id="1"/>
<foo id="1"/>
<bar id="1"/>
> xml
<foo bar="baz" baz="qux">
<bar>
<foo id="1"/>
<bar id="1"/>
</bar>
<baz id="2"/>
<quux id="1"/>
</foo>
// Adding a quux attribute to the root
> xml.@quux = "foo"
foo
> xml
<foo bar="baz" baz="qux" quux="foo">
<bar>
<foo id="1"/>
<bar id="1"/>
</bar>
<baz id="2"/>
<quux id="1"/>
</foo>
> xml.bar.@id = "0"
> xml..foo[0] = "Foo"
Foo
> xml..bar[1] = "Bar"
Bar
> xml
js> xml
<foo bar="baz" baz="qux" quux="foo" id="0">
<bar id="0">
<foo id="1">Foo</foo>
<bar id="1">Bar</bar>
</bar>
<baz id="2"/>
<quux id="1"/>
</foo>
// Selecting all bar elements where id="1"
> xml..bar.(@id == 1)
Bar
// Literals:
// XMLList literal. No root node.
> <>Foo<br/>Baz</>
Foo
<br/>
Baz
// Interpolation.
> let x = "<foo/>"
> <foo bar={x}>{x + "<?>"}</foo>
<foo/><?>
> <foo bar={x}>{x + "<?>"}</foo>.toXMLString()
<foo bar="<foo/>"><foo/><?></foo>
> let x = <foo/>
> <foo bar={x}>{x}</foo>.toXMLString()
<foo bar="">
<foo/>
</foo>
|