XML in Theory and Practice This page intentionally left blank XML in Theory and Practice Chris Bates Sheffield Hallam University WILEY Copyright ©2003 by John Wiley & Sons Ltd, The Atrium, Southern Gate, Chichester, West Sussex PO19 8SQ, England Telephone (+44) 1243 779777 Email (for orders and customer service enquiries): cs-books@wiley.co.uk Visit our Home Page on www.wileyeurope.com or www.wiley.com All Rights Reserved No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except under the terms of the Copyright, Designs and Patents Act 1988 or under the terms of a licence issued by the Copyright Licensing Agency Ltd, 90 Tottenham Court Road, London WIT 4LP, UK, without the permission in writing of the Publisher, with the exception of any material supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the publication Requests to the Publisher should be addressed to the Permissions Department, John Wiley & Sons Ltd, The Atrium, Southern Gate, Chichester, West Sussex PO19 8SQ, England, or emailed to permreq@wiley.co.uk, or faxed to (+44) 1243 770620 Neither the authors nor John Wiley & Sons, Ltd accept any responsibility or liability for loss or damage occasioned to any person or property through using the material, instructions, methods or ideas contained herein, or acting or freraining from acting as a result of such use The authors and publisher expressly disclaim all implied warranties, including merchantability or fitness for any particular purpose There will be no duty on the authors or publisher to correct any errors or defects in the software Designations used by companies to distinguish their products are often claimed as trademarks In all instances where John Wiley & Sons, Ltd is aware of a claim, the product names appear in capital or all capital letters Readers, however, should contact the appropriate companies for more complete information regarding trademarks and registration This publication is designed to provide accurate and authoritative information in regard to the subject matter covered It is sold on the understanding that the Publisher is not engaged in rendering professional services If professional advice or other expert assistance is required, the services of a competent professional should be sought Other Wiley Editorial Offices John Wiley & Sons Inc., 111 River Street, Hoboken, NJ 07030, USA Jossey-Bass, 989 Market Street, San Francisco, CA 94103-1741, USA Wiley-VCH Verlag GmbH, Boschstr 12, D-69469 Weinheim, Germany John Wiley & Sons Australia Ltd, 33 Park Road, Milton, Queensland 4064, Australia John Wiley & Sons (Asia) Pte Ltd, Clementi Loop #02-01, Jin Xing Distripark, Singapore 129809 John Wiley & Sons Canada Ltd, 22 Worcester Road, Etobicoke, Ontario, Canada M9W 1L1 Wiley also publishes its books in a variety of electronic formats Some content that appears in print may not be available in electronic books British Library Cataloguing in Publication Data A catalogue record for this book is available from the British Library ISBN 0-470-84344-6 Typeset from author-supplied PDF files Printed and bound in Great Britain by Biddies Ltd, Guildford and King's Lynn This book is printed on acid-free paper responsibly manufactured from sustainable forestry in which at least two trees are planted for each one used for paper production Contents Introduction Part I Extensible Markup Language Writing XML 2.1 A First Example 2.2 Why Not Use HTML? 2.3 The XML Rules 2.4 Parsing XML Files 2.5 The Recipe Book 2.6 The Business Letter 13 14 15 18 29 34 38 Document Type Definitions 3.1 Structure 3.2 Elements 3.3 Attributes 3.4 Entities 43 44 45 47 48 vi CONTENTS 3.5 3.6 3.7 3.8 Notations Using DTDs The Recipe Book Business Letter Specifying XML Structures Using Schema 4.1 Namespaces 4.2 Using Schemas 4.3 Defining Types 4.4 Data In Schema 4.5 Compositors 4.6 Example Schema 51 52 54 57 61 63 66 71 78 82 90 Part II Formatting XML for Display and Print Cascading Style Sheets 5.1 CSS and HTML 5.2 CSS and XML 5.3 Denning Your Own Styles 5.4 Properties and Values in Styles 5.5 A Stylesheet For The Business Letter 103 104 108 110 113 119 Cascading Style Sheets Two 6.1 The Design Of CSS2 6.2 Styling For Paged Media 6.3 Using Aural Presentation 6.4 Counters And Numbering 123 124 126 130 134 Navigating within and between XML Documents 7.1 XPath 7.2 XLink 7.3 XPointer 139 140 154 166 XSL Transformation Language 169 CONTENTS 8.1 8.2 8.3 8.4 8.5 8.6 8.7 8.8 8.9 Introducing XSLT Starting the Stylesheet Templates XSL Elements XSL Functions Using Variables Parameter Passing Modes Handling Whitespace vii 170 174 175 177 179 182 184 186 187 XSLT in Use 9.1 The Recipe Book 9.2 The Business Letter 197 198 208 10 XSL Formatting Objects 10.1 Document Structure 10.2 Processing XSL-FO 10.3 Formatting Object Elements 10.4 The Recipe Book 219 221 224 227 250 Part III Handling XML in Your Own Programs 11 Java and XML 11.1 Java Packages for Processing XML 263 267 12 The Document Object Model 12.1 The W3C Document Object Model 12.2 The Xerces DOM API 12.3 Using the DOM to Count Nodes 12.4 Using the DOM to Display a Document 275 276 279 283 286 13 The Simple API for XML 13.1 The SAX API 13.2 A Sax Example 289 291 299 viii CONTENTS Part IV Some Real-World Applications of XML 14 Introducing XHTML 14.1 XHTML Document Type Definitions 14.2 An XHTML Primer 14.3 The Rules Of XHTML 309 311 312 325 15 Web Services - The Future of the Web? 15.1 Some Typical Scenarios 15.2 Semantic Web 15.3 Resource Description Framework 15.4 Web Services 329 330 333 335 340 16 Distributed Applications with SOAP 16.1 An Overview of SOAP 16.2 Programming SOAP in Java 16.3 Accessing Recipes 351 352 362 372 17 DocBook 17.1 Introducing DocBook 17.2 Creating DocBook Documents 17.3 Styling DocBook Documents Using DSSSL 17.4 Styling DocBook Documents Using XSL 381 382 383 395 399 18 XUL 18.1 Introducing XUL 18.2 The XUL Widgets 18.3 Using XUL 403 404 407 417 References 421 Appendix A Business Letter in XML 425 Appendix B Recipe Book in XML 429 Appendix F Recipe Formatting Object Stylesheet 456 Appendix F: Recipe Formatting Object Stylesheet Page 457 458 Appendix F: Recipe Formatting Object Stylesheet < fo:block>Ingredient < fo:block>Amount Unit 459 Note :