0% found this document useful (0 votes)
62 views21 pages

Professional PHP: Day 3 - XML & XSLT

This document provides an overview of XML, XSLT, and web services. It discusses what XML and XSLT are, their benefits and drawbacks, and how to use SimpleXML and XSLT in PHP. It also covers what web services are, how XML fits with web services, examples of companies that use web services, and the differences between SOAP and REST. Exercises are included to work with an RSS feed using SimpleXML, XSLT, and validating the XML structure.

Uploaded by

api-30979114
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views21 pages

Professional PHP: Day 3 - XML & XSLT

This document provides an overview of XML, XSLT, and web services. It discusses what XML and XSLT are, their benefits and drawbacks, and how to use SimpleXML and XSLT in PHP. It also covers what web services are, how XML fits with web services, examples of companies that use web services, and the differences between SOAP and REST. Exercises are included to work with an RSS feed using SimpleXML, XSLT, and validating the XML structure.

Uploaded by

api-30979114
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Professional PHP

Day 3 – XML & XSLT

Course Author: Paul Reinheimer


Course Author: D. Keith Casey, Jr.
[email protected]

1
Session Information
• This course has been designed to introduce
XML, XSLT, and related techniques available in
PHP 5
• Exercises will be presented to provide the
opportunity to try out new techniques and
experiment.

2
For exercises in this section:

 You'll need:
– A fully functional PHP5+ instance (5.2+ is preferred)
– Your favorite PHP editor
– A willingness to break things, make mistakes, and
ask questions

3
What is XML?

• A way to encode documents electronically – first


specs in 1996, became useful in 1999, became
common in 2000+
• The most common example is RSS
• PHP4 used a SAX-style (event driven) parser,
was painful to use, more painful to extend
• PHP5 implemented the DOM-based parser
known as SimpleXML

4
Key Aspects of XML

• Character Encoding & Escaping


• Well-formedness & Validation
– Numerous competing validation standards
– DTD, XML Schema, RELAX NG, Schematron,
etc
• Useful and compatible with just about every
language out there...

5
Benefits of XML

• Human readable (mostly)


• Flexible in implementation technology
• Vendor-independence
• Based on “open” standards
• Tends to increase collaboration

6
Drawbacks of XML

• Human readable (mostly)


• Vendor mis-implementations
• Different design paradigm
– Mostly relevant to Web Services in general

7
SimpleXML to the Rescue!

• It's like XML without the XML!


• Every XML Document is treated as an array
• Every Attribute is an “element” of the array
• Every Child Node is an item in that array
– Every Child Node is an item in that array
• Every Child Node is an item in that array
– Every Child Node is an item in that array
» Every Child Node is an item in that array
$xmldoc = simplexml_load_file('rss.xml');
print_r($xmldoc->children());
8
Exercise 1

• Get an RSS/Atom feed


– Suggested: http://feeds.feedburner.com/phpa
• Load it into memory using SimpleXML
• Dump the content to screen
– Use <pre> to make it legible
• Display a list of titles

9
What is XSLT?

• A templating language used for transforming


XML into something else... XHTML, XML, even
PDF (via XSL-FO)
• All the same rules as XML apply because XSL is
XML, just a more specific form
• It's sort of a functional programming language
with if/else, case, and other basic conditional
statements

10
Exercise 2

• Using the previous RSS/Atom feed:


– Suggested: http://feeds.feedburner.com/phpa
• Create an XSL file that displays the titles
– Resources:
– http://en.wikipedia.org/wiki/XSL_Transformation
s
– http://devzone.zend.com/article/688

11
Web Services

• Web Services allow disparate applications to


communicate with each other over the Internet.
– Broad Definition, includes Ajax and B2B
communications.
– Many different protocols and implementations exist.
– XML is a popular choice for communication: cross
platform, language independent.

12
So how does this relate to XML?

• Why does XML fit nicely for Web Services?


– XML
• Human readable (mostly)
• Flexible in implementation technology
– Vendor-independence
• Based on “open” standards
• Easier to add additional partners
– “Many parts, loosely joined”
• Promotes reuse/commoditization of components
• Increased collaboration
13
And the drawbacks?

• So XML solves all of our problems, right?


– XML
• Lots of data to push, pull, manipulate
• Vendor mis-implementations
– Different design paradigm
• Bypasses the “planned” user interfaces
• Different security model
• Dependency management
– SOAP / RPC
• Dedicated application servers
14 • Vendor/marketing-driven
So who uses Web Services?

• Everyone:
– Amazon (AWS), Ebay, FedEx, USPS, Google,
PayPal, Delicious, Flickr, United States National
Weather Service, Library of Congress, Twitter,
Basecamp, SugarCRM, Blogger, Intellipedia,
Getty Images, and lots of others...

15
So what's the point?

• Sites offer web services to provide information


they believe other people want or need
– Providing information over a web service may be
cheaper computationally, or in labour than
alternatives
– Communicating with clients over a web service
increases the barrier of conversion to a competitor
– Automated communication allows higher levels of
customer service
– Offering something your competitors do not
provides a competitive advantage
16
How?

• The majority of sites implement either a REST


based web service, or one using SOAP
• Some sites choose to create their own protocol
from scratch, these are problematic
• Some large providers implement both, allowing
the programmer to choose the method that
meets their needs, or matches their skills

17
SOAP

• The word formally known as an acronym for


the Simple Object Access Protocol
• SOAP is the successor to XML-RPC
• Microsoft, IBM, and the W3C created SOAP
with cross platform interoperability in mind
• It’s a very thick protocol, there’s lots involved

18
REST

• REpresentational State Transfer


– The term was defined in Roy Fielding’s doctoral
dissertation in 2000
• The term is now used to describe virtually any
non-SOAP web service, including many that
violate some of the concepts presented in the
paper
• Under REST, the state of a resource can be
represented and transmitted
19
Exercise 3

• Using the previous RSS/Atom feed:


– Suggested: http://feeds.feedburner.com/phpa

• Validate the XML structure


– Note: This is not the same as well-formed

20
Homework

• Choose a Web Service and explore it


– Flickr is a good option
• Start with the read-based functions but try to
use one of the write/update-based functions
• Good resources:
– http://benramsey.com/tag/rest/
– RESTful Web Architectures

21

You might also like