Apress Pro PHP XML and Web Services (2006).pdf

(2987 KB) Pobierz
Pro PHP XML and
Web Services
Robert Richards
993042618.017.png 993042618.018.png 993042618.019.png 993042618.020.png 993042618.001.png 993042618.002.png 993042618.003.png 993042618.004.png 993042618.005.png 993042618.006.png 993042618.007.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.
 
993042618.008.png 993042618.009.png
Contents
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix
About the Technical Reviewers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . x
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xii
Introduction to XML and Web Services . . . . . . . . . . . . . . . . . . . . . 1
CHAPTER 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
XML Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
CHAPTER 2
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
Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
CHAPTER 3
Introducing Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Introducing Document Type Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Using XML Schemas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Using RELAX NG . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
XPath, XPointer, XInclude, and the Future . . . . . . . . . . . . . . . . 123
CHAPTER 4
Introducing XPath . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Introducing XPointer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
iii
993042618.010.png 993042618.011.png 993042618.012.png
iv
CONTENTS
Introducing XInclude . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Examining the Future of XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
PHP and XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
CHAPTER 5
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
Document Object Model (DOM) . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
CHAPTER 6
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
SimpleXML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
CHAPTER 7
Introducing SimpleXML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
Using SimpleXML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239
Using Namespaces in SimpleXML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
Using XPath . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260
Seeing Some Examples in Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
Simple API for XML (SAX) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
CHAPTER 8
Introducing SAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Using the xml Extension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
Migrating from PHP 4 to PHP 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
 
993042618.013.png 993042618.014.png
v
CONTENTS
Seeing Some Examples in Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310
XMLReader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
CHAPTER 9
Introducing XMLReader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
Using XMLReader . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314
Exporting to DOM Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
Dealing with Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
Performing Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
Seeing Some Examples in Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340
Extensible Stylesheet Language Transformations
(XSLT) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
CHAPTER 10
Introducing XSL and XSLT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
Introducing the XSL Extension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387
Using the XSL Extension . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
Using Parameters in XSL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
Calling PHP Functions from XSL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
Seeing Some Examples in Action . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
Effective and Efficient Processing . . . . . . . . . . . . . . . . . . . . . . . . 409
CHAPTER 11
Looking at the Pros and Cons of Parsers . . . . . . . . . . . . . . . . . . . . . . . . . . 409
Optimizing Parsing and Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
Combining Technologies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 439
XML Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441
CHAPTER 12
Introducing XML Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 441
Introducing Basic Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442
Introducing Enterprise Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448
Introducing Canonical XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
Introducing Exclusive XML Canonicalization . . . . . . . . . . . . . . . . . . . . . . . . 456
Introducing XML Signatures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460
Introducing XML Encryption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489
 
993042618.015.png 993042618.016.png
Zgłoś jeśli naruszono regulamin