Generating output to initialize a variable

page 125

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, ,.00

7.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 files