vb@0: include homepage.en.yhtml2 vb@0: vb@0: page "YSLT – XSLT C style" { vb@0: h1 id=intro > YSLT – an introduction vb@0: vb@0: p >> vb@0: Especially the ¬http://www.w3.org/TR/xslt XSLT¬ programmer can benefit from YML. vb@0: Usually many attributes of XSLT are annoying in programming praxis: vb@0: >> vb@0: vb@0: ul { vb@0: li >> vb@0: the missing separation of ¬http://en.wikipedia.org/wiki/Indent_style indention¬ vb@0: of the XSLT program and indention of the output text vb@0: >> vb@0: vb@0: li > the complicated syntax vb@0: vb@0: li > the lack of good text escaping mechanisms (< and > are not seldom) vb@0: vb@0: li > ... vb@0: } vb@0: vb@0: p > In short, it's ugly ;-) vb@0: vb@0: p >> vb@0: Usually the result is, that many programmers are avoiding XSLT as a programming language. vb@0: It's a pity, because for processing XML data, it can do much more than “formatting” as vb@0: “stylesheets”. vb@0: >> vb@0: vb@0: p >> vb@0: The idea of YSLT now is to supply a simple language, which programmers want to use, to vb@0: make the features of XSLT accessible to a broader base of people. vb@0: >> vb@0: vb@0: p >> vb@0: YSLT can be used much simpler; it's just a ¬http://fdik.org/yml/index#ylanguages Y Language¬ vb@0: for XSLT. I'm using it for my blog, here you can ¬http://fdik.org/yblog2.tar.bz2 download YBlog2¬, vb@0: a simple blogging software in YSLT. vb@0: >> vb@0: vb@0: p > Here you can find the ¬yslt.yml2 YSLT specification¬. vb@0: vb@0: h2 id=hello > Hello, World vb@0: vb@0: p > In YSLT, the hello world program reads like this: vb@0: vb@0: Code || vb@0: include yslt.yml2 vb@0: textstylesheet template "/" | hello, world vb@0: || vb@0: vb@0: p >> vb@0: The `a href="http://fdik.org/yml/features#including" code > include` line includes the vb@0: YSLT Y Language declarations. The second line generates an XSLT hello world program. vb@0: You can generate it using: vb@0: >> vb@0: vb@0: Code | % yml2c -o hello.xsl hello.ysl2 vb@0: vb@0: p > This results in the following program: vb@0: vb@0: Code || vb@0: vb@0: vb@0: vb@0: hello, world vb@0: vb@0: || vb@0: vb@0: p >> vb@0: You can execute this program with any vb@0: ¬http://en.wikipedia.org/wiki/XML_template_engine XSL processor¬, for example the Free Software vb@0: ¬http://xmlsoft.org/XSLT/xsltproc2.html xsltproc¬. vb@0: >> vb@0: vb@0: Code || vb@0: % yml2c -o hello.xsl hello.ysl2 vb@0: % echo '' > empty.xml vb@0: % xsltproc hello.xsl empty.xml vb@0: hello, world vb@0: % _ vb@0: || vb@0: vb@0: p >> vb@0: Or you can just use ¬toolchain#processor yml2proc¬: vb@0: >> vb@0: vb@0: Code || vb@0: % yml2proc -My hello.ysl2 vb@0: hello, world vb@0: % _ vb@0: || vb@0: vb@0: h2 id=programming > Programming in YSLT vb@0: vb@0: p >> vb@0: Because YSLT is just a ¬index#ylanguages Y Language¬ for XSLT, you can do anything with YSLT vb@0: you can do with XSLT in just nicer syntax ;-) To read what XSLT is all about, I recommend vb@0: ¬http://www.w3.org/TR/xslt the official W3C documentation for XSLT¬. vb@0: >> vb@0: vb@0: p >> vb@0: So this document is just an beginners guide, if you want to understand XSLT completely, vb@0: better read the ¬http://www.w3.org W3C¬ stuff. vb@0: >> vb@0: vb@0: h3 > Programming YSLT means programming with a pure functional language. vb@0: vb@0: p >> vb@0: Lovers of ¬http://en.wikipedia.org/wiki/Lisp_(programming_language) Lisp¬ or vb@0: ¬http://www.haskell.org/ Haskell¬ will not see any problems. But many programmers are used to vb@0: have a programming language with a vb@0: ¬http://en.wikipedia.org/wiki/Procedural_programming procedural imperative paradigma¬ like vb@0: ¬http://java.sun.com Java¬, vb@0: ¬http://en.wikipedia.org/wiki/C_(programming_language) C¬/¬http://en.wikipedia.org/wiki/C++ C++¬ vb@0: or ¬http://www.python.org Python¬. Why should they use a vb@0: ¬http://en.wikipedia.org/wiki/Functional_programming functional¬ language? vb@0: >> vb@0: vb@0: p >> vb@0: Actually, if a functional language is practical or not, depends of what's to do – “the vb@0: right tool for the task”, one could say. vb@0: >> vb@0: vb@0: p >> vb@0: Because processing XML data means traversing a (document) tree, vb@0: ¬http://en.wikipedia.org/wiki/Recursion recursive¬ algorithms are a clear advantage. vb@0: And this is the reason, why choosing a functional language is a good choice for that job. vb@0: >> vb@0: vb@0: p >> vb@0: It's a little bit like with ¬http://en.wikipedia.org/wiki/SQL SQL¬ – for it's job, it's vb@0: excellent, but no-one wants to write a complete application in SQL (and also not in one vb@0: of the Turing-complete SQL extension languages like vb@0: ¬http://www.oracle.com/technology/tech/pl_sql/index.html PL/SQL¬). vb@0: >> vb@0: vb@0: h3 id=htmlgen > Generating HTML out of a DSL vb@0: vb@0: p >> vb@0: Of course, you can use YSLT as you would use XSLT. Let's say, you have data in XML vb@0: documents, and you want to have an excerpt out of this data. This is a common task, vb@0: comparable to «SELECT» data out of an SQL database. But we have no database, we have vb@0: something like this file customers.yml2: vb@0: >> vb@0: vb@0: Code || vb@0: decl customer(*id, *name) { id *id, name *name }; vb@0: vb@0: list { vb@0: customer 23, "Kurt Meier"; vb@0: customer 42, "Lieschen Schmidt"; vb@0: } vb@0: || vb@0: vb@0: p >> vb@0: Let's say, we want to output this into an ¬http://en.wikipedia.org/wiki/XHTML XHTML¬ document, vb@0: where the list is showed as a table. Then we would do the following: vb@0: >> vb@0: vb@0: p > The XHTML document should be like the following and include the list in the body: vb@0: vb@0: Code || vb@0: include yslt.yml2 vb@0: vb@0: stylesheet { vb@0: template "/" html { vb@0: head title "Customer List"; vb@0: body apply "list"; vb@0: } vb@0: || vb@0: vb@0: p >> vb@0: In the example above, stylesheet declares the main program. Then we define a template, which vb@0: is executed automatically starting reading the root «'/'» of the document tree of the XML vb@0: document we're processing. vb@0: >> vb@0: vb@0: p > Using the XML document as input, we're creating this HTML tree: vb@0: vb@0: Code || vb@0: vb@0: vb@0: Customer List vb@0: vb@0: vb@0: vb@0: