The Power Of Three: XML, XSLT and PHP -- Part I
Ray Hunter on 2002 May 05
Ray Hunter on 2002 May 05
Part 1: The Power of XML
This is an introduction and presentation into the value and use of PHP and XML in a single environment. This is a series of articles that take a journey into the basics of XML and the fundamentals of PHP
What is Xml?
In all simplicity, XML stands for eXtensible Markup Language. So what does it really mean? XML itself is a markup language similar to HTML that is for documents containing some form of structural data. When referring to the word documents, I am referring to many types of definitions: images, database transactions, spreadsheets, mathematical equations, server APIs, vector graphics and the many other types of documents out there today.
XML is a standard defined by the World Wide Web consortium (W3C) and you may read further about XML at their website http://www.w3c.org/XML or you can review the 10 basic points of XML at http://www.w3.org/XML/1999/XML-in-10-points.
In my terms of thinking, XML is a way to organize data in a universal format that provides solid structure and form for sharing data over the internet, between applications, and between various platforms.
Why use Xml?
There are various reasons for using XML, some of which are: platform independence, many applications already understand XML, XML is simple to use (yes it is) and, my favorite one: "It's FREE!". XML provides developers the ability to exchange data between applications and systems...allowing for a common format or protocol in which to communicate.
Getting started with Xml
The first step in using the power of XML is to know how to write XML files.
Let me show you a basic XML file (with out the data):
<?xml version="1.0" encoding="UTF-8"?> <person>
<firstname></firstname>
<lastname></lastname>
<email></email>
<phone></phone>
<department></department>
</person>
First, let us take a closer look at the tags:
Notice similar they are to the tags we use for HTML. Apparently, the developers of XML have chosen to create XML as a markup language and to have it looking as closest as possible to HTML. This allows for the simplicity and ease of use.
Let us look at the first line:
<?xml version="1.0" encoding="UTF-8"?>
Here we are stating that this is an XML file, the version of xml and the encoding type. The encoding we have specified here is UTF-8. PHP supports up to three character-encoding types: US-ASCII, ISO-8859-1 and UTF-8. By default the PHP parser has a source encode, that is UTF-8. Character encoding is somewhat advanced for this type of tutorial and I would suggest reading up on XML to fully understand the types of character encoding available.
It suffices to say that you will want to start all of your XML files with this line. After this line, you are free to create tags as you see fit.
The second line starts with a tag called person. I have created this tag, and it is referred to as the root node. This means that everything
in this XML file needs is to be contained in between the person open and close tags.
We would receive an error if we had something like this:
<?xml version="1.0" encoding="UTF-8"?> <person>
<firstname></firstname>
<lastname></lastname>
<email></email>
<phone></phone>
</person>
<department></department>
Your error in this case would sound something like "not well-formed filed".
Remember that XML follows a strict structural format, meaning that all tags need a closing tag and there needs to be a root node. Also, note that you cannot change the tag placement. Let me explain to you why with another example:
<?xml version="1.0" encoding="UTF-8"?> <person>
<firstname><lastname>
</firstname></lastname>
<email></email>
<phone></phone>
<department></department>
</person>
Have you guessed it? Here we would get an error message because the closing tag the parser is expecting for the firstname comes after the lastname tag opens.
Let us now move to a more complex example of an XML file:
<?xml version="1.0" encoding="UTF-8"?> <person>
<name>
<firstname></firstname>
<middlename></middlename>
<lastname></lastname>
</name>
<email></email>
<phone>
<home></home>
<cellular></cellular>
</phone>
<department></department>
</person>
Notice how we have added some more tags between other tags. Wasn't it logic? The only thing to remember is that we must continue with the strict structure in the document. We can go on adding the tags between tags until the infinite.
In this example, I have added a new tag "name". Inside the name tag, I have added three additional tags: firstname, middlename, and lastname. Here we are splitting the name into subgroups or subcategories. Also with the phone tag, I am adding two additional tags: home and cellular.
Now, as we know the syntax, let us finally add some data to the xml file we have just created.
Here is the XML file with data:
<?xml version="1.0" encoding="UTF-8"?> <person>
<name>
<firstname>John</firstname>
<middlename>Michael</middlename>
<lastname>Stevens</lastname>
</name>
<email>jstevens@company.com</email>
<phone>
<home>999 999-9999</home>
<cellular>888 888-888</cellular>
</phone>
<department>Software Engineering</department>
</person>
Got it? That was easy, wasn't it?
Setting up PHP for Xml
In order to handle XML files in PHP we need to compile PHP with xml. This will give the optimal performance from PHP. Although XML already comes as a built in extension for PHP, it is recommended to recompile your PHP installations with the XML option.
How do we do that? Very simple. On Unix based systems we will need to add the --with-xml option when and run the configure command.
Example:
./configure --prefix=/usr/local/php --with-xml
This example is a basic one, which adds the xml extensions into the PHP parser.
While for our "tricky" windows, we should just make sure we have the xmlparser.dll" loaded.
To verify that your PHP installation supports (configured with) XML, just make a silly PHP file with the "phpinfo()" function inside and display this file in a web browser. If you see a XML section with additional information in it - means you have XML supported.
To be continued...
After we created some basic XML files, we are now ready to move on to XSLT with PHP. Next, I will show you how to translate and (finally) layout the XML data based on the very example we did today.
Stay tuned for next week's Part 2: The Power of XSLT!
Ray "BigDog" Hunter
rhunter@hunterhysteria.com
