[xsl] XSLT Hello World

Subject: [xsl] XSLT Hello World
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxx>
Date: Mon, 24 Mar 2014 12:05:32 -0400

Over in that other thread, Ihe said:

> Extracting text from an XML document is the hello world of XSLT.
> text() would appear to be an obvious way of doing that and it's really
> important that it entails no surprises. If I were an XSLT antagonist
> that is exactly the sort of thing I would  home in on to portray the
> language as arcane, difficult to use and not suitable for my project.

I find this really quite interesting.

One thing I stress to beginners is that if they're starting to learn
the language using only a syntax reference plus whatever they might
know about Perl, Python or Java, they are heading for a world of hurt.

It's not like your granddaddy's programming language, and when you
assume things like "text() is the way to get my data into the output,
because it says 'text'" ... ouch.

Indeed, the Hello World I like to use looks like this:

<xsl:stylesheet version="2.0"


I.e., a stylesheet with no templates at all. (You can also write it as
an empty element if you prefer.)

After having acknowledged our astonishment at the results of running
this, we can talk about the built-in templates. And then we start
writing templates of our own ... matching elements, not text nodes. We
do this with a source document with mixed content (such as <doc>Hello
<who>World</who></doc> ), (so we can try a few things out such as
matching elements in line), and then a slightly more elaborate
instance (perhaps including some comments or processing instructions
so we can illustrate aspects of the data model, such as text nodes) .

Take in a few points of fact, practice for an hour or two, and get a
good night's sleep, and you no longer run the risk that you will be
surprised by 'text()' in XSLT.

Talking about the processing model in the context of documents with
mixed content also gives us an early opportunity to start thinking
about XSLT's purposes, strengths and weaknesses.

I know this approach may reinforce the impressions of some programmers
that XSLT is, as Ihe says, "arcane, difficult to use and not suitable
for my project". To this my only answer is in the doing of it.
Assuming your problem is in fact in XSLT's sweet spot, showing how
powerful and easy XSLT is ... once you have a clue ... answers the
critique by itself.

If your problem isn't in XSLT's sweet spot, I ask why are you
interested in it. Yes, XSLT is generally very powerful, and can be
used for problems often quite different from the kinds of document
processing for which it was designed. But please please don't expect
an easy time trying to tackle one of those in XSLT before you have a
feel for templates and traversals.

The thing about "arcane" is also relative. Indeed, if you aren't
naturally a curious sort with a reasonable tolerance for the arcane,
one wonders why you expect to do well with programming at all.

Cheers, Wendell

Wendell Piez | http://www.wendellpiez.com
XML | XSLT | electronic publishing
Eat Your Vegetables

Current Thread