XML and PHP Simplified - Formatting XML Documents | WebReference

XML and PHP Simplified - Formatting XML Documents

By Leidago Noabeb


[next]

In the last article, we looked at how to use XML, PHP and MYSQL. We in particular looked at how to integrate database information into an XML document. In this article we will be looking at:

  • How to view XML documents in web browsers; more specifically we will be looking at how to format them.
  • Then, how to parse or read XML documents using PHP strings and functions.

Parsing XML Documents

In the last article, we created a small program to demonstrate how to create an XML document using ordinary PHP functions. This is all good and well when creating an XML file, the moment you try to do something more complicated than simply creating a file, you will undoubtedly run into problems. In this section, we will look at how to read the contents of an XML file using the XML functions of PHP4. We have already discussed the XML functions so let's use an example to demonstrate how they work. First, we get the XML file that we want to use. The idea is to create an XML parser. This will then enable us to read and write to an XML document. First we read the contents of the document into a string using the file_get_contents() function, then we use the xml_parse_into_struct() function to turn the string into an array. Let's get to it:

The result will show both the keys array, which should look something like this:

And the values array, which should look something like this:

Viewing an XML document

In our last article, we created an XML document called myxml.xnl. When we try to view this document in a web browser, you will see only the text of the document. Take a look a the screenshot below to get an idea of what I'm talking about.

Figure 1

You can actually make your XML documents more presentable than this. Just as HTML pages have Cascading Style Sheets (or CSS as it is commonly known) to present HTML data in a neat and more presentable fashion, XML has XSL. XSL stands for eXtensible Style Sheet and it does for XML what CSS does for HTML. I've created an XSL file called generic. It has the following code:

This file is specifically designed to work with the tags that are created in the XML document. Currently the myxml.xml file has the following code:

To connect the XML document to the style sheet we simply add the following line of code to it:

This line tells the browser to apply the styles that are contained in a style sheet called generic.xsl. Therefore, your new xml document will now have the following code. Take note that we place the link to the style sheet right after the version declaration:

Now take a look at what the XML document looks like with the styles applied:

Figure 2

Now, since this article is not about XSL, but about XML we will not go into too much detail when looking at the XSL code. What we will look at is the point at which the XML document elements and attributes are referenced in the XSL code. We have three elements, namely, email, address, and name. Take a look at the XSL code.

First we set the style headers. Notice that the XML version is again declared:

Then the HTML table is created. If you have been using HTML before it should be easy for you to recognize the code that describes the rows and columns of the table. We also set the Header title of the table here:

The next step is to create a hyperlink. Normally a hyperlink would be written like so <a href="something">name</a>. Try to see the similarities; we want to link the name of the contact to an email address:

We then close the hyperlink:
</a>

Now we've completed the hyperlink, if you point the cursor at the name of the contact you should see the email address of that contact. The only other element left to write is the address of the contact; we want to write that next to the linked name:

The above code simply lays out the name of the element to which a style should be applied.


[next]