Namespaces XML Document Rules
1.2.3 DOM and SAX
The two most popular APIs used to parse XML documents are the Document Object Model DOM and the Simple API for XML SAX. DOM is an official recommendation of the W3C available at http:www.w3.orgTRREC-DOM-Level-1 , while SAX is a de facto standard created by David Megginson and others on the XML-DEV mailing list http:lists.xml.orgarchives . Well discuss these two APIs briefly here. We wont use them much in this book, but discussing them will give you some insight into how most XSLT processors work. See http:www.megginson.comSAX for the SAX standard. Make sure the letters SAX are in uppercase. If youd like to learn more about the XML-DEV mailing list, send email with subscribe xml-dev in the body of the message to majordomoxml.org . You can also check out http:www.lists.ic.ac.uk hypermailxml-dev to see the XML-DEV mailing list archives.1.2.3.1 DOM
DOM is designed to build a tree view of your document. Remember that all XML documents must be contained in a single element; that single element becomes the root of the tree. The DOM specification defines several language-neutral interfaces, described here: Node This interface is the base datatype of the DOM. Element , document , text , comment , and attr all extend the Node interface. Document This object contains the DOM representation of the XML document. Given a Document object, you can get the root of the tree the Document element; from the root, you can move through the tree to find all elements, attributes, text, comments, processing instructions, etc., in the XML document. Element This interface represents an element in an XML document. Attr This interface represents an attribute of an element in an XML document. Text This interface represents a piece of text from the XML document. Any text in your XML document becomes a Text node. This means that the text of a DOM object is a child of the object, not a property of it. The text of an Element is represented as a Text child of an Element object; the text of an Attr is also represented that way. Comment This interface represents a comment in the XML document. A comment begins with -- and ends with -- . The only restriction on its contents is that two consecutive hyphens -- can appear only at the start or end of the comment. Other than that, a comment can include angle brackets , ampersands , single or double quotation marks , and anything else. page 17 ProcessingInstruction This interface represents a processing instruction in the XML document. Processing instructions look like this: ?xml-stylesheet href=case-study.xsl type=textxsl? ?cocoon-process type=xslt? Processing instructions contain processor-specific information. The first of the two PIs PI is XML jargon—feel free to drop this into casual conversations to impress your friends is the standard way to associate an XSLT stylesheet with an XML document more on this in a minute. The second PI is used by Cocoon, an XML publishing framework from the Apache Software Foundation. If youre not familiar with Cocoon, look at the Cocoon home page at http:xml.apache.orgcocoon . When you parse an XML document with a DOM parser, it: • Creates objects Element s, Attr , Text , Comment s representing the contents of the document. These objects implement the interfaces defined in the DOM specification. • Arranges these objects in a tree. Each Element in the XML document has some properties such as the elements name, and may also have some children. • Parses the entire document before control returns to your code. This means that for large documents, there is a significant delay while the document is parsed. The most significant thing about the DOM is that it is based on a tree view of your document. An XSLT processor uses a very similar tree view with some slight differences, such as the fact that not everything we deal with in XPath and XSLT has the same root element. Understanding how a DOM parser works makes it easier to understand how an XSLT processor views your document.1.2.3.1.1 A sample DOM tree
DOM, XSLT, and XPath all use tree structures to represent data from an XML document. For this reason, its important to have at least a casual knowledge of how DOM builds a tree structure. Our earlier postalcodes document is shown as a DOM tree in Figure 1-2 . Figure 1-2. DOM tree representation of an XML documentParts
» O'Reilly-XSLT-Mastering.XML.Transformati... 2264KB Mar 29 2010 05:03:43 AM
» An XML document must be contained in a single element
» XML declarations Document Type Definitions DTDs and XML Schemas
» Well-formed versus valid documents
» Tags versus elements XML Document Rules
» Namespaces XML Document Rules
» The Extensible Stylesheet Language XSL
» Document Object Model DOM Level 1
» Document Object Model DOM Level 2
» Namespaces in XML XML Standards
» Associating stylesheets with XML documents
» Installing Xalan Getting Started
» Our Sample Document A Sample Stylesheet
» Transforming the XML Document
» Stylesheet Results Transforming Hello World
» Parsing the Stylesheet How a Stylesheet Is Processed
» Parsing the Transformee How a Stylesheet Is Processed
» Lather, Rinse, Repeat How a Stylesheet Is Processed
» The xsl:template for greeting Elements
» Built-in template rule for element and root nodes
» Built-in template rule for modes
» Built-in template rule for text and attribute nodes
» Top-Level Elements Stylesheet Structure
» Other Approaches Stylesheet Structure
» The Hello World Java Program
» Goals of This Chapter Summary
» The Root Node The XPath Data Model
» Element Nodes The XPath Data Model
» Attribute Nodes The XPath Data Model
» Text Nodes The XPath Data Model
» Comment Nodes The XPath Data Model
» Processing Instruction Nodes The XPath Data Model
» Namespace Nodes The XPath Data Model
» Simple Location Paths Location Paths
» Relative and Absolute Expressions
» Selecting attributes Selecting Things Besides Elements with Location Paths
» Selecting the text of an element
» Selecting comments, processing instructions, and namespace nodes
» Using Wildcards Location Paths
» Numbers in predicates Predicates
» Functions in predicates Predicates
» Attribute Value Templates XPath Datatypes
» Output View The XPath View of an XML Document
» The Stylesheet The XPath View of an XML Document
» Summary XPath: A Syntax for Describing Needles and Haystacks
» Converting to boolean values
» Boolean examples The xsl:if Element
» xsl:for-each example The xsl:for-each Element
» How It Works Invoking Templates by Name
» Templates à la Mode Invoking Templates by Name
» Defining a Parameter in a Template
» Microsofts XSLT tools Global Parameters
» Setting global parameters in a Java program
» Are These Things Really Variables?
» Procedural design Implementing a String Replace Function
» Recursive design Implementing a String Replace Function
» Template Design Implementation A Stylesheet That Emulates a for Loop
» The Complete Example A Stylesheet That Emulates a for Loop
» XML Input A Stylesheet That Generates a Stylesheet That Emulates a for Loop
» Template Design A Stylesheet That Generates a Stylesheet That Emulates a for Loop
» Complications A Stylesheet That Generates a Stylesheet That Emulates a for Loop
» Summary Branching and Control Elements
» The ID, IDREF, and IDREFs Datatypes
» An XML Document in Need of Links
» A Stylesheet That Uses the id Function
» Limitations of IDs Generating Links with the id Function
» Defining a key Generating Links with the key Function
» A Slightly More Complicated XML Document in Need of Links
» The key function and the IDREFS datatype
» Solution 1: Replace the IDREFS datatype
» Solution 2: Use the XPath contains function
» Solution 3: Use recursion to process the IDREFS datatype
» Solution 4: Use an extension function
» Advantages of the key Function
» An Unstructured XML Document in Need of Links
» The generate-id Function Generating Links in Unstructured Documents
» Summary Creating Links and Cross-References
» Our First Example Sorting Data with xsl:sort
» Whats the deal with that syntax?
» Attributes The Details on the xsl:sort Element
» Another Example Sorting Data with xsl:sort
» Our First Attempt Grouping Nodes
» A Brute-Force Approach Grouping with xsl:variable
» Summary Sorting and Grouping Elements
» Recursive design An Aside: Doing Math with Recursion
» Generating output to initialize a variable
» Overview Invoking the document Function
» The document Function and Sorting
» Implementing Lookup Tables More Sophisticated Techniques
» Grouping Across Multiple Documents
» Summary Combining XML Documents
» Example: Generating multiple output files
» Example: Using extension functions from multiple processors
» Example: A library of trigonometric functions
» Example: Writing extensions in other languages
» Fallback Processing Extension Elements, Extension Functions, and Fallback Processing
» Extending the Saxon Processor
» Generating JPEG Files from XML Content
» About the Toot-O-Matic Case Study: The Toot-O-Matic
» Make It Easier to Create Tutorials
» Individual Panels Tutorial Layout
» Email Panel Zip File PDF Files
» Individual Panels XML Document Design
» Stylesheets and Modes XSLT Source Code
» Initializing Global Variables XSLT Source Code
» Generating the Main Menu Panel
» Generating the Section Indexes
» Generating the Individual Panels
Show more