HomePHP Page 2 - Using XPath, SOAP, and More with Web Services
Using XPath to Retrieve Node Information - PHP
In this fourth part of a five-part series on Web Services, you'll learn about SimpleXML objects, SOAP, and more. This article is excerpted from chapter 20 of the book Beginning PHP and Oracle: From Novice to Professional, written by W. Jason Gilmore and Bob Bryla (Apress; ISBN: 1590597702).
XPath is a W3C standard that offers an intuitive, path-based syntax for identifying XML nodes. For example, referring to thebooks.xmldocument, you could use thexpath()method to retrieve allauthornodes using the expression/library/book/author:
array simplexml_element->xpath(string path)
XPath also offers a set of functions for selectively retrieving nodes based on value.
Suppose you want to retrieve all authors found in thebooks.xmldocument:
-------------------------------------------- Jane Austen Alberto Moravia Ernest Hemingway --------------------------------------------
You can also use XPath functions to selectively retrieve a node and its children based on a particular value. For example, suppose you want to retrieve allbooktitles where the author is namedErnest Hemingway: