DOMi is an improved DOM object for PHP that combines and improves upon the capabilities of three existing PHP objects: DOMDocument, DOMXpath and XSLTProcessor. These three objects, combined with the XSL language, are the foundation of the next generation of web development.
The current system of using PHP's echo function to print out individual pieces of HTML is being rendered obsolete by the power and flexibility of an XSL driven system. DOMi is an object that is designed to make this process easier than ever before. A DOMi object can convert complex PHP data structures into an XML tree through a single command, and then render that XML tree against an XSL stylesheet through another command.
The traditional HTML echo system worked well for a time, but it has a few drawbacks. It requires a constant intermingling of PHP and HTML, it often requires derivative data to be generated, and PHP is unable to quickly process a large complex data structure. By converting the PHP data into an XML tree, and then using an XSL stylesheet to convert to HTML, all of these issues are resolved.
The PHP scripts are no longer going back and forth into a language with a radically different syntax, while the HTML is being merged into a language that has an identical syntax: XSL. This makes code easier to read and understand, and keeps each script more pure.
XSL can use XPath to quickly navigate and extract data from a complex XML tree, and thus reduces the need for derivative data. Each piece of data needs to be stored only once, and XSL can obtain any derivative data with ease. For example, if you had a list of users, and each user had a list of forum posts, and you wanted to obtain a list of all posts in a particular thread, you could use a XPath like the following to get that information.
Another advantage of using an XSL system is code cleanliness. When using HTML echo through PHP, it is often difficult to have well formatted HTML as the ouput. With XSL, a few declarations in the <xsl:output> node will tell the system to keep an incredibly clean layout. View the source code for this page as an example. This page is 100% XSL driven using the DOMi object.
The first step to using DOMi to create an XSL driven website is to include the DOMi class and create an instance of the DOMi object. DOMi's constructor requires a single parameter to be passed over. This parameter is used to set up the DOMDocument object that DOMi will use to build the XML tree. Three different kinds of parameters can be passed to create a DOMi object.
The first way to create a DOMi object is passing a string to be used as the node name of the root node of the XML document, as in the example below.
The second way to create a DOMi object is by passing a string that is the filename of a local XML document.
The third way to create a DOMi object is by passing a DOMDocument object, and DOMi will use this DOMDocument as it's primary DOMDocument
In all three events, DOMi is created with a built in DOMDocument, DOMXpath and XSLTProcessor. Each of these objects is a member property that can be accessed transparently through DOMi. For instance, if you wanted to use the DOMXpath::query method, you could access it in one of two ways
Both of these methods are identical. When an unknown method is requested of DOMi, it will check to see if that method exists within it's member objects, and if so, return the result of the member object invoking the requested method. Through this system, legacy support for old DOMDocument setups is 100%.
Once the DOMi object exists, the next step is to begin adding data to the DOMDocument, and the best way to do this is through the DOMi::AttachToXml method. This method will accept a PHP data structure and transform it into an XML tree and attach it to the DOMDocument inside DOMi. Currently, AttachToXml supports the following PHP data structures
In all cases except Array, DOMi will build a single node, insert the provided data as the node value, and attach it to the DOMDocument. In the case of an array, each element will be recursively attached.
AttachToXml accepts two required parameters and a third optional parameter. The first parameter is the data to be converted into an XML tree. The second parameter is the name to be used for the node. The third parameter is a DOMNode within DOMi's DOMDocument where the new node will be attached, this will default to the root node.
This code will make a new node named <server>, attach that to the root node of the DOMDocument, and create new nodes for each element within the $_SERVER superglobal array.
DOMi can import a stylesheet upon rendering, or a manual call can be made to add the stylesheet. If you plan to add upon rendering, which is a simpler, cleaner method, you may skip to the Rendering section.
As mentioned above, DOMi is capable of transparently accessing any of the methods of DOMDocument, DOMXpath and XSLTProcessor. This means that stylesheets are added through the same method as XSLTProcessor - the importStylesheet() method.
However, DOMi has a method that allows for easier creation of DOMDocument objects that are to be used as stylesheets. The GenerateXsl() method accepts either a string or an array of filenames that are to be included. This method will then dynamically create a DOMDocument that includes each of the provided stylesheets, which is useful when the list of stylesheets to be included will vary based on what page the user is looking at. GenerateXsl will, by default, then import these stylesheets, although a second parameter can be passed to disable this feature.
Once DOMi has been created, data has been added, and a stylesheet has been imported, the only thing left to do is render the page. DOMi::Render() is the method that DOMi uses to run the XSLTProcessor and send the output to screen. Render() accepts two optional parameters - the file location for an XSL stylesheet and a rendering flag. The first parameter can be used to provide the stylesheet upon rendering, if GenerateXsl() or importStylesheet() is not called. The second flag can be set to one of the following three values