forked from mvolkmann/mvolkmann.github.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwax_ruby.html
More file actions
445 lines (419 loc) · 10.9 KB
/
Copy pathwax_ruby.html
File metadata and controls
445 lines (419 loc) · 10.9 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR.html1/DTD.html1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>WAX for Ruby</title>
<link rel="stylesheet" type="text/css" href="../common.css"/>
</head>
<body>
<h2>WAX for Ruby</h2>
<h3>Links</h3>
<a href="WAX/rdoc/index.html">RDoc documentation</a>
<a href="http://rubyforge.org/projects/waxy/">RubyForge project</a>
<h3>Installing</h3>
<p>
Simply run "<code>gem install wax</code>".
You may need to precede this with "<code>sudo </code>" on non-Windows systems
in order to obtain the privilege necessary to do this.
</p>
<h3>Tutorial</h3>
<p>
This page provides many examples of using WAX for Ruby.
Each code snippet is followed by the output it produces.
</p>
<p>
When nothing is passed to the <code>WAX.write</code> method,
XML is written to <code>$stdout</code>.
There is also a <code>WAX.write</code> method
that takes an <code>IO</code> object.
</p>
<p>
Here's a simple example where only a <b>root element</b> is written:
</p>
<div class="code"><pre>
WAX.write { start 'car' }
</pre></div>
<div class="code"><pre>
<car/>
</pre></div>
<p>
Let's write a root element with some <b>text</b> inside:
</p>
<div class="code"><pre>
WAX.write do
start 'car'
text 'Prius'
end
</pre></div>
<div class="code"><pre>
<car>Prius</car>
</pre></div>
<p>
The <code>end!</code> method terminates the element
that is started by the <code>start</code> method.
In this case it's not necessary to call <code>end!</code>
because the <code>close</code> method
terminates all unterminated elements.
</p>
<p>
Let's put the text inside a <b>child element</b>:
</p>
<div class="code"><pre>
WAX.write do
start 'car'
start 'model'
text 'Prius'
end
</pre></div>
<div class="code"><pre>
<car>
<model>Prius</model>
</car>
</pre></div>
<p>
Let's do the same with the
<b><code>child</code> convenience method</b>:
which is equivalent to calling <code>start</code>,
<code>text</code> and <code>end</code>.
</p>
<div class="code"><pre>
WAX.write do
start 'car'
child 'model', 'Prius'
end
</pre></div>
<div class="code"><pre>
<car>
<model>Prius</model>
</car>
</pre></div>
<p>
Let's put text containing all the special XML characters
in a <b>CDATA section</b>:
</p>
<div class="code"><pre>
WAX.write do
start 'car'
start 'model'
cdata "1<2>3&4'5\"6"
end
</pre></div>
<div class="code"><pre>
<car>
<model>
<![CDATA[1<2>3&4'5"6]]>
</model>
</car>
</pre></div>
<p>
Let's output the XML without indentation, on a <b>single line</b>:
</p>
<div class="code"><pre>
WAX.write do
no_indents_or_line_separators
start 'car'
child 'model', 'Prius'
end
</pre></div>
<div class="code"><pre>
<car><model>Prius</model></car>
</pre></div>
<p>
Let's <b>indent</b> the XML with four spaces
instead of the default of two:
</p>
<div class="code"><pre>
WAX.write do
set_indent ' ' <span class="comment"># could also pass 4</span>
start 'car'
child 'model', 'Prius'
end
</pre></div>
<div class="code"><pre>
<car>
<model>Prius</model>
</car>
</pre></div>
<p>
Let's add an <b>attribute</b>:
</p>
<div class="code"><pre>
WAX.write do
start 'car'
attr 'year', 2008
child 'model', 'Prius'
end
</pre></div>
<div class="code"><pre>
<car year="2008">
<model>Prius</model>
</car>
</pre></div>
<p>
Attributes must be specified before
any content for their element is specified.
For example, calling
<code>start</code>, <code>attr</code> and <code>text</code> is valid,
but calling
<code>start</code>, <code>text</code> and <code>attr</code> is not.
If this rule is violated then a <code>RuntimeError</code> is raised.
</p>
<p>
Let's add an <b>XML declaration</b>:
</p>
<div class="code"><pre>
WAX.write($stdout, '1.0') do
start 'car'
attr 'year', 2008
child 'model', 'Prius'
end
</pre></div>
<div class="code"><pre>
<?xml version="1.0" encoding="UTF-8"?>
<car year="2008">
<model>Prius</model>
</car>
</pre></div>
<p>
Let's add a <b>comment</b>:
</p>
<div class="code"><pre>
WAX.write do
comment 'This is a hybrid car.'
start 'car'
child 'model', 'Prius'
end
</pre></div>
<div class="code"><pre><!-- This is a hybrid car. -->
<car>
<model>Prius</model>
</car>
</pre></div>
<p>
Let's add a <b>processing instruction</b>:
</p>
<div class="code"><pre>
WAX.write do
processing_instruction 'target', 'data'
start 'car'
attr 'year', 2008
child 'model', 'Prius'
end
</pre></div>
<div class="code"><pre><?target data?>
<car year="2008">
<model>Prius</model>
</car>
</pre></div>
<p>
Let's associate an <b>XSLT stylesheet</b> with the XML:
The <code>xslt</code> method is a convenience method for adding
this commonly used processing instruction.
</p>
<div class="code"><pre>
WAX.write do
xslt 'car.xslt'
start 'car'
attr 'year', 2008
child 'model', 'Prius'
end
</pre></div>
<div class="code"><pre>
<?xml-stylesheet type="text/xsl" href="car.xslt"?>
<car year="2008">
<model>Prius</model>
</car>
</pre></div>
<p>
Let's associate a <b>default namespace</b> with the XML:
</p>
<div class="code"><pre>
WAX.write do
xslt 'car.xslt'
start 'car'
attr 'year', 2008
namespace 'http://www.ociweb.com/cars'
child 'model', 'Prius'
end
</pre></div>
<div class="code"><pre>
<car year="2008"
xmlns="http://www.ociweb.com/cars">
<model>Prius</model>
</car>
</pre></div>
<p>
Let's associate a <b>non-default namespace</b> with the XML:
</p>
<div class="code"><pre>
prefix = 'c'
WAX.write do
start prefix, 'car'
attr 'year', 2008
namespace prefix, 'http://www.ociweb.com/cars'
child prefix, 'model', 'Prius'
end
</pre></div>
<div class="code"><pre>
<c:car year="2008"
xmlns:c="http://www.ociweb.com/cars">
<c:model>Prius</c:model>
</c:car>
</pre></div>
<p>
Like attributes, namespaces must be specified
before any content for their element is specified.
If this rule is violated then a <code>RuntimeError</code> is raised.
</p>
<p>
Let's associate an <b>XML Schema</b> with the XML:
</p>
<div class="code"><pre>
WAX.write do
start 'car'
attr 'year', 2008
namespace '', 'http://www.ociweb.com/cars', 'car.xsd'
child 'model', 'Prius'
end
</pre></div>
<div class="code"><pre>
<car year="2008"
xmlns="http://www.ociweb.com/cars"
xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
xsi:schemaLocation="http://www.ociweb.com/cars car.xsd">
<model>Prius</model>
</car>
</pre></div>
<p>
Let's associate <b>multiple XML Schemas</b> with the XML:
</p>
<div class="code"><pre>
WAX.write do
start 'car'
attr 'year', 2008
namespace '', 'http://www.ociweb.com/cars', 'car.xsd'
namespace 'm', 'http://www.ociweb.com/model', 'model.xsd'
child 'm', 'model', 'Prius'
end
</pre></div>
<div class="code"><pre>
<car year="2008"
xmlns="http://www.ociweb.com/cars"
xmlns:m="http://www.ociweb.com/model"
xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"
xsi:schemaLocation="http://www.ociweb.com/cars car.xsd
http://www.ociweb.com/model model.xsd">
<m:model>Prius</m:model>
</car>
</pre></div>
<p>
Let's associate a <b>DTD</b> with the XML:
</p>
<div class="code"><pre>
WAX.write do
dtd 'car.dtd'
start 'car'
attr 'year', 2008
child 'model', 'Prius'
end
</pre></div>
<div class="code"><pre>
<!DOCTYPE car SYSTEM "car.dtd">
<car year="2008">
<model>Prius</model>
</car>
</pre></div>
<p>
Let's add and use <b>entity definitions</b>:
</p>
<div class="code"><pre>
String url = 'http://www.ociweb.com/xml/';
WAX.write do
entity_def 'oci', 'Object Computing, Inc.'
external_entity_def 'moreData', url + 'moreData.xml'
start 'root'
text 'The author works at &oci; in St. Louis, Missouri.',
true, false <span class="comment"># turning escaping off for entity reference</span>
text '&moreData;', true, false
end
</pre></div>
<div class="code"><pre>
<!DOCTYPE root [
<!ENTITY oci "Object Computing, Inc.">
<!ENTITY moreData SYSTEM "http://www.ociweb.com/xml/moreData.xml">
]>
<root>
The author works at &oci; in St. Louis, Missouri.
&moreData;
</root>
</pre></div>
<p>
A <b>common usage pattern</b> is to pass a
<code>WAX</code> object to a method of model objects
that use it to write their XML representation.
A "chaining" style is employed here so that <code>self</code> refers to
the object being operated upon rather than a <code>WAX</code> object.
For example, a <code>Car</code> class could have the following method.
</p>
<div class="code"><pre>
def to_xml(wax)
wax.start('car').attr('year', @year).
child('make', @make).
child('model', @model).
end!
end
</pre></div>
<p>
An example of the XML this would produce follows:
</p>
<div class="code"><pre>
<car year="2008">
<make>Toyota</make>
<model>Prius</model>
</car>
</pre></div>
<p>
A <code>Person</code> class whose objects hold a reference to
an <code>Address</code> object could have the following method.
</p>
<div class="code"><pre>
def to_xml(wax)
wax.start('person').attr('birthdate', @birthdate).child('name', @name)
@address.to_xml(wax)
wax.end!
end
</pre></div>
<p>
The <code>Address</code> class could have the following method.
</p>
<div class="code"><pre>
def to_xml(wax)
wax.start('address').
child('street', @street).
child('city', @city).
child('state', @state).
child('zip', @zip).
end!
end
</pre></div>
<p>
An example of the XML this would produce follows:
</p>
<div class="code"><pre>
<person birthdate="4/16/1961">
<name>R. Mark Volkmann</name>
<address>
<street>123 Some Street</street>
<city>Some City</city>
<state>MO</state>
<zip>12345</zip>
</address>
</person>
</pre></div>
<hr style="clear:both"/>
<p style="text-align:center">
Copyright © 2008 Object Computing, Inc. All rights reserved.
</p>
</body>
</html>