Generating output to initialize a variable
7.2.1.3 Using format-number to control output
The final nicety in our stylesheet is that we use the XSLT format-number function to display the total for the current purchase order. Weve already discussed how we set the value of the variable orderTotal to be the output of the template named sumItems ; once the variable is set, we use format-number to display it with a currency sign, commas, and two decimal places: xsl:value-of select=format-numberorder-total, ,.007.3 Invoking the document Function
In our previous stylesheet, we used the document function to select some number of nodes from the original source document our list of purchase orders, then open those files. There are a number of ways to invoke the document function; well discuss them briefly here. The most common way to use the document function is as we just did. We use an XPath expression to describe a node-set; the document function takes each node in the node-set, converts it to a string, then uses that string as a URI. So, when we passed a node-set containing the filename attributes in the list of purchase orders, each one is used as a URI. If those URIs are relative references i.e., they dont begin with a protocol like http , the base URI of the stylesheet is used as the base URI for the reference. If the document function has two arguments, the second must be a node-set. The first argument is processed as just described, with the difference that the base URI of the first node in the node-set is used as the base URI for any relative URIs. That combination isnt used often, but its there if you need it. You can also pass a string or any other XPath datatype to the document function. If we wanted to open a particular resource, we could simply pass the name of the resource: documenthttp:www.ibm.compricelist.xml This action would open this particular resource and process it. Be aware that XSLT processors are required to return an empty node-set if a resource cant be found, but they arent required to signal an error. XSLT processors also dont have to support any particular protocols http , ftp , etc.; you have to check the documentation of your XSLT processor to see what protocols are and arent supported. Every node in the XPath source tree is associated with a base URI. When using the document function, the base URI is important for resolving references to various resources typically specified with relative links in a file opened with the document function. If a given node is an element or processing instruction node, and that node occurs in an external entity, then the base URI for that node is the base URI of the external entity. If an element or processing instruction node does not occur in an external entity, then its base URI is the base URI of the document in which it appears. The base URI of a document node is the base URI of the document itself, and the base URI of an attribute, comment, namespace, or text node is the base URI of that nodes parent. page 126 A special case occurs when you pass an empty string to the document function. As weve discussed the various combinations of arguments that can be passed to the function, weve gone over the rules for resolving URIs. When we call document , the XSLT processor parses the current stylesheet and returns a single node, the root node of the stylesheet itself. This technique is very useful for processing lookup tables in a stylesheet, something well discuss later in this chapter.7.4 More Sophisticated Techniques
Up to now, weve written a simple XML document that contains references to other XML documents, then we created a stylesheet that combines all those referenced XML documents into a single output document. Thats all well and good, but well probably want to do more advanced things. For example, it might be useful to generate a document that lists all items ordered by all the customers. It might be useful to sort all the purchase orders by the state to which they were shipped, by the last name of the customer, or to group them by the state to which they were shipped. Well go through some of these scenarios to illustrate the design challenges we face when generating documents from multiple input files.7.4.1 The document Function and Sorting
Our first challenge will be to generate a listing of all purchase orders and sort them by state. This isnt terribly difficult; well simply use the xsl:sort element in conjunction with the document function. Heres the heart of our new stylesheet: body h3Selected Purchase Orders - iSorted by stateih3 xsl:for-each select=documentreportpofilenamepurchase-ordercustomeraddressstate xsl:sort select=. xsl:apply-templates select=ancestor::purchase-order xsl:for-each body Figure 7-2. Another document generated from multiple input filesParts
» 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