Web Hosting How-Tos
  Home arrow Web Hosting How-Tos arrow Page 4 - Building a CMS, part 2: Taking it to t...
Web Hosting Articles  
Web Hosting FAQs  
Web Hosting How-Tos  
Web Hosting News  
Web Hosting Security  
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
ASP Web Hosting  
ASP.NET Web Hosting 
Budget Hosting 
Coldfusion 
Colocation 
Mobile Linux 
APP Generation ROI 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Reseller Web Hosting 
Shared Hosting 
Small Business Hosting 
Virtual Private Servers 
Windows Web Hosting
 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WEB HOSTING HOW-TOS

Building a CMS, part 2: Taking it to the server
By: Chris Root
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 1
    2005-03-30

    Table of Contents:
  • Building a CMS, part 2: Taking it to the server
  • Generating the XML
  • Creating the Markup
  • A Word About Namespaces
  • PHP XSLT

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    <window
    xmlns="http://www.mozilla.org/keymaster/gatekeeper/
    there.is.only.xul">

    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.

    <xsl:template match="/">
    <html>
    <head>
    <title>Company X Press Release</title>
    <link href="release.css" rel="stylesheet" type="text/css"/>
    </head>
    <body>
    <xsl:apply-templates/>
    </body>
    </html>
    </xsl:template>
    </xsl:stylesheet>

    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.

    <xsl:template match="mainheading">
    <h1><xsl:value-of select="."/></h1>
    </xsl:template>

    <xsl:template match="date">
    <div>
    <span><xsl:value-of select="day"/>
    </span>
    <span><xsl:value-of select="month"/>
    </span>
    <span><xsl:value-of select="year"/>
    </span>
    <span><xsl:value-of select="time"/>
    </span>
    </div>
    </xsl:template>

    <!--You can call templates by name if you wish-->
    <xsl:template match="company" name="company">
    <strong><xsl:value-of select="header"/>
    </strong>
    <p><xsl:value-of select="description"/>
    </p>
    </xsl:template>

    <!--Just as in many programming languages, xsl has a for each loop construct-->
    <xsl:template match="content">
    <div class="content">
    <strong><xsl:value-of select="mainsubhead"/>
    </strong>
    <p><xsl:value-of select="maincontents"/>
    </p>
    <xsl:for-each select="company">
    <xsl:call-template name="company"/>
    </xsl:for-each>
    </div>
    </xsl:template>

    <!--Inserting values from XML into html attributes is a little different in xsl-->
    <xsl:template match="contactinfo">
    <div>
    <div><xsl:value-of select="contactname"/>
    </div>
    <div><a><xsl:attribute name="href">
    <xsl:value-of select="contactmail/@path"/>
    </xsl:attribute>
    <xsl:value-of select="contactmail"/></a>
    </div>
    <div><a><xsl:attribute name="href">
    <xsl:value-of select="contacturl/@path"/>
    </xsl:attribute>
    <xsl:value-of select="contacturl"/>
    </a>
    </div>
    </div>
    </xsl:template>

    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


     

    WEB HOSTING HOW-TOS ARTICLES

    - Choosing a Web Host for Your WordPress Blog
    - Connecting to a Server using SSH: the Fundam...
    - How to Expand a Simple Website
    - Practical Virtualization with VirtualBox
    - Other Uses for Your Web Hosting Server
    - Hosting Your Own Website: Reliability
    - Introduction to Hosting Websites
    - Choosing a Website Host
    - How to Choose a Budget Web Host
    - URL Redirection
    - How to Link a Domain Name to a Dynamic IP
    - How to Set up a Simple Website
    - Choosing the Right Kind of Web Hosting
    - Introduction to Choosing the Right Web Host
    - Strategies for Creating Domain Names






    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek