Actually, if a functional language is practical or not, depends of what's to do – “the
right tool for the task”, one could say.

Because processing XML data means traversing a (document) tree,
recursive algorithms are a clear advantage.
And this is the reason, why choosing a functional language is a good choice for that job.

It's a little bit like with SQL – for it's job, it's
excellent, but no-one wants to write a complete application in SQL (and also not in one
of the Turing-complete SQL extension languages like
PL/SQL).

Generating HTML out of a DSL

Of course, you can use YSLT as you would use XSLT. Let's say, you have data in XML
documents, and you want to have an excerpt out of this data. This is a common task,
comparable to SELECT data out of an SQL database. But we have no database, we have
something like this file customers.yml2:

In the example above, stylesheet declares the main program. Then we define a template, which
is executed automatically starting reading the root '/' of the document tree of the XML
document we're processing.

How to write that in a Java Program? Well, following the second step, we're
writing our target code manually; for this simple sample, let's be naive and
save the following into a file named Customer.java.target:

Now for the deconstruction. I think, it'll be best, if we
create a .java file for each entity.
So we're moving the whole thing into a template for each entity
creating a file, and we're applying this for each entity using the name of each
Entity as parameter. We're adding some distinction of cases, too.

When we apply, the indention system of YSLT will add an indention level, so we can
take out rendundant whitespace; for the first apply we don't want this, so we're
giving the number 0 as the indention level.

In attributes, braces {…} let us insert the value of an XPath expression into our
DSL, while inside quoted text the same is done by the angle double quotes«…»:

As you can see, we're deconstructing step by step. This is a good idea to get
into code generation with YSLT, but it remains a good idea even for the advanced
programmer: it keeps a clear view on what's happening.

In the last step, test it out and make a diff to your target code. You will see
that our example needs some beautifying: in Java, camel case is important and
makes you some work to revert characters to uppercase or lowercase, respectively.
For that work you'll see that XPath's translate() function is a little bit ugly ;-)
So we're defining an operator for that at the top of the file: