XML
PHP/Drupal code to generate CMS content as XML
- Trigger the alternative theme that writes XML
- Write the theme that renders XML
1)
In your page.tpl.php, add this code at the top
<?php
if (($_GET["format"])==("xml")) {
include 'xml.page.tpl.php'; /*call the alternative template file */
return; }
?>
Do the same for node.tpl.php and this time point to an alternative node.tpl.php
<?php
if (($_GET["format"])==("xml")) {
include 'xml.node.tpl.php'; /*call the alternative template file */
return; }
?>
Example xml.page.tpl.php and xml.node.tpl.php (the naming format is arbitrary and can be anything)
<?php
$xml_header='<?xml version="1.0" encoding="ISO-8859-1"?>';
header('Content-type: text/xml');
print $xml_header;
?>
<drupal_page>
<?php print $content; ?>
</drupal_page>
<node>
<link><?php print $node_url ?></link>
<title><?php print $title ?></title>
<content><?php print $content ?></content>
</node>
Please note that this code is just an example of greater things that you can accomplish by creatively theming CMS data. You call pull-in more fields, and even take the resultant XML and manipulate it using XSLT, or consume it into Adobe flash for presentation... If you come up with some creative application, please share them with th rest of us by commenting on this website, or on Drupal.org: Drupal to XML: By theme, Server-side manipulation
How to generate valid XML from Drupal CMS nodes
There have been many discussions and requests for how to generate Drupal CMS content/nodes. In this document is a review of the reasoning and the steps that facilitate generating XML dat from a Drupal CMS.
Well formed-ness/validity
XML trees must be well formed and valid to a DTD. DTD declaration is optional and in this situation, we will not need one as the tree we ae generating is relatively simple. However, if we are to share the generated XML data with others, it will be necessary to have a DTD to enable others to know the schema of the XML data
CDATA, XHTML within XML - advantage of having valid HTML
Since there is almost certainty that the XML data will contain other angle brackets, either the data must be well-formed HTML/XHTML (this will prevent the parser from generating errors) or in situations where this is not assures, it is necessary to write a DTD and set the contents as CDATA to exclude the HTML/XHTML from validation. When it is possible to control the output, I would always suggest having valid XHTML data to enable the XML data consumer application to dig deeper into the data to find certain elements (image, H titles, forms, etc). Valid XHTMl qualifies as XML, and so you will suddenly have sub-sets of your content accessible to XSLT and backend applications.
Drupal Strategy to generate XML data from the CMS
The presentation / theming layer in Drupal is best suited for generating XML data from CMS nodes. This can be accomplished by either creating a fresh theme, or adding theme template files to generate XML. In most applications, the content within a Drupal site has to be presented both as human browser-readable XHTML along with outputting XML. In the demonstration at hand, I have created template files to generate the node title, link to XHTML and node content as the three XML data nodes (Use your server-side logic to generate the correct content mime-type, text/xml for applications that need to detect the mime-type):
<drupal_page>
<node>
<link></link>
<title></title>
<content></content>
</node>
</drupal_page>
Demonstration
Browse to any page on cmsproducer.com and add a the querystring value format=xml such as:
http://cmsproducer.com?format=xml
or
http://cmsproducer.com/XML-XSLT-XHTML-Transformation-PHP-ASP-NET?format=xml
It will generate the same page with just two nodes in the XML tree:
Assembling XML data from multiple subset XML document files using XSLT
XSLT can be used to manipulate and combine data from multiple XML sources into a new file. In this document, we will review the process of calling using smaller XML data sources to build a new XML data source.
Including an XML data set into another
If we step away from the details of how to code the process, the process is a common programmatic procedure that is used in many languages to declare data sources or logical function definitions, and then use them within the local logic or data structure.
If you have one or more files with data that you would like to include as part of a larger file
Example:
If the following is the content of navigation.xml
<mynavigation>
<menu_item>
<label>XSLT to manipulate XML</label>
<link_href></link_href>
<description>This resource provides an overview and a detailed discussion on how to use XSLT to manipulate XML data</description>
</menu_item>
<menu_item>
<label>XML format future</label>
<link_href></link_href>
<description>Discover how XML is in the future of boundless data exchange and storage</description>
</menu_item>
<mynavigation>
At the top of your new document, and within a DOCTYPE declaration, and then within the data contained in this document, insert the imported data as a node as shown in the example below.
<!DOCTYPE xsl:stylesheet [
<!ENTITY main_navigation SYSTEM 'navigation.xml'>
]>
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<div id="main_navigation">
&main_navigation;
</div><!-- / main_navigation -->
</xsl:template>
</xsl:stylesheet>
The syntax in this case is very similar to creating a character entity (such as & or ©) and then using it in the document
Modular XHTML: XML Flexibility and Extensibility combined with HTML Familiarity
Modular XHTML
The concept and W3C draft of modular XHTML fills the need XHTML (version 1.1) makes it possible for device manufacturers, and platform designers to breakdown the single-block nature of XHTML 1.0 and rebuild it as a composition of the resulting sub-set of modules in addition to new custom modules that may be created to accommodate the extended needs to a particular device or content subjected (MathML as a specialized form of XHTML to represent mathematical relationships between values and variables).
A major advantage of using modular XHTML to represent web content is the reality that as long as the document has a valid driver and/or DTD, it will be understood and parsed by any XHTML capable client without the need to update that client with support for the new markup elements and attributes. The hierarchy and relationship between elements, and with new and existing attributes is defined by the DTD which is called at the beginning of the document.
Combination of Modules
As you may have already imaged it, one does not have to reinvent the wheel to create a custom Modular XHTML Language. Preexisting modules that are part of the standard XHTML 1.0, as well as other modules that have been created by others (for which there is an available DTD) can be combined and extended with other custom addition to create the markup needs at hand.


