Pro PHP XML and Web Services - Richards_ Robert.pdf

(8310 KB) Pobierz
133840339 UNPDF
THE EXPERT’S VOICE ® IN OPEN SOURCE
Covers PHP
versions 5 and the
forthcoming 6
PHP XML and
Web Services
Master working with XML and Web services using PHP
Robert Richards
Pro
133840339.028.png 133840339.029.png 133840339.001.png 133840339.002.png 133840339.003.png 133840339.004.png 133840339.005.png 133840339.006.png 133840339.007.png 133840339.008.png 133840339.009.png 133840339.010.png 133840339.011.png 133840339.012.png 133840339.013.png 133840339.014.png 133840339.015.png 133840339.016.png 133840339.017.png 133840339.018.png 133840339.019.png 133840339.020.png 133840339.021.png
Pro PHP XML and
Web Services
Robert Richards
133840339.022.png 133840339.023.png 133840339.024.png
Pro PHP XML and Web Services
Copyright © 2006 by Robert Richards
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,
electronic or mechanical, including photocopying, recording, or by any information storage or retrieval
system, without the prior written permission of the copyright owner and the publisher.
ISBN-13: 978-1-59059-633-3
ISBN-10: 1-59059-633-1
Library of Congress Cataloging-in-Publication data is available upon request.
Printed and bound in the United States of America 987654321
Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence
of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark
owner, with no intention of infringement of the trademark.
Lead Editor: Matt Wade
Technical Reviewers: Christian Stocker, Adam Trachtenberg
Editorial Board: Steve Anglin, Dan Appleman, Ewan Buckingham, Gary Cornell, Jason Gilmore,
Jonathan Hassell, James Huddleston, Chris Mills, Matthew Moodie, Dominic Shakeshaft,
Jim Sumser, Matt Wade
Project Manager: Kylie Johnston
Copy Edit Manager: Nicole LeClerc
Copy Editor: Kim Wimpsett
Assistant Production Director: Kari Brooks-Copony
Production Editor: Kelly Gunther
Compositor: Linda Weidemann, Wolf Creek Press
Proofreader: Nancy Sixsmith
Indexer: Jan Wright
Artist: Kinetic Publishing Services, LLC
Cover Designer: Kurt Krames
Manufacturing Director: Tom Debolski
Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor,
New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders-ny@springer-sbm.com , or
visit http://www.springeronline.com .
For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley,
CA 94710. Phone 510-549-5930, fax 510-549-5939, e-mail info@apress.com , or visit http://www.apress.com .
The information in this book is distributed on an “as is” basis, without warranty. Although every precaution
has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to
any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly
by the information contained in this work.
The source code for this book is available to readers at http://www.apress.com in the Source Code section.
This book is dedicated to my wife and best friend, Julie.
Thank you for your patience, support, and encouragement
at the times I most needed it.
133840339.025.png
Contents
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
About the Technical Reviewers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . x
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xii
CHAPTER 1 Introduction to XML and Web Services . . . . . . . . . . . . . . . . . . . . . 1
Exploring the History of XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Using XML in the Real World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Introducing Service Oriented Architecture and Web Services . . . . . . . . . . . 9
Defining Common Terms and Acronyms . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
CHAPTER 2 XML Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Introducing Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Understanding Basic Layout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Understanding Basic Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Using Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Using IDs, IDREF/IDREFS, and xml:id . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Using xml:space and xml:lang . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Understanding XML Base . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
CHAPTER 3 Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Introducing Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Introducing Document Type Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Using XML Schemas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Using RELAX NG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
CHAPTER 4 XPath, XPointer, XInclude, and the Future . . . . . . . . . . . . . . . . 123
Introducing XPath . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Introducing XPointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
iii
133840339.026.png
iv
CONTENTS
Introducing XInclude . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Examining the Future of XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
CHAPTER 5 PHP and XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Introducing XML in PHP 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Configuring libxml Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
Introducing Encoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Figuring Out the libxml2 Version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Introducing Parser Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Introducing PHP Streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Performing Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179
CHAPTER 6 Document Object Model (DOM) . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Introducing the DOM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Using the DOM Extension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
Performing Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214
Using XPath . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
Extending Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
Common Questions, Misconceptions, and Problems . . . . . . . . . . . . . . . . 223
Migrating from domxml to the DOM Extension . . . . . . . . . . . . . . . . . . . . . 228
Seeing Some DOM Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
CHAPTER 7 SimpleXML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
Introducing SimpleXML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
Using SimpleXML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
Using Namespaces in SimpleXML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
Using XPath . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
Seeing Some Examples in Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
CHAPTER 8 Simple API for XML (SAX) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Introducing SAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Using the xml Extension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
Migrating from PHP 4 to PHP 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
133840339.027.png
Zgłoś jeśli naruszono regulamin