Building a CMS, part 2: Taking it to the server - A Word About Namespaces
(Page 4 of 5 )
Namespaces are a much misunderstood part of XML.
Namespaces are a way to identify tags that are part of a specific XML based language, when mixed with tags from other XML based languages.
XSL, XUL, SVG and MathML are all XML based languages. All these and more (including those that you come up with) can live inside a single XML document, but this can cause a problem. What if one or more of these languages use tags with the same name? This could easily happen, and the solution is namespaces.
Each markup language is given a unique string to identify it. The question is, what is the best way to easily generate a unique string for identifying a markup language? The solution that is used for XML is to use a URI (Universal Resource Identifier).
The URI in a namespace declaration looks like a URL that you would type in a browser. This is where the confusion about namespaces starts. Though you could certainly put something at this location, that isn't the purpose of a namespace, however.
Nobody but the Mozilla Foundation, for instance, owns the Mozilla.org domain. This makes it a unique and protected identifier for that organization. That being the case, using the following line to identify Mozilla XUL in an XML document creates a string that is unique.
HTML also has a namespace which allows you to use HTML (given the proper user agent) in an XML document. An HTML tag could then live happily in the same document with other markup languages in an XML document.
In the case of our XSL stylesheet, we only need a namespace for XSL.
An XSLT Stylesheet
We start out with an XML declaration and follow with the stylesheet root element. The stylesheet element has a beginning and ending tag and encompasses the entire contents of the document. The stylesheet tag is followed by an output tag which tells the XSLT processor about the intended output.
<?xml version='1.0' encoding='iso-8859-1'?>
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:output method='html' version='1.0' encoding='iso-8859-1' indent='no'/>
The next tag is a main template. This template generally contains the HTML markup that you want in your document that is not dictated by the XML source but rather just printed as is. In-between the body tags is an xsl:apply-templates tag that inserts all the templates we are about to write into the result document. The values you see in match and select attributes are XPath locations. XPath looks a lot like file paths in a file system in it's simplest form, except in this case, instead of navigating a file system we are navigating an XML document. A "/" refers to the root XML element, otherwise you can use the name of an element to select it. XPath is much more powerful than that, but for our current purposes this is all we need to know.
<title>Company X Press Release</title>
<link href="release.css" rel="stylesheet" type="text/css"/>
Next are all the templates for dropping XML source information into the HTML result document. We use the xsl:value-of tag to get the value of each XML source tag. There is a template for each press release feature that we need. Notice that "." is used below, which in XPath means the currently selected node.
<!--You can call templates by name if you wish-->
<xsl:template match="company" name="company">
<!--Just as in many programming languages, xsl has a for each loop construct-->
<!--Inserting values from XML into html attributes is a little different in xsl-->
By using this stylesheet, all of the information in the XML source document is formatted and styled into proper HTML.
When authoring an XSLT stylesheet, I would recommend using an XML editor, especially one that is designed to assist the development of XSLT documents. This will help you keep your syntax correct and test your transformations without having to do it with the server and browser. Complex transformations likely will need to be debugged in much the same way you would debug other code.
More Web Hosting How-Tos Articles
More By Chris Root