SoftCorporation LLC.

 

XSL EASY

Thanks God and great developers that now there is no discussion about future of XML. Looks like everybody agreed that it is kind of future and it is good.

However with XSL it is not quite clear and question "Is XSL the right language to use with XML documents?" still remains. More of it, fellows like Michael Leventhal (you can see some articles on www.xml.com) declared war to XSL! What is the reason?

The reason is simple. They do not quite understand what they are dealing with. This is the real problem. Unfortunately there is not so many articles about basics of XSL, and people just turning off looking on XSL statements and don't getting a sense of it. So, let's do something about it! This is the reason why
I decided to write this manuscript. On the start, sorry for my English, it is not my native language...

First of all, let's make it clear: XSL is the most powerful and expandable language at present time for use with XML as a formatting language. Just believe me, this is it, like axiom. For example, CSS is good, but, like "BASIC" and "C++", you can choose something that looks simple (is it so simple?), but if you decided to create something real, suddenly you may realize that it does not provide necessary power! You may try JSP, it is also good, but it is yet more difficult and still not so powerful!

Second: XSL is easy. YES! And I'm going to prove it! I just don't want to go to the third point, let's show here that I'm right.

If you are familiar with XML (just a little bit), you will understand XSL in a five minutes, not all of it, of course, but enough in order to judge on the mentioned above question.

Let's take a simple XML:

<?xml version="1.0"?>
<user>
    <name>Bill</name>
    <email>bill@softcorporation.com</email>
</user>

Let's suppose that I need to create an HTML like:

<html>
    <head>
        <title>User Login</title>
    </head>
    <body>
        Your username: Bill
        <br>
        Your email: bill@softcorporation.com
        <br>
    </body>
</html>

The easiest way of doing it is just to have it as a text and insert a name and email in it (similar like JSP doing). So it should be something like:

<html>
    <head>
        <title>User Login</title>
    </head>
    <body>
        Your username: "PUT NAME HERE!"
        <br>
        Your email: "PUT EMAIL HERE!"
        <br>
    </body>
</html>

If you were agreed with me, that it would be the easiest way, congratulations! Just now you agreed that XSL is the easiest way to create this HTML page from our XML! Let's see how our XSL looks:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/XSL/Transform/1.0">
<xsl:output method="html"/>

<xsl:template match="/user">

    <html>
        <head>
            <title>User Login</title>
        </head>
        <body>
            Your username: <xsl:value-of select="name"/>
            <br/>
            Your email: <xsl:value-of select="email"/>
            <br/>
        </body>
    </html>

</xsl:template>

</xsl:stylesheet>

Ok let's see what is inside. I'll go line by line and will give you a short introduction what does it mean.

1. <?xml version="1.0"?> - It looks familiar! Yes, XSL as well is an XML document, and it is a great advantage. For example, you can use a DTD.

2. <xsl:stylesheet xmlns:xsl="http://www.w3.org/XSL/Transform/1.0"> - This is a stylesheet definition, actually a definition of XSL itself. Because an XSL is an XML document, so we have to define it!

3. <xsl:output method="html"/> - This is an output format definition. Everything will work without this line, but attribute "html" shows that output must be formatted especially for HTML language.

4. <xsl:template match="/user"> - This is our output, to be more precise, a text, which we'll have as a result of transformation if in XML document root tag matches "user". Yes it does!

5. <html> ... - You are right, this is just a plain HTML. Because all three languages are children of SGML, so why not just insert HTML in XSL? But pay attention! There is a difference. XSL must be a valid XML document, so all tags must be closed and all attributes must be in quotations. This is why we have
<br/> tag instead of just <br>. This is an excellent rule! How many times you had a problem like “it works fine with one browser and does not with another!” Very often the reason is that some tags are not closed at all, and then different browsers trying to interpret this situation different ways. So why not just to say - it must be closed! This is where HTML4 finally came. XML/XSL transformer will automatically transform <br/> to <br> because we pointed that output must be an HTML document (see p.3).

6. <xsl:value-of select="email"/> - This is the expression which does not required too math explanations, it will select the value of <email> tag and insert it instead of itself.

7. </xsl:template> and </xsl:stylesheet> - As I mentioned before, all tags must be closed, so we closing them.

That's it. We have working XML and XSL documents to make an HTML. I doubt that anybody may say that it was difficult. You can try our samples using IBM’s XSL Editor, which you can download free from http://www.alphaworks.ibm.com/tech/xsleditor. But before I finish let me show you the simple example of XSL power. (I would like to see how you would do it using JSP or CSS).

Let's add users to our XML:

<?xml version="1.0"?>
<users>
    <user>
        <name>Bill</name>
        <email>bill@softcorporation.com</email>
    </user>
    <user>
        <name>John</name>
        <email>John@softcorporation.com</email>
    </user>
    <user>
        <name>Bob</name>
        <email>bob@softcorporation.com</email>
    </user>
    <user>
        <name>Maria</name>
        <email>maria@softcorporation.com</email>
    </user>
</users>

Here is XSL code to create an HTML document with a list of our users:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/XSL/Transform/1.0">
<xsl:output method="html"/>

<xsl:template match="/users">
<html>
    <head>
        <title>Users List</title>
    </head>
    <body>
        <xsl:apply-templates select="user"/>
    </body>
</html>
</xsl:template>

<xsl:template match="user">
    User username: <xsl:value-of select="name"/><br/>
    User email: <xsl:value-of select="email"/><br/><br/>
</xsl:template>

</xsl:stylesheet>

Isn’t it simple? I don’t think you need any explanations. Using templates (and reusing them!) brings great power to XSL language and gives you an ability to create your documents from many prepared templates stored separately, which you can just include using XSL statements like <xsl:include> or <xsl:import>. XSL allows you to separate the data from presentation, and this is also very important. XML document keeps only data, all presentation saved inside XSL. Above all don't forget about extensions, which allows creating your own namespaces and use elements like <myPrefix:myFunction myAttribute="myRule"/> which doing exactly what you want and returns result what you need. For example you can create your own variables and use it if you don’t like what you have in XSL. No limits!

By the way, creating separate templates and reusing them will save you a lot of very expensive programmer’s time. This was my third point.

Thanks a lot if you are reached here. We plan to put on our web site in a nearest future more information about simple way of working with XSL, with examples of included templates, using Java extensions, working with DOM, multi-XSL transformations, etc. Visit our web site at http://www.softcorporation.com.

Vadim L Permyakov.
SoftCorporation LLC.



Home

Keywords: XML, XSL, XSLT, XSL transformations, e-Business, SoftCorporation LLC.