SoftCorporation LLC.

 

XSL EASY 2

Since I wrote a little article "XSL easy" surprisingly I have got a number of emails, where people are asking me to give more examples and show more advanced steps.

Ok, why not? It is clear for me that most interesting transformation would be from XML to HTML, but which example would be good? What is it that everybody can use in real life? Hey, let's do a resume! This is a document which everybody need, as well it is changing from time to time, so, instead of wasting our time on formatting issues, we'll give this task to XSLT and concentrate on the content.

I cannot show here how resume HTML page looks, but if you have new Internet Explorer you can just go to resume.xml and take a look.

Why first of all I offer to look on HTML page? We have to know what we want to achieve as a result of our transformation. It does not matter, if it will be HTML, XML, plain text, etc. it is important to have clear understanding what is our resulting document. It will help us to separate variables from constants, or, in our case, content from presentation, otherworlds XML from XSL.

Couple words about XML document

Because I'm going to use here all the time words like Elements, Tags, Attributes, Content and Entities, I would like to give a short introduction of it. Actually it is the same as what we have in HTML, so it is easy.

Here is a tag: <resume> and its name is "resume". A tag always has left angle bracket (<) and a right angle bracket (>).

Tag may have an attribute (optional): <resume language="English">. In this case we have a tag "resume" and one attribute definition, where attribute "language" has value "English".

There are a start tag and an end tag. The Tag <resume> is a start tag. The end tag has the same name as a start tag, but tag name is prefaced with a slash (/) and there is no attributes, for example </resume>.

An element is a pair of identically named start and an end tags. It may have a content. Here is a sample of an element, with content "Text of my resume goes here."

<resume language="English">
    Text of my resume goes here.
</resume>

What if we don't have any content? You can use

<resume language="English"></resume>

or

<resume language="English"/>

A slash (/) in the end of start tag means that this tag does not have a content and it is closed. In valid XML document all tags must be closed (meaning that each start tag must have an end tag, unless it is closed itself) and all attributes must be in double or in single quotes (") or (').

Very often the Element is called as a Node. A node definition goes from DOM (Document Object Model) and not every node is an element. A node can be an element itself, or element attribute, or simply the text content of an element. In other words, node is an instance of any data within a document tree that gets processed.

An Entity is a storage unit. It may store single character, or a text, or any other type of media. Entity reference starts with symbol "&" and ends with ";". When XML processor recognizes the entity reference it replaces it with the actual chunk of information that the entity is referred to.

Here is an example of an entity: &nbsp;

XML has several predefined, or built-in, entities that can be invoked without any special declarations. Those entities replace only one character each:

&lt; <
&gt; >
&apos; '
&quote; "
&amp; &

XML provides a method for declaring your own entities in a DTD for reference later in your document instance. The XML declaration and CDATA section are standard W3C XML 1.0 markup that affects how the parser handles the document.

Resume XML document

I do not use attributes here. It is always possible to create XML document without attributes. Usually for beginners it is easier to understand XML with no attributes in it. But in real life attributes play important role.


<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="resume.xsl"?>

<!--============================================-->
<!— Resume XML -->
<!--============================================-->
<resume>
    <author>
        <name-first>James</name-first>
        <name-last>Bond</name-last>
        <name-middle>X</name-middle>
        <address>
            <street>1000 Lincoln St.</street>
            <town>Ridgewood</town>
            <state>NJ</state>
            <zip>07450</zip>
        </address>
        <phone>(123) 456-7890</phone>
        <e-mail>james@bond.com</e-mail>
        <web>www.softcorporation.com</web>
    </author>

    <prof-summary>
        Over 20 years experience in software development. Involved in various phases of
        software life cycle which includes Design, Development, Testing and Analysis.
        Experience in Internet and Client/Server development.
    </prof-summary>

    <tech-profile>
        <os>
            Windows 95/98/NT/2000, Solaris/SunOS, UNIX, MSDOS.
        </os>
        <languages>
            Delphi, SQL, Java, HTML, XML, XSL, PAL, C, Assembler.
            (As well English, German, French, Italian, Russian, Spanish and Japanize)
        </languages>
        <gui>
            Delphi 1...5, Borland Jbuilder 3 and 4, Oracle JDeveloper.
        </gui>
        <databases>
            Oracle 8.1.5, MS SQL Server 6, Paradox, Access.
        </databases>
        <software>
            BEA Weblogic Server, IBM XSL Editor, MS Office
        </software>
        <hardware>
            PC, networking.
        </hardware>
        <technology>
            Servlets, Java applets, ODBC, Windows API, COM, TCP/IP Sockets,
            JDBC, RMI.
        </technology>
    </tech-profile>

    <job-experience>
        <job>
            <date-from>May 1998</date-from>
            <date-to>Present</date-to>
            <company>SoftCorporation LLC.</company>
            <location>Ridgewood, NJ</location>
            <title>Director of Software Development Department</title>
            <projects>
Led the architecture and analysis phase which included data modeling and graphical user interface design to support the client specific customizations. Designed, coded and tested an application running on BEA's Weblogic server. Worked extensively with Servlets. Development Java database applets with Oracle JDBC thin driver.
            </projects>
        </job>
        <job>
            <date-from>Jun 1997</date-from>
            <date-to>Aug 1999</date-to>
            <company>Information &amp; Technology Inc.</company>
            <location>NYC, NY</location>
            <title>Computer programmer</title>
            <projects>
                Development and maintaining the software for healthcare.
            </projects>
        </job>
    </job-experience>

    <education>
        <school>
            <study>
                Bachelor of Business Administration in Finance and Economics
            </study>
            <date-from>1991</date-from>
            <date-to>1991</date-to>
            <school-name>University of Wisconsin-Eau Claire</school-name>
            <location>Eau Claire, WI</location>
        </school>
        <school>
            <study>Advanced Java</study>
            <date-from>1988</date-from>
            <date-to>1988</date-to>
            <school-name>Stevens Institute of Technology</school-name>
            <location>Hoboken, NJ</location>
        </school>
    </education>

    <add-info>
        I have over 20 patents. References available upon request.
    </add-info>

</resume>


Couple words about XSL document

I have to tell you that it is more proper to call it XSLT, because XSL includes an XML vocabulary and XSLT, which is a language for transforming XML documents. So, I'm talking here about XSLT. XSLT can be used to manipulate, sort and filter XML data. In our case we'll create HTML text, which can be displayed by the browser.

Here is the list of most often used XSL Elements:

1. xsl:apply-templates - Directs the XSL processor to find the appropriate template to apply based on the type and context of each selected node.

2. xsl:attribute - Creates an attribute node and attaches it to an output element.

3. xsl:choose - Provides multiple conditional testing in conjunction with the
xsl:otherwise and xsl:when elements.

4. xsl:copy - Copies the current node from the source to the output.

5. xsl:for-each - Applies a template repeatedly to a set of nodes.

6. xsl:if - Allows simple conditional template fragments.

7. xsl:otherwise - Provides multiple conditional testing in conjunction with the xsl:choose and xsl:when elements.

8. xsl:stylesheet - The document element of a stylesheet, containing xsl:template elements.

9. xsl:template - Defines a template for the desired output for nodes of a particular type and context.

10. xsl:value-of - Inserts the value of the selected node as text.

11. xsl:when - Provides multiple conditional testing in conjunction with the xsl:choose and xsl:otherwise elements.

Some of those elements may have no attributes at all and some of them must be with attributes.


Resume XSL document

For a beginning my prime goal is to show how to use templates. In our Resume example I'll use only 5 XSL elements: xsl:stylesheet, xsl:template, xsl:value-of, xsl:apply-templates, and xsl:attribute. Later I'll show how to replace simple templates with elements xsl:if and xsl:choose.


<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<!--============================================-->
<!—Resume XSL -->
<!-- Root Element Template -->
<!-- This template specifies what creates the root -->
<!-- element of the result tree. -->
<!--============================================-->
<xsl:template match="/">
<xsl:apply-templates select="resume"/>
</xsl:template>

<!--============================================-->
<!-- <resume> template -->
<!-- This template creates the entire HTML document, -->
<!-- including the <head> and <body> sections. -->
<!--============================================-->
<xsl:template match="resume">
<html>

<!--============================================-->
<!-- The title of the HTML document with author's name -->
<!--============================================-->
<head>
<title>
Resume of <xsl:value-of select="author/name-first"/>
<xsl:apply-templates select="author/name-middle"/>
<xsl:value-of select="author/name-last"/>
</title>
</head>
<body>
<table border="0" width="600">
<tr>
<td width="250"/>
<td width="350">
<h1><font color="blue">RESUME</font></h1><p/>

<!--============================================-->
<!-- Create address, phone, etc. -->
<!--============================================-->
<xsl:value-of select="author/name-first"/>
<xsl:apply-templates select="author/name-middle"/>
<xsl:value-of select="author/name-last"/><br/>
<xsl:value-of select="author/address/street"/><br/>
<xsl:value-of select="author/address/town"/>,
<xsl:value-of select="author/address/state"/>
<xsl:value-of select="author/address/zip"/>
<p/>
Phone:
<xsl:value-of select="author/phone"/><br/>
<xsl:apply-templates select="author/e-mail"/><br/>
<xsl:apply-templates select="author/web"/><br/>
</td>
</tr>
</table>

<!--============================================-->
<!-- Create professional summary -->
<!--============================================-->
<table border="0" width="600">
<tr>
<td width="600"><h3>Professional Summary:</h3></td>
</tr>
</table>
<table border="0" width="600">
<tr>
<td width="100"/>
<td width="500">
<xsl:value-of select="prof-summary"/>
</td>
</tr>
</table>
<br/>

<!--============================================-->
<!-- Create technical profile -->
<!--============================================-->
<table border="0" width="600">
<tr>
<td width="600"><h3>Technical profile:</h3></td>
</tr>
</table>
<table border="0" width="600">
<xsl:apply-templates select="tech-profile/os"/>
<xsl:apply-templates select="tech-profile/languages"/>
<xsl:apply-templates select="tech-profile/gui"/>
<xsl:apply-templates select="tech-profile/databases"/>
<xsl:apply-templates select="tech-profile/hardware"/>
<xsl:apply-templates select="tech-profile/software"/>
<xsl:apply-templates select="tech-profile/technology"/>
</table>
<br/>

<!--============================================-->
<!-- Create job experience -->
<!--============================================-->
<table border="0" width="600">
<tr>
<td width="600"><h3>Job experience:</h3></td>
</tr>
</table>
<table border="0" width="600">
<xsl:apply-templates select="job-experience/job"/>
</table>

<!--============================================-->
<!-- Create education -->
<!--============================================-->
<table border="0" width="600">
<tr>
<td width="600"><h3>Education:</h3></td>
</tr>
</table>
<table border="0" width="600">
<xsl:apply-templates select="education/school"/>
</table>

<!--============================================-->
<!-- Create additional information -->
<!--============================================-->
<table border="0" width="600">
<tr>
<td><h3>Additional information:</h3></td>
</tr>
</table>
<table border="0" width="600">
<tr>
<td width="100"/>
<td width="500">
<xsl:value-of select="add-info"/>
</td>
</tr>
</table>
<br/>

<!--============================================-->
<!-- The END of HTML file -->
<!--============================================-->
</body>
</html>
</xsl:template>

<!--============================================-->
<!-- <author/name-middle> template -->
<!-- This template prints the optional middle name -->
<!--============================================-->
<xsl:template match="author/name-middle">
<xsl:value-of select="."/>.
</xsl:template>

<!--============================================-->
<!-- <author/e-mail> template -->
<!-- This template prints the optional email -->
<!--============================================-->
<xsl:template match="author/e-mail">
E-mail:
<A>
<xsl:attribute name="HREF">mailto:<xsl:value-of select="."/></xsl:attribute>
<xsl:value-of select="."/>
</A>
</xsl:template>

<!--============================================-->
<!-- <author/web> template -->
<!-- This template prints the optional web site -->
<!--============================================-->
<xsl:template match="author/web">
Web:
<A>
<xsl:attribute name="HREF">http://<xsl:value-of select="."/></xsl:attribute>
<xsl:value-of select="."/>
</A>
</xsl:template>

<!--============================================-->
<!-- <tech-profile> templates -->
<!--============================================-->
<xsl:template match="tech-profile/os">
<tr>
<td width="100" align="right">OS -</td>
<td width="500"><xsl:value-of select="."/></td>
</tr>
</xsl:template>

<xsl:template match="tech-profile/languages">
<tr>
<td width="100" align="right">Languages -</td>
<td width="500"><xsl:value-of select="."/></td>
</tr>
</xsl:template>

<xsl:template match="tech-profile/gui">
<tr>
<td width="100" align="right">GUI -</td>
<td width="500"><xsl:value-of select="."/></td>
</tr>
</xsl:template>

<xsl:template match="tech-profile/databases">
<tr>
<td width="100" align="right">Databases -</td>
<td width="500"><xsl:value-of select="."/></td>
</tr>
</xsl:template>

<xsl:template match="tech-profile/hardware">
<tr>
<td width="100" align="right">Hardware -</td>
<td width="500"><xsl:value-of select="."/></td>
</tr>
</xsl:template>

<xsl:template match="tech-profile/software">
<tr>
<td width="100" align="right">Software -</td>
<td width="500"><xsl:value-of select="."/></td>
</tr>
</xsl:template>

<xsl:template match="tech-profile/technology">
<tr>
<td width="100" align="right">Technology -</td>
<td width="500"><xsl:value-of select="."/></td>
</tr>
</xsl:template>

<!--============================================-->
<!-- <job-experience> template -->
<!--============================================-->
<xsl:template match="job-experience/job">
<tr>
<td width="10"/>
<td width="90">
<xsl:value-of select="date-from"/> -<br/>
<xsl:value-of select="date-to"/><p/>
</td>
<td width="500">
<b><xsl:value-of select="company"/></b>
<xsl:value-of select="location"/><br/><br/>
<xsl:value-of select="title"/>.<br/>
<xsl:value-of select="projects"/>
<p/>
</td>
</tr>
</xsl:template>

<!--============================================-->
<!-- <education> template -->
<!--============================================-->
<xsl:template match="education/school">
<tr>
<td width="10"/>
<td width="90">
<xsl:value-of select="date-from"/> -<br/>
<xsl:value-of select="date-to"/><p/>
</td>
<td width="500">
<xsl:value-of select="study"/>.<br/>
<xsl:value-of select="school-name"/>.
<xsl:value-of select="location"/><br/>
<p/>
</td>
</tr>
</xsl:template>

</xsl:stylesheet>


Here is a short introduction of XSL Elements, which have been used in our Resume example.

First of all I want to introduce xsl:stylesheet element.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">

It is a root of XML tree. (Don't forget that XSL document is XML itself!) This element can have a set of xsl:template elements representing different output templates. An attribute xmlns:xsl="http://www.w3.org/TR/WD-xsl" works fine with Microsoft IE5. If you prefer to use this example with Apache Xalan or IBM XSL Editor replace this attribute value to "http://www.w3.org/XSL/Transform/1.0".

Next most important element is xsl:template.

<xsl:template match="tech-profile/hardware">

A template defines a set of rules for transforming a source document into the result tree. An attribute "match" specifies XML element for which the template should be executed.

In order to apply the template we are using xsl:apply-templates element.

<xsl:apply-templates select="resume"/>

The xsl:apply-templates element directs the XSL processor to find an appropriate xsl:template to apply. An attribute "select" used to select appropriate XSL template. If more than one template satisfies the pattern, the one appearing last in the style sheet is chosen.

To insert text data from XML document used a xsl:value-of element.

<xsl:value-of select="author/name-first"/>

The xsl:value-of element inserts a text string representing the value of the corresponding XML element specified by the select attribute. If more than one XML element satisfies the pattern, the value of the first element (in document order) is inserted. If XML element appear to be an element with substructure, xsl:value-of returns the concatenated text of all subtree with the markup removed.

In <xsl:value-of select="."/> the pattern "." means that selected value will be current XML element.

An XSLT allows add attributes to nearest upper XSL element.

<a>
    <xsl:attribute name="href">http://www.softcorporation.com</xsl:attribute>
</a>

So result of transformation will be <a href="http://www.softcorporation.com">.


Improvement of Resume XSL document

All process starts from the root XSL template, indicated by the pattern "/". In our case the root template selects the root of XML document and applies to "resume" template. To simplify the XSL we can replace it with our "resume" template, changed as:

<xsl:template match="/resume">

</xsl:template>


Creating the title of the document, I'm using:

<xsl:apply-templates select="author/name-middle"/>

The only purpose of using template here is to show how a template works with XML document. If we will delete from XML element "author/name-middle/" then in our HTML output document text "X." will disappear.

To simplify the XSL we can use xsl:if instead of applying a template:

<xsl:if test="author/name-middle != ''">
    <xsl:value-of select="author/name-middle"/>.
</xsl:if>

There is no "else" for element xsl:if in XSL, but you can use xsl:choose with xsl:test and xsl:otherwise instead:

<xsl:choose>
    <xsl:when test="author/name-middle != ''">
        <xsl:value-of select="author/name-middle"/>.
    </xsl:when>
    <xsl:otherwise>I don't have a middle name!</xsl:otherwise>
</xsl:choose>

Here I would like to finish. In order to try Resume example save XML document in to the file resume.xml and XSL document in to the file resume.xsl located in the same directory on your hard drive. Open XML file in Microsoft Internet Explorer 5 and you will see transformed HTML document.

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.