Summary Sorting and Grouping Elements
Chapter 7. Combining XML Documents
One of XSLTs most powerful features is the document function. document lets you use part of an XML document identified with an XPath expression, of course as a URI. In other words, you can look in a document, use parts of that document as URLs or filenames, open and parse those files, then perform stylesheet functions on the combination of all those documents. In this chapter, well cover the document function in all its glory.7.1 Overview
The document function is very useful for defining views of multiple XML documents. In this chapter, well use XML-tagged purchase orders that look like this: purchase-order id=38292 customer id=4738 level=Platinum address type=business name titleMr.title first-nameChester Hasbrouckfirst-name last-nameFrisbylast-name name street1234 Main Streetstreet citySheboygancity stateWIstate zip48392zip address address type=ship-to customer items item part_no=28392-33-TT nameTurnip Twaddlername qty3qty price9.95price item item part_no=28813-70-PG namePrawn Goadername qty1qty price18.95price item items purchase-order If we had a few dozen documents like this, we might want to view the collection of purchase orders in a number of ways. We could view them sorted or even grouped by customer, by part number, by the amount of the total order, by the state to which they were shipped, etc. One way to do this would be to write code that worked directly with the Document Object Model. We could parse each document, retrieve its DOM tree, then use DOM functions to order and group the various DOM trees, display certain parts of the DOM trees, etc. Because this is an XSLT book, though, you probably wont be surprised to learn that XSLT provides a function to handle most of the heavy lifting for us.7.2 The document Function
Well start with a couple of simple examples that use the document function. Well assume that we have several purchase orders and that we want to combine them into a single report document. One thing we can do is create a master document that references all the purchase orders we want to include in the report. Heres what that master document might look like: report titlePurchase Orderstitle po filename=po38292.xml page 121 po filename=po38293.xml po filename=po38294.xml po filename=po38295.xml report Well fill in the details of our stylesheet as we go along, but heres what the shell of our stylesheet looks like: xsl:template match= xsl:for-each select=reportpo xsl:apply-templates select=documentfilename xsl:for-each xsl:template In this template, we use the filename attribute as the argument to the document function. The simplest thing we can do is open each purchase order, then write its details to the output stream. Heres a stylesheet that does this: ?xml version=1.0?-- xsl:stylesheet version=1.0 xmlns:xsl=http:www.w3.org1999XSLTransform xsl:output method=html indent=no xsl:strip-space elements= xsl:template match= html head titlexsl:value-of select=reporttitletitle head body xsl:for-each select=reportpo xsl:apply-templates select=documentfilenamepurchase-order xsl:for-each body html xsl:template xsl:template match=purchase-order h1 xsl:value-of select=customeraddress[type=business]nametitle xsl:text xsl:text xsl:value-of select=customeraddress[type=business]namefirst-name xsl:text xsl:text xsl:value-of select=customeraddress[type=business]namelast-name h1 p xsl:textOrdered on xsl:text xsl:value-of select=datemonth xsl:textxsl:text xsl:value-of select=dateday xsl:textxsl:text xsl:value-of select=dateyear p h2Items:h2 table width=100 border=1 cols=55 15 15 15 tr bgcolor=lightgreen thItemth thQuantityth thPrice Eachth thTotalth tr xsl:for-each select=itemsitem tr xsl:attribute name=bgcolor xsl:choose xsl:when test=position mod 2 xsl:textwhitexsl:text xsl:when xsl:otherwise xsl:textlightgreenxsl:text xsl:otherwiseParts
» 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