WebReference.com - Part 1 of Chapter 10 from Professional PHP4 XML, from Wrox Press Ltd (1/7)
Professional PHP4 XML
Programming XML Applications
With a very broad vision we may classify programming tasks using XML into two major categories:
- Storing and retrieving XML
- Processing XML
Storing and retrieving XML is detailed in Chapter 13.
Up till now in this book we've covered the core standards for XML processing--XPath, SAX, DOM, and XSLT. In this chapter we are going to study different XML processing problems and study different solutions to these problems using the tools we've mastered in the previous chapters.
We need to process XML data from some source since it is useful to our application. We are going to study some very common problems that arise when processing an XML document. For each problem we'll discuss different options to solve the problem and the scenarios where each solution is better than the others.
The assumption here is that we are in the middle of a large application, and maintaining it is an important issue, hence, we'll be focusing on OOP solutions most of the time. We'll also observe some patterns of OOP that can be applied to programs that process XML.
When it comes to transforming XML, we have an XML file or XML data source that we need to transform. This can be achieved in many different ways, for example:
- Transform XML into HTML to be displayed in a browser
- Transform XML into a PDF file and store it on the disk
- Transform XML into objects that are going to be stored in an OO database
- Transform XML into SQL scripts to insert data in a relational database
- Transform an XML file from a given vocabulary to a different vocabulary
- Transform XML into proprietary message syntax to be sent over some communication network
- Transform XML into a text report that will be sent by e-mail
- Transform XML into an SVG chart to be used in a presentation
- Transform XML into an XSLT stylesheet that will be used to perform a transformation or validation of another XML document
Transforming implies reading XML, peeking at the content, and writing something that may or may not be XML.
To illustrate different solutions, we'll work with a very simple example. Let's use the following XML file describing some applications coded by a software company:
<?xml version="1.0" ?> <!-- This XML file is an example --> <apps> <application id="1"> <name>Editor</name> <author>John</author> <bugs> <bug> <!-- This is one bug --> <desc>Foo</desc> <sev>2</sev> </bug> <bug> <desc>Bar</desc> <sev>3</sev> </bug> </bugs> </application> <application id="2"> <name>Compiler</name> <author>Peter</author> <bugs> <bug> <desc>Foo Bar</desc> <sev>5</sev> </bug> </bugs> </application> </apps>
Our transformation task will be to transform all the
<name> elements to a
<name><b>text</b></name> format where all the text is uppercase. After transformation, the sample file would look like this:
<?xml version="1.0" ?> <!-- This XML file is an example --> <apps> <application id="1"> <name><b>EDITOR</b></name> <author>John</author> <bugs> <bug> <!-- This is one bug --> <desc>Foo</desc> <sev>2</sev> </bug> <bug> <desc>Bar</desc> <sev>3</sev> </bug> </bugs> </application> <application id="2"> <name><b>COMPILER</b></name> <author>Peter</author> <bugs> <bug> <desc>Foo Bar</desc> <sev>5</sev> </bug> </bugs> </application> </apps>
We can transform XML documents using:
Created: August 12, 2002
Revised: August 12, 2002