0
|
1 |
include homepage.en.yhtml2
|
|
2 |
|
|
3 |
page "YSLT – XSLT C style" {
|
|
4 |
h1 id=intro > YSLT – an introduction
|
|
5 |
|
|
6 |
p >>
|
|
7 |
Especially the ¬http://www.w3.org/TR/xslt XSLT¬ programmer can benefit from YML.
|
|
8 |
Usually many attributes of XSLT are annoying in programming praxis:
|
|
9 |
>>
|
|
10 |
|
|
11 |
ul {
|
|
12 |
li >>
|
|
13 |
the missing separation of ¬http://en.wikipedia.org/wiki/Indent_style indention¬
|
|
14 |
of the XSLT program and indention of the output text
|
|
15 |
>>
|
|
16 |
|
|
17 |
li > the complicated syntax
|
|
18 |
|
|
19 |
li > the lack of good text escaping mechanisms (< and > are not seldom)
|
|
20 |
|
|
21 |
li > ...
|
|
22 |
}
|
|
23 |
|
|
24 |
p > In short, it's ugly ;-)
|
|
25 |
|
|
26 |
p >>
|
|
27 |
Usually the result is, that many programmers are avoiding XSLT as a programming language.
|
|
28 |
It's a pity, because for processing XML data, it can do much more than “formatting” as
|
|
29 |
“stylesheets”.
|
|
30 |
>>
|
|
31 |
|
|
32 |
p >>
|
|
33 |
The idea of YSLT now is to supply a simple language, which programmers want to use, to
|
|
34 |
make the features of XSLT accessible to a broader base of people.
|
|
35 |
>>
|
|
36 |
|
|
37 |
p >>
|
|
38 |
YSLT can be used much simpler; it's just a ¬http://fdik.org/yml/index#ylanguages Y Language¬
|
|
39 |
for XSLT. I'm using it for my blog, here you can ¬http://fdik.org/yblog2.tar.bz2 download YBlog2¬,
|
|
40 |
a simple blogging software in YSLT.
|
|
41 |
>>
|
|
42 |
|
|
43 |
p > Here you can find the ¬yslt.yml2 YSLT specification¬.
|
|
44 |
|
|
45 |
h2 id=hello > Hello, World
|
|
46 |
|
|
47 |
p > In YSLT, the hello world program reads like this:
|
|
48 |
|
|
49 |
Code ||
|
|
50 |
include yslt.yml2
|
|
51 |
textstylesheet template "/" | hello, world
|
|
52 |
||
|
|
53 |
|
|
54 |
p >>
|
|
55 |
The `a href="http://fdik.org/yml/features#including" code > include` line includes the
|
|
56 |
YSLT Y Language declarations. The second line generates an XSLT hello world program.
|
|
57 |
You can generate it using:
|
|
58 |
>>
|
|
59 |
|
|
60 |
Code | % yml2c -o hello.xsl hello.ysl2
|
|
61 |
|
|
62 |
p > This results in the following program:
|
|
63 |
|
|
64 |
Code ||
|
|
65 |
<xsl:stylesheet xmlns:func="http://exslt.org/functions"
|
|
66 |
xmlns:dyn="http://exslt.org/dynamic" xmlns:str="http://exslt.org/strings"
|
|
67 |
xmlns:math="http://exslt.org/math"
|
|
68 |
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
|
|
69 |
extension-element-prefixes="exsl func str dyn set math"
|
|
70 |
xmlns:set="http://exslt.org/sets" version="1.0"
|
|
71 |
xmlns:exsl="http://exslt.org/common"><xsl:output method="text"/>
|
|
72 |
<xsl:variable name="space" select="' '"/>
|
|
73 |
<xsl:param name="autoindent" select="4"/><xsl:template match="/">
|
|
74 |
<xsl:param name="_indent" select="0"/><xsl:value-of
|
|
75 |
select="substring($space, 1, $_indent+0*$autoindent)"/>hello, world
|
|
76 |
</xsl:template></xsl:stylesheet>
|
|
77 |
||
|
|
78 |
|
|
79 |
p >>
|
|
80 |
You can execute this program with any
|
|
81 |
¬http://en.wikipedia.org/wiki/XML_template_engine XSL processor¬, for example the Free Software
|
|
82 |
¬http://xmlsoft.org/XSLT/xsltproc2.html xsltproc¬.
|
|
83 |
>>
|
|
84 |
|
|
85 |
Code ||
|
|
86 |
% yml2c -o hello.xsl hello.ysl2
|
|
87 |
% echo '<empty/>' > empty.xml
|
|
88 |
% xsltproc hello.xsl empty.xml
|
|
89 |
hello, world
|
|
90 |
% _
|
|
91 |
||
|
|
92 |
|
|
93 |
p >>
|
|
94 |
Or you can just use ¬toolchain#processor yml2proc¬:
|
|
95 |
>>
|
|
96 |
|
|
97 |
Code ||
|
|
98 |
% yml2proc -My hello.ysl2
|
|
99 |
hello, world
|
|
100 |
% _
|
|
101 |
||
|
|
102 |
|
|
103 |
h2 id=programming > Programming in YSLT
|
|
104 |
|
|
105 |
p >>
|
|
106 |
Because YSLT is just a ¬index#ylanguages Y Language¬ for XSLT, you can do anything with YSLT
|
|
107 |
you can do with XSLT in just nicer syntax ;-) To read what XSLT is all about, I recommend
|
|
108 |
¬http://www.w3.org/TR/xslt the official W3C documentation for XSLT¬.
|
|
109 |
>>
|
|
110 |
|
|
111 |
p >>
|
|
112 |
So this document is just an beginners guide, if you want to understand XSLT completely,
|
|
113 |
better read the ¬http://www.w3.org W3C¬ stuff.
|
|
114 |
>>
|
|
115 |
|
|
116 |
h3 > Programming YSLT means programming with a pure functional language.
|
|
117 |
|
|
118 |
p >>
|
|
119 |
Lovers of ¬http://en.wikipedia.org/wiki/Lisp_(programming_language) Lisp¬ or
|
|
120 |
¬http://www.haskell.org/ Haskell¬ will not see any problems. But many programmers are used to
|
|
121 |
have a programming language with a
|
|
122 |
¬http://en.wikipedia.org/wiki/Procedural_programming procedural imperative paradigma¬ like
|
|
123 |
¬http://java.sun.com Java¬,
|
|
124 |
¬http://en.wikipedia.org/wiki/C_(programming_language) C¬/¬http://en.wikipedia.org/wiki/C++ C++¬
|
|
125 |
or ¬http://www.python.org Python¬. Why should they use a
|
|
126 |
¬http://en.wikipedia.org/wiki/Functional_programming functional¬ language?
|
|
127 |
>>
|
|
128 |
|
|
129 |
p >>
|
|
130 |
Actually, if a functional language is practical or not, depends of what's to do – “the
|
|
131 |
right tool for the task”, one could say.
|
|
132 |
>>
|
|
133 |
|
|
134 |
p >>
|
|
135 |
Because processing XML data means traversing a (document) tree,
|
|
136 |
¬http://en.wikipedia.org/wiki/Recursion recursive¬ algorithms are a clear advantage.
|
|
137 |
And this is the reason, why choosing a functional language is a good choice for that job.
|
|
138 |
>>
|
|
139 |
|
|
140 |
p >>
|
|
141 |
It's a little bit like with ¬http://en.wikipedia.org/wiki/SQL SQL¬ – for it's job, it's
|
|
142 |
excellent, but no-one wants to write a complete application in SQL (and also not in one
|
|
143 |
of the Turing-complete SQL extension languages like
|
|
144 |
¬http://www.oracle.com/technology/tech/pl_sql/index.html PL/SQL¬).
|
|
145 |
>>
|
|
146 |
|
|
147 |
h3 id=htmlgen > Generating HTML out of a DSL
|
|
148 |
|
|
149 |
p >>
|
|
150 |
Of course, you can use YSLT as you would use XSLT. Let's say, you have data in XML
|
|
151 |
documents, and you want to have an excerpt out of this data. This is a common task,
|
|
152 |
comparable to «SELECT» data out of an SQL database. But we have no database, we have
|
|
153 |
something like this file customers.yml2:
|
|
154 |
>>
|
|
155 |
|
|
156 |
Code ||
|
|
157 |
decl customer(*id, *name) { id *id, name *name };
|
|
158 |
|
|
159 |
list {
|
|
160 |
customer 23, "Kurt Meier";
|
|
161 |
customer 42, "Lieschen Schmidt";
|
|
162 |
}
|
|
163 |
||
|
|
164 |
|
|
165 |
p >>
|
|
166 |
Let's say, we want to output this into an ¬http://en.wikipedia.org/wiki/XHTML XHTML¬ document,
|
|
167 |
where the list is showed as a table. Then we would do the following:
|
|
168 |
>>
|
|
169 |
|
|
170 |
p > The XHTML document should be like the following and include the list in the body:
|
|
171 |
|
|
172 |
Code ||
|
|
173 |
include yslt.yml2
|
|
174 |
|
|
175 |
stylesheet {
|
|
176 |
template "/" html {
|
|
177 |
head title "Customer List";
|
|
178 |
body apply "list";
|
|
179 |
}
|
|
180 |
||
|
|
181 |
|
|
182 |
p >>
|
|
183 |
In the example above, stylesheet declares the main program. Then we define a template, which
|
|
184 |
is executed automatically starting reading the root «'/'» of the document tree of the XML
|
|
185 |
document we're processing.
|
|
186 |
>>
|
|
187 |
|
|
188 |
p > Using the XML document as input, we're creating this HTML tree:
|
|
189 |
|
|
190 |
Code ||
|
|
191 |
<html>
|
|
192 |
<head>
|
|
193 |
<title>Customer List</title>
|
|
194 |
</head>
|
|
195 |
<body>
|
|
196 |
|
|
197 |
<!-- ... ->
|
|
198 |
|
|
199 |
</body>
|
|
200 |
</html>
|
|
201 |
||
|
|
202 |
|
|
203 |
p > How do we create the list? Well, let's use a table with customers:
|
|
204 |
|
|
205 |
Code | template "list" table apply "customer";
|
|
206 |
|
|
207 |
p >>
|
|
208 |
What to do per customer? Well, generate a table row with two columns, one for «id» and one
|
|
209 |
for «name»:
|
|
210 |
>>
|
|
211 |
|
|
212 |
Code ||
|
|
213 |
template "customer" tr {
|
|
214 |
td value "id";
|
|
215 |
td value "name";
|
|
216 |
}
|
|
217 |
}
|
|
218 |
||
|
|
219 |
|
|
220 |
p > That was it. We now can run our small program:
|
|
221 |
|
|
222 |
Code | % yml2proc -y customer.ysl2 -x customer.xml -o customer.html -P
|
|
223 |
|
|
224 |
p > The result looks like this:
|
|
225 |
|
|
226 |
Code ||
|
|
227 |
% cat customer.html
|
|
228 |
<?xml version="1.0"?>
|
|
229 |
<html>
|
|
230 |
<head>
|
|
231 |
<title>Customer List</title>
|
|
232 |
</head>
|
|
233 |
<body>
|
|
234 |
<table>
|
|
235 |
<tr>
|
|
236 |
<td>23</td>
|
|
237 |
<td>Kurt Meier</td>
|
|
238 |
</tr>
|
|
239 |
<tr>
|
|
240 |
<td>42</td>
|
|
241 |
<td>Lieschen Schmidt</td>
|
|
242 |
</tr>
|
|
243 |
</table>
|
|
244 |
</body>
|
|
245 |
</html>
|
|
246 |
% _
|
|
247 |
||
|
|
248 |
|
|
249 |
p > Here you can download ¬samples/customer.ysl2 the complete program¬.
|
|
250 |
|
|
251 |
h3 id=codegen > How to generate code with YSLT
|
|
252 |
|
|
253 |
p > Generating code is easy with YSLT, if you follow these steps:
|
|
254 |
|
|
255 |
ol {
|
|
256 |
li > design the software you want to generate using patterns and write that in a DSL
|
|
257 |
li > write target code once for each pattern
|
|
258 |
li > deconstruct a compiler for the target code for each pattern
|
|
259 |
li > fine tune until the diff is empty
|
|
260 |
}
|
|
261 |
|
|
262 |
p >>
|
|
263 |
Let's test by example. First let's say, we have a pattern of entities we want
|
|
264 |
to implement as Java beans. The enities in our DSL are:
|
|
265 |
>>
|
|
266 |
|
|
267 |
Code ||
|
|
268 |
decl entity +name;
|
|
269 |
decl attr +type +name;
|
|
270 |
decl aggregates +entity;
|
|
271 |
|
|
272 |
structure {
|
|
273 |
entity Customer {
|
|
274 |
attr String name;
|
|
275 |
attr int creditLimit;
|
|
276 |
aggregates Order;
|
|
277 |
}
|
|
278 |
|
|
279 |
entity Order {
|
|
280 |
attr String no;
|
|
281 |
attr String description;
|
|
282 |
attr int amount;
|
|
283 |
}
|
|
284 |
}
|
|
285 |
||
|
|
286 |
|
|
287 |
p >>
|
|
288 |
How to write that in a Java Program? Well, following the second step, we're
|
|
289 |
writing our target code manually; for this simple sample, let's be naive and
|
|
290 |
save the following into a file named Customer.java.target:
|
|
291 |
>>
|
|
292 |
|
|
293 |
Code ||
|
|
294 |
import java.util.Vector;
|
|
295 |
import java.util.Collections;
|
|
296 |
import base.Entity;
|
|
297 |
|
|
298 |
class Customer extends Entity {
|
|
299 |
Customer {
|
|
300 |
id = genId();
|
|
301 |
}
|
|
302 |
|
|
303 |
// attribute name
|
|
304 |
|
|
305 |
private String name;
|
|
306 |
public String getName() {
|
|
307 |
return name;
|
|
308 |
}
|
|
309 |
public void setName(String value) {
|
|
310 |
name = value;
|
|
311 |
}
|
|
312 |
|
|
313 |
// attribute creditLimit
|
|
314 |
|
|
315 |
private int creditLimit;
|
|
316 |
public int getCreditLimit() {
|
|
317 |
return creditLimit;
|
|
318 |
}
|
|
319 |
public void setCreditLimit(int value) {
|
|
320 |
creditLimit = value;
|
|
321 |
}
|
|
322 |
|
|
323 |
// Order aggregation
|
|
324 |
|
|
325 |
protected Vector orderList = new Vector();
|
|
326 |
void addOrder(Order entity) {
|
|
327 |
orderList.add(entity);
|
|
328 |
}
|
|
329 |
void removeOrder(Order entity) {
|
|
330 |
orderList.remove(entity);
|
|
331 |
}
|
|
332 |
Iterator orderIterator() {
|
|
333 |
return orderList.iterator();
|
|
334 |
}
|
|
335 |
}
|
|
336 |
||
|
|
337 |
|
|
338 |
p >>
|
|
339 |
The third step does most of the work. First we cite this code
|
|
340 |
into gen_entity.ysl2 and create the basic form of an YSLT script:
|
|
341 |
>>
|
|
342 |
|
|
343 |
Code ||
|
|
344 |
include yslt.yml2
|
|
345 |
|
|
346 |
tstylesheet {
|
|
347 |
template "/" {
|
|
348 |
| import java.util.Vector;
|
|
349 |
| import java.util.Collections;
|
|
350 |
| import base.Entity;
|
|
351 |
|
|
|
352 |
| class Customer extends Entity {
|
|
353 |
| Customer {
|
|
354 |
| id = genId();
|
|
355 |
| }
|
|
356 |
|
|
|
357 |
| // attribute name
|
|
358 |
|
|
|
359 |
| private String name;
|
|
360 |
| public String getName() {
|
|
361 |
| return name;
|
|
362 |
| }
|
|
363 |
| public void setName(String value) {
|
|
364 |
| name = value;
|
|
365 |
| }
|
|
366 |
|
|
|
367 |
| // attribute creditLimit
|
|
368 |
|
|
|
369 |
| private int creditLimit;
|
|
370 |
| public int getCreditLimit() {
|
|
371 |
| return creditLimit;
|
|
372 |
| }
|
|
373 |
| public void setCreditLimit(int value) {
|
|
374 |
| creditLimit = value;
|
|
375 |
| }
|
|
376 |
|
|
|
377 |
| // Order aggregation
|
|
378 |
|
|
|
379 |
| protected Vector orderList = new Vector();
|
|
380 |
| void addOrder(Order entity) {
|
|
381 |
| orderList.add(entity);
|
|
382 |
| }
|
|
383 |
| void removeOrder(Order entity) {
|
|
384 |
| orderList.remove(entity);
|
|
385 |
| }
|
|
386 |
| Iterator orderIterator() {
|
|
387 |
| return orderList.iterator();
|
|
388 |
| }
|
|
389 |
| }
|
|
390 |
}
|
|
391 |
}
|
|
392 |
||
|
|
393 |
|
|
394 |
p >>
|
|
395 |
Now for the deconstruction. I think, it'll be best, if we
|
|
396 |
¬#edocument create a .java file for each entity¬.
|
|
397 |
So we're moving the whole thing into a template for each entity
|
|
398 |
creating a file, and we're applying this for each entity using the name of each
|
|
399 |
Entity as parameter. We're adding some distinction of cases, too.
|
|
400 |
>>
|
|
401 |
|
|
402 |
p >>
|
|
403 |
When we apply, the indention system of YSLT will add an indention level, so we can
|
|
404 |
take out rendundant whitespace; for the first apply we don't want this, so we're
|
|
405 |
giving the number 0 as the indention level.
|
|
406 |
>>
|
|
407 |
|
|
408 |
p >>
|
|
409 |
In attributes, braces «{…}» let us insert the value of an XPath expression into our
|
|
410 |
DSL, while inside quoted text the same is done by the ¬#angledouble angle double quotes¬
|
|
411 |
`] <code>«…»</code>`:
|
|
412 |
>>
|
|
413 |
|
|
414 |
Code ||
|
|
415 |
include yslt.yml2
|
|
416 |
|
|
417 |
tstylesheet {
|
|
418 |
template "/structure" apply "Entity", 0;
|
|
419 |
|
|
420 |
template "Entity" document "{@name}.java" {
|
|
421 |
if "aggregates" {
|
|
422 |
| import java.util.Vector;
|
|
423 |
| import java.util.Collections;
|
|
424 |
}
|
|
425 |
|
|
426 |
| import base.Entity;
|
|
427 |
|
|
|
428 |
| class `] «`@name`] »` extends Entity {
|
|
429 |
| `] «`@name`] »` {
|
|
430 |
| id = genId();
|
|
431 |
| }
|
|
432 |
|
|
|
433 |
[...]
|
|
434 |
| orderList.remove(entity);
|
|
435 |
| }
|
|
436 |
| Iterator orderIterator() {
|
|
437 |
| return orderList.iterator();
|
|
438 |
| }
|
|
439 |
| }
|
|
440 |
}
|
|
441 |
}
|
|
442 |
||
|
|
443 |
|
|
444 |
p >>
|
|
445 |
Well, not bad. Now for the pattern of an attribute and an aggregation, respectively.
|
|
446 |
>>
|
|
447 |
|
|
448 |
Code ||
|
|
449 |
include yslt.yml2
|
|
450 |
|
|
451 |
tstylesheet {
|
|
452 |
template "/structure" apply "Entity";
|
|
453 |
|
|
454 |
template "Entity" document "{@name}.java" {
|
|
455 |
if "aggregates" {
|
|
456 |
| import java.util.Vector;
|
|
457 |
| import java.util.Collections;
|
|
458 |
}
|
|
459 |
|
|
460 |
| import base.Entity;
|
|
461 |
|
|
|
462 |
| class `] «`@name`] »` extends Entity {
|
|
463 |
| `] «`@name`] »` {
|
|
464 |
| id = genId();
|
|
465 |
| }
|
|
466 |
|
|
|
467 |
|
|
468 |
apply "attr|aggregates";
|
|
469 |
|
|
470 |
| }
|
|
471 |
}
|
|
472 |
|
|
473 |
template "attr" {
|
|
474 |
|
|
|
475 |
| // attribute `] «`@name`] »`
|
|
476 |
|
|
|
477 |
| private `] «`@type`] »` `] «`@name`] »`;
|
|
478 |
| public `] «`@type`] »` get`] «`@name`] »`() {
|
|
479 |
| return `] «`@name`] »`;
|
|
480 |
| }
|
|
481 |
| public void set`] «`@name`] »`(`] «`@type`] »` value) {
|
|
482 |
| `] «`@name`] »` = value;
|
|
483 |
| }
|
|
484 |
}
|
|
485 |
|
|
486 |
template "aggregates" {
|
|
487 |
|
|
|
488 |
| // `] «`@entity`] »` aggregation
|
|
489 |
|
|
|
490 |
| protected Vector `] «`@entity`] »`List = new Vector();
|
|
491 |
| void add`] «`@entity`] »`(`] «`@entity`] »` entity) {
|
|
492 |
| `] «`@entity`] »`List.add(entity);
|
|
493 |
| }
|
|
494 |
| void remove`] «`@entity`] »`(`] «`@entity`] »` entity) {
|
|
495 |
| `] «`@entity`] »`List.remove(entity);
|
|
496 |
| }
|
|
497 |
| Iterator `] «`@entity`] »`Iterator() {
|
|
498 |
| return `] «`@entity`] »`List.iterator();
|
|
499 |
| }
|
|
500 |
}
|
|
501 |
}
|
|
502 |
||
|
|
503 |
|
|
504 |
p >>
|
|
505 |
As you can see, we're deconstructing step by step. This is a good idea to get
|
|
506 |
into code generation with YSLT, but it remains a good idea even for the advanced
|
|
507 |
programmer: it keeps a clear view on what's happening.
|
|
508 |
>>
|
|
509 |
|
|
510 |
p >>
|
|
511 |
In the last step, test it out and make a diff to your target code. You will see
|
|
512 |
that our example needs some beautifying: in Java, camel case is important and
|
|
513 |
makes you some work to revert characters to uppercase or lowercase, respectively.
|
|
514 |
For that work you'll see that ¬http://www.w3.org/TR/xpath/#function-translate XPath's translate() function¬ is a little bit ugly ;-)
|
|
515 |
So we're defining an ¬features#userop operator¬ for that at the top of the file:
|
|
516 |
>>
|
|
517 |
|
|
518 |
Code ||
|
|
519 |
define operator "“(.*?)”" as call "ucase" with "text", "%1";
|
|
520 |
||
|
|
521 |
|
|
522 |
p >>
|
|
523 |
Inside the template, we're defining the ucase function:
|
|
524 |
>>
|
|
525 |
|
|
526 |
Code ||
|
|
527 |
function "ucase" {
|
|
528 |
param "text";
|
|
529 |
|
|
530 |
value "translate(substring($text,1,1),'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')";
|
|
531 |
value "substring($text, 2)";
|
|
532 |
}
|
|
533 |
}
|
|
534 |
||
|
|
535 |
|
|
536 |
p >>
|
|
537 |
Now we can replace one quoting with another; have a look at the getter and setter
|
|
538 |
methods:
|
|
539 |
>>
|
|
540 |
|
|
541 |
Code ||
|
|
542 |
include yslt.yml2
|
|
543 |
|
|
544 |
define operator "“(.*?)”" as call "ucase" with "text", "%1";
|
|
545 |
|
|
546 |
tstylesheet {
|
|
547 |
template "/structure" apply "Entity";
|
|
548 |
|
|
549 |
[...]
|
|
550 |
|
|
551 |
template "attr" {
|
|
552 |
|
|
|
553 |
| // attribute `] «`@name`] »`
|
|
554 |
|
|
|
555 |
| private `] «`@type`] »` `] «`@name`] »`;
|
|
556 |
| public `] «`@type`] »` get“@name”() {
|
|
557 |
| return `] «`@name`] »`;
|
|
558 |
| }
|
|
559 |
| public void set“@name”(`] «`@type`] »` value) {
|
|
560 |
| `] «`@name`] »` = value;
|
|
561 |
| }
|
|
562 |
}
|
|
563 |
|
|
564 |
[...]
|
|
565 |
|
|
566 |
function "ucase" {
|
|
567 |
param "text";
|
|
568 |
|
|
569 |
value "translate(substring($text,1,1),'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')";
|
|
570 |
value "substring($text, 2)";
|
|
571 |
}
|
|
572 |
}
|
|
573 |
||
|
|
574 |
|
|
575 |
p >>
|
|
576 |
Well, the rest is a pure laborious task ;-) Feel free to complete. And: use diff!
|
|
577 |
>>
|
|
578 |
|
|
579 |
h3 id=ddlgen > A more advanced example: generating SQL DDL out of an UML diagram
|
|
580 |
|
|
581 |
p >>
|
|
582 |
Well, now for something real ;-) This is a very common task: somebody models
|
|
583 |
with an ¬http://en.wikipedia.org/wiki/Class_diagram UML class diagram¬, and you want
|
|
584 |
to have SQL ¬http://en.wikipedia.org/wiki/Data_Definition_Language DDL¬, which generates
|
|
585 |
a matching database structure.
|
|
586 |
>>
|
|
587 |
|
|
588 |
p > Let's go:
|
|
589 |
|
|
590 |
p >>
|
|
591 |
First, lets use a stylesheet, which declares the needed
|
|
592 |
¬http://www.w3.org/TR/REC-xml-names/ XMI namespaces¬:
|
|
593 |
>>
|
|
594 |
|
|
595 |
Code ||
|
|
596 |
include yslt.yml2
|
|
597 |
|
|
598 |
tstylesheet xmlns:uml="http://schema.omg.org/spec/UML/2.1",
|
|
599 |
xmlns:xmi="http://schema.omg.org/spec/XMI/2.1" {
|
|
600 |
||
|
|
601 |
|
|
602 |
p > Now, search the Model for all content:
|
|
603 |
|
|
604 |
Code | template "/" apply "xmi:XMI/uml:Model/packagedElement", 0;
|
|
605 |
|
|
606 |
p >>
|
|
607 |
We're translating ¬http://en.wikipedia.org/wiki/Package_(UML) UML Packages¬ into
|
|
608 |
underscore separated prefixes:
|
|
609 |
>>
|
|
610 |
|
|
611 |
Code ||
|
|
612 |
template "packagedElement[@xmi:type='uml:Package']" {
|
|
613 |
param "name", "''";
|
|
614 |
if "$name=''" apply "packagedElement", 0 { with "name", "@name"; }
|
|
615 |
if "$name!=''" apply "packagedElement", 0 { with "name", "concat($name, '_', @name)"; }
|
|
616 |
}
|
|
617 |
|
|
618 |
||
|
|
619 |
|
|
620 |
p > Each Class is represented by a table in the database:
|
|
621 |
|
|
622 |
Code ||
|
|
623 |
template "packagedElement[@xmi:type='uml:Class']" {
|
|
624 |
param "name";
|
|
625 |
|
|
626 |
| CREATE TABLE «$name»_«@name» (
|
|
627 |
apply "ownedAttribute";
|
|
628 |
| );
|
|
629 |
}
|
|
630 |
||
|
|
631 |
|
|
632 |
p >>
|
|
633 |
Finally, for each different data type for an attribute we're outputting different fields
|
|
634 |
with different types:
|
|
635 |
>>
|
|
636 |
|
|
637 |
Code ||
|
|
638 |
template "ownedAttribute[@xmi:type='uml:Property' and type/@xmi:type='uml:PrimitiveType']" {
|
|
639 |
0> «@name»
|
|
640 |
choose {
|
|
641 |
when "type/@href='http://schema.omg.org/spec/UML/2.1/uml.xml#String'"
|
|
642 |
> VARCHAR
|
|
643 |
|
|
644 |
// [...] for other types, extend when clauses
|
|
645 |
}
|
|
646 |
if "position()!=last()" > ,
|
|
647 |
text "\\n";
|
|
648 |
}
|
|
649 |
}
|
|
650 |
||
|
|
651 |
|
|
652 |
p > Our little sample only supports «VARCHAR», but it is an easy game to play to complete that.
|
|
653 |
|
|
654 |
p >>
|
|
655 |
Here you can download the ¬samples/xmi2ddl.ysl2 XMI 2 DDL compiler sample¬. I used
|
|
656 |
¬http://bouml.free.fr/ BOUML¬ to create a small ¬samples/demo.xmi UML sample file¬
|
|
657 |
as ¬http://www.omg.org/spec/XMI/ XMI 2.1¬.
|
|
658 |
>>
|
|
659 |
|
|
660 |
p > To compile that, use:
|
|
661 |
|
|
662 |
Code ||
|
|
663 |
% yml2proc -y xmi2ddl.ysl2 -x demo.xmi
|
|
664 |
CREATE TABLE demo_Customer (
|
|
665 |
id VARCHAR,
|
|
666 |
name VARCHAR
|
|
667 |
);
|
|
668 |
% _
|
|
669 |
||
|
|
670 |
|
|
671 |
p >>
|
|
672 |
In the samples directory you'll find
|
|
673 |
¬samples/xmi2ddl.uml2 a prettier solution¬ using ¬samples/uml.yml2 some declares to prettify¬.
|
|
674 |
>>
|
|
675 |
|
|
676 |
h1 id=features > Features of YSLT
|
|
677 |
|
|
678 |
p >>
|
|
679 |
Because YSLT just generates XSLT programs, it will be a good idea to read the
|
|
680 |
¬http://www.w3.org/TR/xslt XSLT Documentation¬ as well as the
|
|
681 |
¬http://www.w3.org/TR/xpath XPath Documentation¬.
|
|
682 |
>>
|
|
683 |
|
|
684 |
p >>
|
|
685 |
In the following, you find a ¬#functionlist List of YSLT Functions¬ and a
|
|
686 |
¬#operatorlist List of YSLT Operators¬.
|
|
687 |
>>
|
|
688 |
|
|
689 |
h2 id=functionlist > List of YSLT Functions
|
|
690 |
|
|
691 |
h3 > apply(select, *indent=1)
|
|
692 |
|
|
693 |
p >>
|
|
694 |
Generates the «<xsl:apply-templates />»
|
|
695 |
¬http://www.w3.org/TR/xslt#section-Applying-Template-Rules tag¬. The «*indent» pointer gives
|
|
696 |
the number of indention levels to add (default: 1) for the Indention System.
|
|
697 |
>>
|
|
698 |
|
|
699 |
p i > Example:
|
|
700 |
|
|
701 |
Code | apply "attr|aggregation", mode=define;
|
|
702 |
|
|
703 |
h3 id=assert > assert(test, msg)
|
|
704 |
|
|
705 |
p >>
|
|
706 |
Generates «<xsl:value-of select="yml:assert(test, msg)"». See the ¬#ymlassert yml:assert() XPath extension¬.
|
|
707 |
This function does not generate anything when not being called by ¬toolchain#processor ysltproc¬
|
|
708 |
with the --debug switch.
|
|
709 |
>>
|
|
710 |
|
|
711 |
h3 > attrib(name, namespace)
|
|
712 |
|
|
713 |
p > Generates the «<xsl:attribute />» ¬http://www.w3.org/TR/xslt#creating-attributes tag¬.
|
|
714 |
|
|
715 |
h3 > call(name)
|
|
716 |
|
|
717 |
p >>
|
|
718 |
Generates the «<call-template />» ¬http://www.w3.org/TR/xslt#named-templates tag¬.
|
|
719 |
Used to call a «function()».
|
|
720 |
>>
|
|
721 |
|
|
722 |
p i > Example:
|
|
723 |
|
|
724 |
Code | call "ucase" with "text", "$name";
|
|
725 |
|
|
726 |
h3 > choose()
|
|
727 |
|
|
728 |
p >>
|
|
729 |
Generates the «<xsl:choose />»
|
|
730 |
¬http://www.w3.org/TR/xslt#section-Conditional-Processing-with-xsl:choose tag¬.
|
|
731 |
Use in a «choose() ... when()... otherwise()...» structure.
|
|
732 |
>>
|
|
733 |
|
|
734 |
p i > Example:
|
|
735 |
|
|
736 |
Code ||
|
|
737 |
choose {
|
|
738 |
when "$id=1"
|
|
739 |
> yes
|
|
740 |
when "$id=2"
|
|
741 |
> no
|
|
742 |
otherwise
|
|
743 |
error "invalid id";
|
|
744 |
}
|
|
745 |
||
|
|
746 |
|
|
747 |
h3 > comment()
|
|
748 |
|
|
749 |
p > Generates the «<xsl:comment />» ¬http://www.w3.org/TR/xslt#section-Creating-Comments tag¬.
|
|
750 |
|
|
751 |
p i > Example:
|
|
752 |
|
|
753 |
Code | comment "this comment will remain in XML";
|
|
754 |
|
|
755 |
h3 > const(name, select)
|
|
756 |
|
|
757 |
p > Generates the «<xsl:variable />» ¬http://www.w3.org/TR/xslt#variables tag¬.
|
|
758 |
|
|
759 |
p i > Example:
|
|
760 |
|
|
761 |
Code | const "pi", 3.14;
|
|
762 |
|
|
763 |
h3 > copy(select)
|
|
764 |
|
|
765 |
p > Generates the «<xsl:copy-of />» ¬http://www.w3.org/TR/xslt#copy-of tag¬.
|
|
766 |
|
|
767 |
p i > Example:
|
|
768 |
|
|
769 |
Code | copy ".";
|
|
770 |
|
|
771 |
h3 id=debug > debug(msg)
|
|
772 |
|
|
773 |
p >>
|
|
774 |
Generates «<xsl:value-of select="yml:debug(msg)"». See the ¬#ymldebug yml:debug() XPath extension¬.
|
|
775 |
This function does not generate anything when not being called by ¬toolchain#processor ysltproc¬
|
|
776 |
with the --debug switch.
|
|
777 |
>>
|
|
778 |
|
|
779 |
h3 > def(name)
|
|
780 |
|
|
781 |
p >>
|
|
782 |
Generates the ¬http://www.exslt.org EXSLT¬ «<func:funcion />»
|
|
783 |
¬http://www.exslt.org/func/elements/function/ tag¬.
|
|
784 |
>>
|
|
785 |
|
|
786 |
h3 id=edocument > document(href, method)
|
|
787 |
|
|
788 |
p >>
|
|
789 |
Generates the ¬http://www.exslt.org EXSLT¬ «<exsl:document />»
|
|
790 |
¬http://www.exslt.org/exsl/elements/document/ tag¬.
|
|
791 |
>>
|
|
792 |
|
|
793 |
p i > Example:
|
|
794 |
|
|
795 |
Code ||
|
|
796 |
template "entity" document "{@name}.java" {
|
|
797 |
[…]
|
|
798 |
}
|
|
799 |
||
|
|
800 |
|
|
801 |
h3 > element(name, namespace)
|
|
802 |
|
|
803 |
p >>
|
|
804 |
Generates the «<xsl:element />»
|
|
805 |
¬http://www.w3.org/TR/xslt#section-Creating-Elements-with-xsl:element tag¬.
|
|
806 |
>>
|
|
807 |
|
|
808 |
h3 > error()
|
|
809 |
|
|
810 |
p >>
|
|
811 |
Generates the «<xsl:message />» ¬http://www.w3.org/TR/xslt#message tag¬
|
|
812 |
with attribute «terminate» set to "yes".
|
|
813 |
>>
|
|
814 |
|
|
815 |
h3 > estylesheet(*output="xml")
|
|
816 |
|
|
817 |
p >>
|
|
818 |
Does the same as «stylesheet()», but additionally declares the ¬http://www.exslt.org/ EXSLT¬
|
|
819 |
functions of the groups «exsl», «func», «str», «dyn», «set» and «math» and declares the
|
|
820 |
corresponding name spaces.
|
|
821 |
>>
|
|
822 |
|
|
823 |
h3 > for(select)
|
|
824 |
|
|
825 |
p > Generates the «<xsl:for-each />» ¬http://www.w3.org/TR/xslt#for-each tag¬.
|
|
826 |
|
|
827 |
p i > Example:
|
|
828 |
|
|
829 |
Code | for "../structure[@name='hello'" > «@type»
|
|
830 |
|
|
831 |
h3 > foreach(select)
|
|
832 |
|
|
833 |
p > Same as «for()».
|
|
834 |
|
|
835 |
h3 > function(name)
|
|
836 |
|
|
837 |
p >>
|
|
838 |
Generates the «<xsl:template />» ¬http://www.w3.org/TR/xslt#named-templates tag¬.
|
|
839 |
Used by calling with «call()».
|
|
840 |
>>
|
|
841 |
|
|
842 |
p i > Example:
|
|
843 |
|
|
844 |
Code ||
|
|
845 |
function "ucase" {
|
|
846 |
param "text";
|
|
847 |
|
|
848 |
value "translate(substring($text,1,1),'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')";
|
|
849 |
value "substring($text, 2)";
|
|
850 |
}
|
|
851 |
||
|
|
852 |
|
|
853 |
h3 > if(test)
|
|
854 |
|
|
855 |
p >>
|
|
856 |
Generates the «<xsl:if />»
|
|
857 |
¬http://www.w3.org/TR/xslt#section-Conditional-Processing-with-xsl:if tag¬.
|
|
858 |
>>
|
|
859 |
|
|
860 |
p i > Example:
|
|
861 |
|
|
862 |
Code | if "position()<last()" > ,
|
|
863 |
|
|
864 |
h3 > import(href)
|
|
865 |
|
|
866 |
p > Generates the «<xsl:import />» ¬http://www.w3.org/TR/xslt#import tag¬.
|
|
867 |
|
|
868 |
h3 > key(name, match, use)
|
|
869 |
|
|
870 |
p > Generates the «<xsl:key />» ¬http://www.w3.org/TR/xslt#key tag¬.
|
|
871 |
|
|
872 |
h3 > message()
|
|
873 |
|
|
874 |
p > Generates the «<xsl:message />» ¬http://www.w3.org/TR/xslt#message tag¬.
|
|
875 |
|
|
876 |
h3 > otherwise()
|
|
877 |
|
|
878 |
p >>
|
|
879 |
Generates the «<xsl:otherwise />»
|
|
880 |
¬http://www.w3.org/TR/xslt#section-Conditional-Processing-with-xsl:choose tag¬.
|
|
881 |
Use in a «choose() ... when()... otherwise()...» structure.
|
|
882 |
>>
|
|
883 |
|
|
884 |
h3 > output(method)
|
|
885 |
|
|
886 |
p > Generates the «<xsl:output />» ¬http://www.w3.org/TR/xslt#output tag¬.
|
|
887 |
|
|
888 |
h3 > param(name, select)
|
|
889 |
|
|
890 |
p > Generates the «<xsl:param />» ¬http://www.w3.org/TR/xslt#variables tag¬.
|
|
891 |
|
|
892 |
p i > Example:
|
|
893 |
|
|
894 |
Code | param "x", 42;
|
|
895 |
|
|
896 |
h3 > processing(name)
|
|
897 |
|
|
898 |
p >>
|
|
899 |
Generates the «<xsl:processing-instruction />»
|
|
900 |
¬http://www.w3.org/TR/xslt#section-Creating-Processing-Instructions tag¬.
|
|
901 |
>>
|
|
902 |
|
|
903 |
h3 > raw()
|
|
904 |
|
|
905 |
p >>
|
|
906 |
Generates the «<xsl:text />» ¬http://www.w3.org/TR/xslt#section-Creating-Text tag¬.
|
|
907 |
Sets the attribute «disable-output-escaping» to "yes".
|
|
908 |
>>
|
|
909 |
|
|
910 |
h3 > result(select)
|
|
911 |
|
|
912 |
p >>
|
|
913 |
Generates the ¬http://www.exslt.org EXSLT¬ «<func:result />»
|
|
914 |
¬http://www.exslt.org/func/elements/result/ tag¬.
|
|
915 |
>>
|
|
916 |
|
|
917 |
h3 > stylesheet(*output="xml")
|
|
918 |
|
|
919 |
p >>
|
|
920 |
Generates the XSLT «<stylesheet />» ¬http://www.w3.org/TR/xslt#stylesheet-element tag¬.
|
|
921 |
Additionally generates an «<output />» ¬http://www.w3.org/TR/xslt#output tag¬
|
|
922 |
in the body, with attribute «method» set to the value of the pointer «*output» (default: "xml").
|
|
923 |
>>
|
|
924 |
|
|
925 |
p > The content you're giving is placed in the body after the «<output />» tag.
|
|
926 |
|
|
927 |
p > The «version» attribute is set to "1.0" and XML namespace «xsl» is correctly defined.
|
|
928 |
|
|
929 |
p >>
|
|
930 |
In short: use for a stylesheet, just give the output type as parameter, if you don't want to
|
|
931 |
to generate XML but HTML ("html") oder plain text ("text").
|
|
932 |
>>
|
|
933 |
|
|
934 |
p > «stylesheet()» additionally generates tags for the Indention System.
|
|
935 |
|
|
936 |
h3 > template(match)
|
|
937 |
|
|
938 |
p >>
|
|
939 |
Generates the «<xsl:template />» ¬http://www.w3.org/TR/xslt#section-Defining-Template-Rules tag¬.
|
|
940 |
Additionally generates tags for the Indention System.
|
|
941 |
>>
|
|
942 |
|
|
943 |
p i > Example:
|
|
944 |
|
|
945 |
Code ||
|
|
946 |
template "attr", mode=declare
|
|
947 |
| attribute `] «@type»` `] «@name»`;
|
|
948 |
||
|
|
949 |
|
|
950 |
h3 > text()
|
|
951 |
|
|
952 |
p > Generate the «<xsl:text />» ¬http://www.w3.org/TR/xslt#section-Creating-Text tag¬.
|
|
953 |
|
|
954 |
h3 > textstylesheet()
|
|
955 |
|
|
956 |
p > Same as «estylesheet()», but «*output» is now "text", that means the stylesheet outputs plain text.
|
|
957 |
|
|
958 |
h3 > tstylesheet()
|
|
959 |
|
|
960 |
p > Same as «textstylesheet()».
|
|
961 |
|
|
962 |
h3 > value(select)
|
|
963 |
|
|
964 |
p > Generates the «<xsl:value-of />» ¬http://www.w3.org/TR/xslt#value-of tag¬.
|
|
965 |
|
|
966 |
p i > Example:
|
|
967 |
|
|
968 |
Code | value "@name";
|
|
969 |
|
|
970 |
h3 > warning()
|
|
971 |
|
|
972 |
p >>
|
|
973 |
Generates the «<xsl:message />» ¬http://www.w3.org/TR/xslt#message tag¬
|
|
974 |
with attribute «terminate» set to "no".
|
|
975 |
>>
|
|
976 |
|
|
977 |
h3 > when()
|
|
978 |
|
|
979 |
p >>
|
|
980 |
Generates the «<xsl:when />»
|
|
981 |
¬http://www.w3.org/TR/xslt#section-Conditional-Processing-with-xsl:choose tag¬.
|
|
982 |
Use in a «choose() ... when()... otherwise()...» structure.
|
|
983 |
>>
|
|
984 |
|
|
985 |
h3 > with(name, select)
|
|
986 |
|
|
987 |
p >>
|
|
988 |
Generates the «<xsl:with-param />»
|
|
989 |
¬http://www.w3.org/TR/xslt#section-Passing-Parameters-to-Templates tag¬.
|
|
990 |
>>
|
|
991 |
|
|
992 |
p i > Example:
|
|
993 |
|
|
994 |
Code | call "ucase" with "text", "$name";
|
|
995 |
|
|
996 |
h2 id=operatorlist > List of YSLT Text Operators
|
|
997 |
|
|
998 |
h3 id="angledouble" > Operator `] <code>«…»</code>`
|
|
999 |
|
|
1000 |
p > Generate YSLT Function Call «value('…')».
|
|
1001 |
|
|
1002 |
h2 id=xpathext > Debugging Functions
|
|
1003 |
|
|
1004 |
p >>
|
|
1005 |
YML defines two functions in namespace http://fdik.org/yml, which are enabled if the command line
|
|
1006 |
option --debug is given in ¬yslt#processor yml2proc¬.
|
|
1007 |
>>
|
|
1008 |
|
|
1009 |
h3 id=ymlassert > yml:assert(test, msg)
|
|
1010 |
|
|
1011 |
p >>
|
|
1012 |
If XPath expression «test» evaluates to «false()» or to an empty set, XPath expression «msg» is
|
|
1013 |
printed to stderr; the compilation then aborts with an error.
|
|
1014 |
>>
|
|
1015 |
|
|
1016 |
p >>
|
|
1017 |
Better don't use it directly, use the ¬#assert assert() YSLT function¬.
|
|
1018 |
>>
|
|
1019 |
|
|
1020 |
h3 id=ymldebug > yml:debug(msg)
|
|
1021 |
|
|
1022 |
p >>
|
|
1023 |
Prints XPath expression «msg» to stderr.
|
|
1024 |
>>
|
|
1025 |
|
|
1026 |
p >>
|
|
1027 |
Better don't use it directly, use the ¬#debug debug() YSLT function¬.
|
|
1028 |
>>
|
|
1029 |
|
|
1030 |
h2 id=stdlib > Standard Function Library
|
|
1031 |
|
|
1032 |
p >>
|
|
1033 |
Additionally, you can «include standardlib.ysl2» in the body of your stylesheet.
|
|
1034 |
Then you'll have these extra functions:
|
|
1035 |
>>
|
|
1036 |
|
|
1037 |
h3 id=dec2hex > yml:dec2hex(dec, digits=8)
|
|
1038 |
|
|
1039 |
p >>
|
|
1040 |
Converts number «dec» into a string with a hexadecimal representation filled up to
|
|
1041 |
«digits» digits. If you're omitting the second parameter, it is set to 8.
|
|
1042 |
>>
|
|
1043 |
|
|
1044 |
h3 id=hex2dec > yml:hex2dec(hex)
|
|
1045 |
|
|
1046 |
p >>
|
|
1047 |
Converts the string «hex» consisting of hexadecimal digits into a number.
|
|
1048 |
>>
|
|
1049 |
|
|
1050 |
h3 id=lcase > yml:lcase(text)
|
|
1051 |
|
|
1052 |
p >>
|
|
1053 |
Converts all uppercase letters of string «text» into lowercase ones.
|
|
1054 |
>>
|
|
1055 |
|
|
1056 |
h3 id=ucase > yml:ucase(text)
|
|
1057 |
|
|
1058 |
p >>
|
|
1059 |
Converts all lowercase letters of string «text» into uppercase ones.
|
|
1060 |
>>
|
|
1061 |
|
|
1062 |
div id=bottom {
|
|
1063 |
a href="features" "<< back to YML Features" " "
|
|
1064 |
a href="#top" "^Top^" " "
|
|
1065 |
a href="toolchain" "> > explain the Tool Chain" " "
|
|
1066 |
a href="yslt.en.yhtml2" "(source)"
|
|
1067 |
}
|
|
1068 |
}
|
|
1069 |
|