XML Publishing – Part 1
XML Publishing - Part 1: Producing HTML. This article, part of a series, will show you how to do it.
Click on the panel titles below to view the contents.
Copyright
Terms
Summary
Section 1: Source code for the article
build.xml - Ant build script
build.properties - Properties for Ant build script
runAnt.cmd - Windows batch file for:
1. Checking Java and Ant versions.
2. Running Ant to create an HTML document from XML and XSL.
3. Printing help instructions for using the batch file.
browser - Directory containing browser example and includes the files listed below.
browser\article.css
browser\article.js
browser\effects.js
browser\prototype.js
browser\scriptaculous.js
browser\accordion.js
browser\articleHtml.xsl
browser\article.xml
browser\extern.gif
browser\process-simple.jpg
browser\xmldialect9.jpg
browser\xmldialect1.jpg
browser\xmldialect10.jpg
browser\xmldialect7.jpg
browser\xmldialect8.jpg
browser\xslcode.jpg
src - Directory containing all the source code and includes the files listed below.
src\js- Directory containing JavaScript files including scriptaculous libraries.
src\js\effects.js
src\js\scriptaculous.js
src\js\accordion.js
src\js\prototype.js
src\js\article.js
src\css - Directory containing CSS file for the article.
src\css\article.css
src\xsl - Directory containing XSL file for the article.
src\xsl\articleHtml.xsl
src\images - Directory containing image files for the article and includes the files listed below.
src\images\process-simple.jpg
src\images\extern.gif
src\images\xmldialect1.jpg
src\images\xmldialect7.jpg
src\images\xmldialect8.jpg
src\images\xmldialect9.jpg
src\images\xmldialect10.jpg
src\images\xslcode.jpg
src\xml - Directory containing XML file for the article.
src\xml\article.xml
Section 2: So you have 5 minutes ...
- Download the source code bundle from
- Enable JavaScript in your browser if you want the scriptaculous (JavaScript libraries) Accordion effect to work in the generated HTML file. For details see
- Unzip the source code bundle and go to the sub-directory called 'browser'.
- Double-click on the 'article.xml' file.
Section 3: You still here ?

The XSLT processor takes as inputs optional parameters, an XML file and an XSL file.
The XSLT processor produces a corresponding output HTML document.
- You will need Administrator rights on your computer. At the very least, you need to be able to install software.
- Download and install a Java J2SE Runtime Environment (JRE) 1.4.x or greater from
- Download and install Ant 1.6.5 from
- Set the JAVA_HOME and ANT_HOME environment variables on your computer.
- Download the source code bundle from
- Unzip the source code download bundle into an empty directory. If you haven't set the JAVA_HOME and ANT_HOME environment variables, update them in the Windows batch script 'runAnt.cmd' using your favorite text editor. These are currently set to the installation defaults for Ant and the Java Runtime Environment (JRE).
- Check that your set up works by opening a command window, changing to the extracted source code bundle directory and running the following command in the command window at the prompt: 'runAnt -check'. You should get the following output below. In the example below, the source code download bundle was unzipped to the 'C:\tools\xmlpublish' directory.
- Use the command 'runAnt -help' to display all the possible options or just take a look at the batch file code.
- Enable JavaScript in your browser if you want the scriptaculous (JavaScript libraries) Accordion effect to work in the generated HTML file. See
...
set JAVA_HOME=C:\progra~1\java\jre1.5.0_09
...
set ANT_HOME=C:\progra~1\apache-ant-1.6.5
C:\> cd \tools\xmlpublish
C:\tools\xmlpublish>runAnt.cmd -check
C:\tools\xmlpublish>
Setting JAVA_HOME and ANT_HOME.
==========================================================
Checking Java Version
==========================================================
java version "1.4.2_12"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_12-b03)
Java HotSpot(TM) Client VM (build 1.4.2_12-b03, mixed mode)
==========================================================
Checking Ant Version
==========================================================
Unable to locate tools.jar. Expected to find it in C:\progra~1\java\j2re1.4.2_12\lib\tools.jar
Apache Ant version 1.6.5 compiled on June 2 2005
C:\tools\xmlpublish>runAnt.cmd
C:\tools\xmlpublish>runAnt.cmd -help
C:\tools\xmlpublish>
==========================================================
Using runAnt.
==========================================================
Type the following:
"runAnt -build" : To run the supplied build script.
"runAnt -check" : To check the versions of the Java JDK and Ant installed.
"runAnt -help" : To get this message.
To produce an HTML document using the Ant build tool, run your 'runAnt.bat' batch script from the bundle download directory using the command, 'runAnt -build'.
C:\tools\xmlpublish>runAnt.cmd -build
C:\tools\xmlpublish>
==========================================================
Running Ant.
==========================================================
Unable to locate tools.jar. Expected to find it in C:\progra~1\java\j2re1.4.2_12\lib\tools.jar
Buildfile: build.xml
clean:
[echo] build.dir = ./build
[delete] Deleting directory C:\tools\xmlpublish\build
[echo] publish.target.dir = ./complete
[delete] Deleting directory C:\tools\xmlpublish\complete
init:
[echo] start time = November 23 2006
[echo] build.dir = ./build
[mkdir] Created dir: C:\tools\xmlpublish\build
[echo] publish.target.dir = ./complete
[mkdir] Created dir: C:\tools\xmlpublish\complete
generate:
[echo] generate.debug:0
[echo] start.touch.time:23/Nov/2006 10:35 PM
[xslt] Processing C:\tools\xmlpublish\src\xml\article.xml to C:\tools\xmlpublish\build\xmlpublish.htm
[xslt] Loading stylesheet C:\tools\xmlpublish\src\xsl\articleHtml.xsl
copyfiles:
[copy] Copying 15 files to C:\tools\xmlpublish\complete
copyimages:
[copy] Copying 4 files to C:\tools\xmlpublish\complete
BUILD SUCCESSFUL
Total time: 1 second
If the process is successful, go to the 'complete' sub-directory and double-click on the 'xmlPublishing.htm' file. Your browser should open, displaying your spanking brand-new generated copy of this article.
If the process fails, and it shouldn't if you are using the sample, the output screen will also give details of the cause of the error.
C:\tools\xmlpublish>
==========================================================
Running Ant.
==========================================================
Unable to locate tools.jar. Expected to find it in C:\progra~1\java\j2re1.4.2_12\lib\tools.jar
Buildfile: build.xml
clean:
[echo] build.dir = ./build
[delete] Deleting directory C:\tools\xmlpublish\build
[echo] publish.target.dir = ./complete
[delete] Deleting directory C:\tools\xmlpublish\complete
init:
[echo] start time = November 23 2006
[echo] build.dir = ./build
[mkdir] Created dir: C:\tools\xmlpublish\build
[echo] publish.target.dir = ./complete
[mkdir] Created dir: C:\tools\xmlpublish\complete
generate:
[echo] generate.debug:0
[echo] start.touch.time:23/Nov/2006 10:35 PM
[xslt] Processing C:\tools\xmlpublish\src\xml\article.xml to C:\tools\xmlpublish\build\xmlpublish.htm
[xslt] Loading stylesheet C:\tools\xmlpublish\src\xsl\articleHtml.xsl
[xslt] C:\tools\xmlpublish\src\xsl\articleHtml.xsl:304:47: Fatal Error! Could not find template named: buildImage
[xslt] : Fatal Error! Fatal error during transformation Cause: Fatal error
during transformation
[xslt] Failed to process C:\tools\xmlpublish\src\xml\article.xml
BUILD FAILED
C:\tools\xmlpublish\build.xml:92: Fatal error
during transformation
Total time: 0 seconds
Section 4: Now for the explanation ...
-
XML structure=Document structure
The XML document structure, order, position of elements should be the same in the transformed document. This just makes it easy to edit the XML document in a text editor, makes it easy to have a good idea of the resulting document structure and helps with debugging XSL errors. But that doesn't stop you from changing it in the transforming XSL, it just means less XSL code.
-
Use short, descriptive, meaningful tag names
A 'list' tag groups different kinds of other tags.
-
Use plural tag names to group singular tag names
For tags grouping other tags of the same type, use plural names. So the 'references' tag will always contain the 'reference' tags. Same applies for the 'definitions' tag etc.
-
Create the tags to simplify the XSL code
To simply the XSL code, I have 'text' tags surrounding text so that I don't have to include a lot of formatting logic.
-
Declare and use variables
Don't hard code values in your XSL code. Yes, it is quicker but a lazy and sloppy practice. Even if you have a photographic memory, you cannot remember every single line you wrote and, more importantly, why. Plus I'm sure you have better things to use to clog your memory banks. Using variables means fewer changes to the code, therefore less syntax and logic errors. It also makes the code easy to read and maintain, so that you don't need a photographic memory.
-
Be consistent
Use attributes to supply specific information about a particular tag and make them optional. If I'm checking any attribute of a tag in my XSL code, I have to assume that it may not be present and code accordingly. I can also use the same XSL code, if the attribute is present on more than one tag e.g. the title attribute.
-
Comment, comment, comment
You can never have too many comments. For XSL I try to keep my comments on the same line as the code, if it fits on one line, to reduce the blank lines in the transformed HTML document.
-
Include debugging code
You can never do too much testing. See my tip on using 'xsl:comment' for debugging.
-
Don't get too hung up on frameworks and standards
Frameworks and standards are there to help not hinder you, but they are not a 'silver bullet'. For instance, I could have used DocBook XML and XSL for this exercise. If you can code something yourself that fully meets the requirement, follows best software practice and is a more productive, predictable use of your time that does the job, just do it.
XML dialect (or XML tag set) for the article

article (root tag)
copyright (child tag) - Contains text only.
terms (child tag) - Contains text only.
title (child tag) - Contains text only.
subtitle (child tag) - Contains text only.
summary (child tag) - Contains text only.
sections (child tag) - Contains a set of one or more 'section' tags.
references (child tag) - Contains a set of one or more 'reference' tags.
definitions (child tag) - Contains a set of one or more 'definition' tags.

sections (child tag)
section (grandchild tag) - Contains other various tags.
The optional 'type' attribute is used to apply different presentation styles.
section (grandchild tag)
subsection (great grandchild tag) - Is exactly the same as a section.
text (great grandchild tag) - Contains text only.
The optional 'stack' attribute is used to apply different float
presentation styles.
image (great grandchild tag) - Contains a 'url' tag and a 'description' tag.
The optional 'stack' attribute is used to apply different float
presentation styles.
url (great great grandchild tag) -
Contains the full URL that identifies the source of the image
description (great great grandchild tag) -
Contains text that describes the external reference and
is used for the alt and title HTML tags.
code (great grandchild tag) - Contains text only, for displaying code.
The optional 'stack' attribute is used to apply different float
presentation styles.
output (great grandchild tag) -
Contains text only, for displaying the resulting output of any code.
reflink (great grandchild tag) -
Contains an 'id' attribute that connects to a 'reference' tag.
list (great grandchild tag) -
Contains a set of any of section 'grandchild' tags as well as the 'item' tag.
The 'ordered' attribute indicates whether the list is numbered or bullets.
ordered='true' means 'item' tags are numbered, otherwise they are bullets.
item (great great grandchild tag) -
Contains text and is formatted as a bullet or number.

references (child tag)
reference (grandchild tag) - Contains a set of 'title', 'url' and 'description' tags.
reference (grandchild tag)
title (great grandchild tag) - Contains title of the external reference.
url (great grandchild tag) -
Contains the full URL that identifies the source of the external reference.
description (great grandchild tag) -
Contains text that that describes the external reference and
is used for the alt and title HTML tags.

definitions (child tag)
text (grandchild tag) - Contains text only.
definition (grandchild tag) - Contains a set of 'text' and
one or more 'url' tags.
definition (grandchild tag)
text (great grandchild tag) - Contains text only.
The optional 'stack' attribute is used to apply different float
presentation styles.
url (great grandchild tag) -
Contains the full URL that identifies the source of the external reference.
The 'type' attribute defines whether it is a definition or tutorial URL.
XSL code for the article XML dialect
rules I've defined above on the article XML dialect to produce an HTML version of this article. It should be easy to follow and I've included code that uses alternate XSL code to do the same thing. I also have XSL code specifically for generating the scriptaculous accordion panel effect like 'buildPanel'.

xsl:param 'libDebug' - debugging input parameter to the XSLT processor
supplied externally from Ant.
xsl:param 'timeStamp' - time stamp input parameter to the XSLT processor
supplied externally from Ant.
xsl:template match='n' - Where 'n' is an XML tag/node.
For each XML tag that is 'matched' from the XML
document, the XSL code in the matched template is run.
xsl:template name='m' - Where 'm' is a custom XSL template.
The XSL code is only run when the template is
explicitly called using 'xsl:call-template' element.
These templates are prefixed with the word 'build'.
Section 5: What next?
References
Download Java J2SE Runtime Environment (JRE) 1.4.2 or greater *![]()
Download source bundle for this article*![]()
Scriptaculous cross-browser user interface JavaScript libraries.*![]()
Learn XML in 11.5 minutes by L.C. Rees.*![]()
What is XML?
Definition:
http://en.wikipedia.org/wiki/XML
FAQ:
xml.silmaril.ie/basics/whatisxml/
Tutorial:
http://www.w3schools.com/xml/default.asp
What is XSL?
XSL describes how an XML document should be displayed. XSL consists of three parts
XSLT - a language for transforming XML documents
XPath - a language for navigating inside XML documents
XSL-FO - a language for visually formatting XML documents
Run the sample in the bundle download, if you can't wait.
Definition:
http://en.wikipedia.org/wiki/Extensible_Stylesheet_Language
What is XQuery?
Definition:
http://en.wikipedia.org/wiki/XQuery
What is DTD?
Definition:
http://en.wikipedia.org/wiki/Document_Type_Definition
Tutorial:
http://www.w3schools.com/dtd/default.asp
What is XSD?
Definition:
http://en.wikipedia.org/wiki/XSD
What is an XSLT processor?
What is a build tool?
Definition:
http://en.wikipedia.org/wiki/Build_tool
What is an XML dialect?
Technorati Tags: XML XSL Document Publishing