Maybe I should have titled this post "Learning to think declaratively".
I would be very interested to receive reactions of XSLT newbies to
something I am exploring as a way to help newcomers to XSLT to be able to
help themselves in solving XSLT programming tasks. I know many newcomers to
mailing lists much prefer simply to lurk, so replies on or off list are
equally welcome.
I don't recall seeing this approach anywhere explicitly used. If I am
re-inventing the wheel someone will no doubt let me know.
OK, what I have in mind ... for written teaching material on XSLT ... is as
follows.
To approach an XSLT task you simply declare (in writing) what it is that
you want to do.
1. Say what output you want
2. Say what source data you want to output in what way
3. Piece together the XSLT and XPath to achieve what you want as output
This takes advantage of XSLT being a declarative language. It seems so
obvious, yet I don't recall seeing anyone suggest this as a learning
approach. [I hope it isn't in Michael Kay's book in 3 foot high type, then
I would have to retreat with many deep blushes! :) ]
Easier to illustrate than describe. A VERY simple example so anyone should
be able to follow, even if they joined the list 5 minutes ago.
Say you have a very simple XML document like this.
<?xml version='1.0'?>
<Greeting>
<From>Andrew Watt</From>
<Title>The key to learning XSLT</Title>
<Message>XSLT can be fun!</Message>
</Greeting>
And you want to create, using XSLT, an HTML document like this.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>The key to learning XSLT</title>
</head>
<body>
<h3>From: Andrew Watt</h3>
<p>XSLT can be fun!</p>
</body>
</html>
What I am suggesting is that you might initially in attempting to solve
this "problem" explicitly declare what you want to do. Of course in a very
simple example like this you can do it in your head, but this is to
illustrate the idea.
1. Output an HTML document
2. In the title of the page put the value of the <Title> element
3. First in the page say who the message is from
4. Then put the message
Maybe as a separate step, may on first run through you add more detail or
code hints, maybe like this. The version could progressively add
indications of the XSLT or XPath to be used.
1. Output an HTML document - put <html> inside template that matches root node
2. In the title of the page put the value of the <Title> element -
<xsl:value-of>
3. First in the page say who the message is from. Value of <Title> which is
child of <Greeting> - <h3> <xsl:value-of> /Greeting/Title
4. Then put the message - <xsl:value-of> /Greeting/Message
Then, when you are comfortable that you have the "problem" (not big in this
case) worked out, you write some XSLT code.
<?xml version='1.0'?>
<xsl:stylesheet
version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:output method="html" indent="yes" encoding="UTF-8" />
<xsl:template match="/">
<html>
<head>
<title><xsl:value-of select="Greeting/Title" /></title>
</head>
<body>
<h3>From: <xsl:value-of select="Greeting/From" /></h3>
<p><xsl:value-of select="Greeting/Message" /></p>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
I think I prefer that the initial "declarations" are in plain English (or
any other human language), rather than pseudo-code. The latter is just
something more to learn for the newcomer.
As people follow this approach I imagine that they would, quite quickly,
start to use English that progressively expresses an XLST thought pattern.
For example, a declaration "For each XXX element do [whatever]" might
translate into an <xsl:for-each>.
Familiar parts of a problem quite possibly don't need to be written down.
But for parts of a problem causing difficulty it could be helpful to return
to a paradigm of, What do I want to output? Where will I get that data? How
can I code it?
So, the approach I am suggesting is as follows:
1. In plain English (etc) state what you want to output, including what
source data you want to use to produce the output
2. Associate what you want to output with XSLT and XPath code
3. Refine as necessary
4. Write the code
I am not suggesting that every step is followed for every part of a
problem. That could become very tedious. It just seems to me that this
approach might be useful for newcomers to XSLT to help them start thinking
in a declarative way. They declare what they want to do, what source data
they want to do it with then translate that into XSLT code.
1. What do I want to output?
2. Where will get that data?
3. How can I code it?
So is this suggested approach stupid/childlike/verbose or helpful? Or is
everybody already teaching XSLT this way and I missed it?
Andrew Watt
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list