Download XML.com: What Is XSLT

Transcript
Figure 2-2: An XML-aware browser viewing the source of a document
The canvas content matches what the non-XML browser rendered in Figure 2-1. Note that View/Source displays the raw XML source and not the
transformed XHTML result of applying the stylesheet.
Note: I found it very awkward when first using browser-based stylesheets to diagnose problems in my stylesheets. Without access to the
intermediate results of transformation, it is often impossible to ascertain the nature of the faulty HTML generation. One of the free resources
found on the Crane Softwrights Ltd. web site is a script for standalone command-line invocation of the MSXML XSLT processor. This script
is useful for diagnosing problems by revealing the result of transformation. This script has also been used extensively by some to create static
HTML snapshots of their XML for delivery to non-XML-aware browsers.
This is a prose version of an excerpt from the book P
" ractical Transformation Using XSLT and XPath"(Eighth Edition ISBN 1-894049-05-5 at the
time of this writing) published by Crane Softwrights Ltd., written by G. Ken Holman; this excerpt was edited by Stan Swaren, and reviewed by
Dave Pawson.
2. Getting started with XSLT and XPath (cont'd)
2.1.2 Some more complex examples
The following more complex examples are meant merely as illustrations of some of the powerful facilities and techniques available in XSLT. These
samples expose concepts such as variables, functions, and process control constructs a stylesheet writer uses to effect the desired result, but does not
attempt any tutelage in their use.
Note: This subsection can be skipped entirely, or, for quick exposure to some of the facilities available in XSLT and XPath, only briefly reviewed.
In the associated narratives, I've avoided the precise terminology that hasn't yet been introduced and I overview the stylesheet contents and
processor behaviors in only broad terms. Subsequent subsections of this chapter review some of the basic terminology and design
approaches.
I hope not to frighten the reader with the complexity of these examples, but it is important to realize that there are more complex operations
than can be illustrated using our earlier three-line source file example. The complexity of your transformations will dictate the complexity of
the stylesheet facilities being engaged. Simple transformations can be performed quite simply using XSLT, but not all of us have to meet only
simple requirements.
The following XML source information in prod.xml is used to produce two very dissimilar renderings:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0"?><!--prod.xml-->
<!DOCTYPE sales [
<!ELEMENT sales ( products, record )> <!--sales information-->
<!ELEMENT products ( product+ )>
<!--product record-->
<!ELEMENT product ( #PCDATA )>
<!--product information-->
<!ATTLIST product id ID #REQUIRED>
<!ELEMENT record ( cust+ )>
<!--sales record-->
<!ELEMENT cust ( prodsale+ )>
<!--customer sales record-->
<!ATTLIST cust num CDATA #REQUIRED>
<!--customer number-->
<!ELEMENT prodsale ( #PCDATA )>
<!--product sale record-->
<!ATTLIST prodsale idref IDREF #REQUIRED>
]>
<sales>
<products><product id="p1">Packing Boxes</product>
<product id="p2">Packing Tape</product></products>
<record><cust num="C1001">
<prodsale idref="p1">100</prodsale>
<prodsale idref="p2">200</prodsale></cust>
<cust num="C1002">
<prodsale idref="p2">50</prodsale></cust>
<cust num="C1003">
<prodsale idref="p1">75</prodsale>