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