Become a fan of Slashdot on Facebook

 



Forgot your password?
typodupeerror
×

No Nonsense XML Web Development with PHP 131

Alex Moskalyuk writes "PHP and XML seems like a marriage made in heaven. Powerful manipulation functions and support on the core language level in PHP5 combined with universal extensibility of XML make it a technology of choice for quite a few Web enthusiasts and companies out there. However, anyone inspired by PHP's ease of use can probably find a good cure from insomnia when facing with XML specs. With all the DTD's, XML Schemas, XSLT and XPath queries one can easily get the impression that the world is changing on them, and perhaps sticking to hard-coded HTML with PHP statements combined with SQL statements for data retrieval would be within the zone of comfort." Read the rest of Alex's review.
No Nonsense XML Web Development with PHP
author Thomas Myer
pages 354
publisher SitePoint
rating 9/10
reviewer Alex Moskalyuk
ISBN 097524020X
summary XML, XSLT, XPath and DOM primer for PHP developers


Thomas Myer's No Nonsense XML Web Development with PHP is an XML primer for those who have been exposed to PHP, but are yet waiting to appreciate the elegance of PHP+XML solutions. Throughout 10 chapters and 2 appendices Myer is introducing the reader to different aspects of XML, their best-practice implementations in LAMP (where last P stands for PHP) environment, and their relevance to the real world. For the real-world example Myer decides to guide the reader through writing a custom content management system - complete with publishing/admin interface, templating/presentation layer, search engine, RSS feeds and other commonly expected features.

The book is not an introduction to PHP, but it does assume that the Web developer knows what XML is, but has never dealt with it. So the first chapter just talks about properly parsing XML with IE and Firefox, validating an XML document, differences between a well-formed and a valid XML document. Overall, it provides a very good introduction to XML for someone who has never dealt with it, and could probably be skipped by developers with XML exposure.

Chapter 2, XML in Practice, goes into nitty-gritty details of XML, and 26 pages later the reader knows how to create an XML file to display in the browser, declare proper namespaces, attach a CSS file to existing XML file and display the proper XML+CSS file (look, Ma, no <html>!) in the browser. The author earns instant geek credibility by displaying Firefox screenshots, with the exception of IE screenshot whenever IE is discussed. At the end of the chapter the author takes us through the basic XSLT.

DTD's, XSLT and writing a practical PHP app take up the next three chapters, followed by XML manipulation chapters. JavaScript enthusiasts will probably find Chapter 6 pretty useful, as it discusses manipulating XML on the client side, working with XSLT, and creating dynamic site navigation based on the XML source. Chapter 7 is what one would expect from the book that has the words PHP and XML in the title - discussion of SAX, DOM and SimpleXML parsers, examples of their implementation, discussion of proper use cases for each one of the technologies. The SimpleXML subchapter also contains a good primer on XPath - a query language that allows the developer to provide the parser with a query to navigate down the XML document.

Chapter 8 takes the reader through RDF and RSS, discusses the ways the syndication feeds are used on the Web nowadays. Since throughout all these chapters we're building a content management system, this is the right time to add the RSS headlines functionality to the site. The next chapter discusses another practical implementation of XML on the Web - XML-RPC calls between the sites and proper ways of exchanging data via XML Web services. The chapter discusses SOAP, although not a whole lot, and just mentions REST as another way to implement Web Services. As a practical exercise, the author takes readers on a tour of building an XML-RPC client, server and connecting those two together.

The last chapter talks about using XML with databases. Native XML databases are discussed, but let's face it - most of the PHP development is done with relational databases anyway. Myer talks about exporting MySQL database contents into XML with phpMyAdmin and mysqldump. The first appendix includes function reference for SAX, DOM and SimpleXML parsing in PHP, while the second one completes the CMS project by providing the rest of the necessary files.

I found the author's style very easy to follow and approachable. The code samples are succinct and to the point, there are also no generic discussions, such as "Why PHP?" The project chosen for the practical implementation is a bit boring, but at the same time quite real-world. The screenshots are clear, and code examples are nicely highlighted. The errata is provided on the book Web site. Code archive is available as a single file download as well. The book site also provides 100% money back guarantee (less shipping and handling fees) to anyone who bought the title, and didn't feel like they were getting their money's worth.

However, there are a few drawbacks that I noticed as well. With topics like XSLT and XPath broken into several chapters and discussed in smaller chunks, it's hard to use the book as a reference later on. Appendix A with PHP function reference for XML parsing hardly seems like a worthy addition, since PHP manual page on the subject contains equivalent information with more real-life examples contributed by users.

With all that, the book is quite informative, educational and useful. The author manages to tackle quite a few difficult topics in 260 pages provided to him (the count excludes preface and appendices). However, kudos to the author for writing chapters on XML without sounding boring, redundant or too academic. I would highly recommend this book to anyone interested in developing PHP-driven Web sites that provide or consume Web services, work with XML data or generate XML for others to use."


You can purchase No Nonsense XML Web Development with PHP from bn.com. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.
This discussion has been archived. No new comments can be posted.

No Nonsense XML Web Development with PHP

Comments Filter:
  • by RobertB-DC ( 622190 ) * on Wednesday March 15, 2006 @02:00PM (#14925702) Homepage Journal
    From the bn.com reviews, a contrarian view:

    Michael, a web programmer, February 7, 2006,
    Almost worthless.
    Based on the title, one might presume that Myer and Marini wrote the book for people who are already familiar with PHP and XML and want to learn some advanced techniques for combining them. What he gets instead are long (relative to the book itself), superficial introductions to PHP and XML and tiny, trivial examples of their combination. Everything in the book is common sense to someone who already knows PHP and XML. What the book teaches to beginners, however, is effectively useless for its superficiality, so I'd discourage anyone, especially beginners, from reading this book, even if he receives it for free. Time also is too valuable to waste on this book. Read 'PHP and MySQL Web Development' by Luke Welling and Laura Thompson and 'XML 1.1 Bible' by Elliotte Rusty Harold. One can visit SitePoint's web site to find a list of their titles and then return to a vendor site to read product reviews. SitePoint books are generally sub-par. This book is no exception.


    Somewhere, someone at bn.com is shaking their head, wondering if this "reader reviews" thing is all that good a deal after all.

    (FWIW: I think the book looks like just what I need, with my n00b level of knowledge of PHP and XML but with hopes to put them together myself [magicnumbers.org], if I can just find the right feed.)
  • by temojen ( 678985 ) on Wednesday March 15, 2006 @03:06PM (#14926333) Journal

    The way you've described it, your site still has to run a script for each request. Here's an approach I've used:

    Semi-static data (faq, articles, etc) is stored in xml files. Site templates are PHP. Scripts and styles are handled in external files.

    There's a directory /view/articles that has HTML versions of the articles; it is writeable by the webserver, but .htaccess rules to reject any requests but *.html . When a change is made to the site layout or newsbar, all the contents of /view/articles are deleted. If a change is made to just one article, only that article's html is deleted.

    On 404, the errorDocument is a php script which:

    1. Verifies presence of the article XML file (returns a real 404 page if not)
    2. Turns on Output buffering
    3. Generates the page using the template, news file, article XML, etc
    4. Saves the contents of the output buffer to /view/articles
    5. gzips the output buffer and saves that in /view/articles
    6. Sends the contents of the output buffer to the client.

    Thus the XML/XSLT stuff only happens when changes are made and apache can just send the file

    I'm working on adding customer-specfic elements to each page. This is via Javascript+Cookies+AJAX so it doesn't need each page to be regenerated for each user. The few pages that are customer-specific but have few static parts are just plain-old PHP that includes the site template file.

  • by G)-(ostly ( 960826 ) on Wednesday March 15, 2006 @03:45PM (#14926666) Journal
    If XML is not for storing data, then why is the OpenDocument format XML?

    That defines data, it does not store it. As I noted in another response to the original poster, XML is not handling any of the storage in any way, the filesystem is handling all of those functions the same way it would if it was retrieving any other type of file. When OOo opens that file, it doesn't use the XML in any way to retrieve the data, it uses the XML as an interface between text-only representation on the disck and formatted screen representation of that file. In other words, it's a transfer medium to translate the plain-text data to an actual office document, nothing more.

    Somewhat more that (sic) column separation, perhaps?

    And a Dodge Viper is more than a Model T, but both objects still exist for the same intrinsic purpose. The fact that one is more advanced and better suited to certain purposes doesn't make them different things.

    XML has nothing to do with storing data. An XML file is not a "storage tool", and when people try to make it one, all they do is build a heirarchical DBMS, which is a system of data storage and retreival that failed at all but the most specialized applications back in the 60s when it was first attempted.
  • by leighklotz ( 192300 ) on Wednesday March 15, 2006 @03:49PM (#14926703) Homepage

    A lot of the middleware that converts data to HTML and back can go away when you use the right XML tools. XSLT does a good job of presenting static pages, and it can be fast if you cache the results as well.

    But for dynamic pages (and forms) XML to XSLT to HTML leaves some big gaps:

    1. The hierarchical XML data gets flattened out into name/value pairs in HTML form fields.
    2. For the return trip HTML->?->XML, XSLT doesn't work; you can't run the transform backwards.
    3. For dynamic pages, you're left with JavaScript or the dreaded "postback."

    These are some of the reasons we updated the W3C HTML forms module [w3.org] to take account of XML data directly.

    How does it fix the above problems?

    1. The hierarchical XML data your PHP or other server-side code outputs is transmitted directly to the web browser, where it remains while the user futzes with only the nodes that the form specifies. The middleware that converts the XML data to web browser data is just printing the XML.
    2. When the form results are submitted, they come back directly in XML, so there's no need to pick apart the name=value pairs and try to put them back into your data. The browser just posts the XML directly back as XML to your PHP or servlet.
    3. For forms, as the data changes, the UI changes with it. If a node disappears, or appears, or if a value changes, entire sections of UI can appear and disapear just by listing a dependency on that data. And if you want dynamic pages, you can use the background submission feature to retrieve instance data asynchronously, and the presentation changes automatically.

    Nice work if you can get it, you say? Well, as everyone knows Microsoft hasn't yet implemented XForms. (Heck, they haven't even implemented CSS, though we hear they do have it as a goal now.)

    So what can you do today:

    1. Use Mozilla or FireFox XForms 0.4 [mozilla.org]. It's a one-click install download from the Mozilla website. Yes, it's beta. Yes it has bugs. Yes, IBM and others are fixing them. But it's open source.
    2. Use FormFaces for most modern browsers (Firefox, IE, Safari, Opera) FormFaces [formfaces.com] is a cool JavaScript/AJAX application that you import into your web page with a one-line include, and it does everything described above. If you need cross-browser support right now, want dynamic AJAX forms, and want to interface to XML, this is your best bet, if you can tolerate a JavaScript program in your browser (i.e., it's done using AJAX). It's available under GPL and commercial licenses.
    3. Use Chiba for backend processing Chiba [sourceforge.net] is an open source Java-based back-end that converts your XHTML+XForms page into either an AJAX page or a static HTML page (good for Sec 503 compliance). Chiba is a great choice for applications that have a Java back end, as it puts less load on the browser than the large JavaScript engine of FormFaces, but I put it below FormFaces here because of the emphasis on PHP. (But, about half of Chiba is an XSLT transformation so a PHP port is possible.)
    4. Use Formsplayer as an IE plugin FormsPlayer [formsplayer.com] is a deluxe XForms processor plug-in for intranet applications using Internet Explorer, and has lots of other features as well, such as sidebar support.

    Here's a quick example:

    Let's suppose you have a book list you want to view, avaialble at http://example.com/books/list [example.com].

    <books>
    <book>
    <title>No Nonsense XML Web Development with PHP</title>
    <author>Thomas Myer</author>
    <book>
    ...
    <books>

    If you want to display this data

  • by G)-(ostly ( 960826 ) on Wednesday March 15, 2006 @04:32PM (#14927077) Journal
    No, XML should not be used in relation to storing data. You can build a system which uses XML to store data, I'm not contesting that, and I already mentioned what that technology is: it's called a heirarchicial DBMS, and they've long been known to be inferior tools.

    XML's purpose is not data storage, it's data description. People try to use those descriptive properties to generate applications which use XML stored on disk to store data within the stored file, but that's a waste. It's like trying to turn a car into a boat when you could just easily go obtain a boat. Sure, you can do it, but it's inefficient, error prone, and doesn't actually have any pros associated with it in 99.999999999% of all cases.
  • by G)-(ostly ( 960826 ) on Thursday March 16, 2006 @09:19AM (#14932069) Journal
    ...it is addressing the issues of data formats going out of date.

    But data formats aren't storage, they're representation.

    There are few inherent enforcements in relational systems - things constraints and foreign keys are optional
    There are many inherent enforcement in relational systems. There also happen to be many psuedo-relational system masquerading as something they're not.

    Also, you can't impose in a relational system the range of constraints you can impose with XML, such as ordering of data.

    That's not true, you can order your datasets in any way you want* when you retrieve them as long as you put them in in a meaningful way to begin with (or, if your order is totally meaningless, keyed it to an autonumber/identity attribute), they're just not stored on the physical medium in an "ordered" fashion of your choosing, which is hardly relevant because a saved XML file probably isn't either.

    * need to order chronologically? Store and inspect a creation date. Need to order alphabetically? Trivial. Need to order by byte size? No problem. There's no way that a reasonably built RDBMS can't order data, except randomly, which you can always accomplish with the presence of an identity attribute.

Those who can, do; those who can't, write. Those who can't write work for the Bell Labs Record.

Working...