Transforming XML
By itself, the capability to turn FileMaker data into XML is not terribly useful. The reason is that FileMaker emits the XML in one of its specialized grammars. Even though other applications can read the file containing the exported data, they might not be able to make much sense of the FMPDSORESULT and FMPXMLRESULT grammars. In fact, this is a general issue with XML-aware applications: They all work with different formats and structures of XML.
Lets say there exists a tool (call it WidgetPro) that can read information about widgets from an XML file, as long as the XML file looks like whats shown in Listing 22.4.
Listing 22.4. The WidgetPro XML Format
|
We can get widget data from FileMaker, and we can get it as XML, but the two XML documents have different structuresthe same data, but expressed with different tag names and tag structures.
This is an important point to understand about XML: XML is not in itself a language or a file format. Using XML, the same data can be described ("marked up," as its often said) in many different ways. For applications to share data via XML, its not enough for each application to support reading and writing data in its own, specific XML format. There has to be some means to translate between different forms of XML as well. In the widgets example, this means that we need to take the "FileMaker widget XML" and make it look like "WidgetPro widget XML." This leads to the concept of XML transformations.
Figure 22.4 illustrates the idea of an XML transformation. From the FileMaker Pro database of widget information, we first need to export the widget data in an FMPXML structure. Next, we need to transform that XML so that it looks like WidgetPros XML structure instead. Finally, we bring the transformed XML into WidgetPro. Figure 22.4 sketches what this process would look like.
Figure 22.4. An XML transformation pipeline. FMPXMLRESULT (emitted by FileMaker) is transformed into WidgetPro XML (accepted by WidgetPro).
Introducing XSL Stylesheets
It turns out that XML already has a transformation technology available for us. That technology is called XSL, which stands for eXtensible Stylesheet Language. The stylesheet turns out to be the transformer. In much the same way that a word processing or page layout stylesheet can take ordinary text and transform it into formatted text, an XSL stylesheet can take one form of XML and transform it into another (or into any other text-based format, actually), as shown in Figure 22.5.
Figure 22.5. An XML transformation pipeline that uses an XSL stylesheet to accomplish the transformation.
Note
You might often see the terms XSL and XSLT (which stands for XSL Transformations) used interchangeably. Technically they e distinct; XSLT is in fact a subset of XSL. But when people speak of XSL they e generally referring to XSL transformations, so we won make a major point of distinguishing between the two terms.
So what is an XSL stylesheet? Its a series of commands that describe how to transform XML input into some new form. The new form can also be XML (and often is), but its possible to use a stylesheet to transform your XML into other text-based formats as well: tab-separated text, HTML, or more complex formats such as PDF and RTF. Interestingly, the XSL transformation language is itself a variety of XML, so XSL stylesheets are also valid XML documents in their own right.
Heres an example of an XSL stylesheet that would transform the "FileMaker widget XML" into "WidgetPro XML" (see Listing 22.5).
Listing 22.5. An XSL Stylesheet
[View full width]
|
Our goal, remember, is to take the original XML output from FileMaker (Listing 22.3) and translate that output into a new form of XML that contains much the same information, but in a different structure (Listing 22.4).
The stylesheet in Listing 22.5 contains two kinds of statements: On the one hand there are XSL commands (which you can tell by their xsl: prefix), and on the other hand are the actual XML tags that the stylesheet will output. The stylesheets job is to pick through the original XML document and decide which pieces of it to output, and in what order.
Analyzing a Stylesheet
If youve never read through an XSL stylesheet before, this section may be useful. Well go through the stylesheet in Listing 22.5 line by line to illustrate its inner workings.
The XML Declaration
Every XML document begins with an XML declarationand XSL stylesheets are XML documents. Simple enough.
The Stylesheet Statement
[View full width]
The xsl:stylesheet statement announces the document as an XSL stylesheet. The stylesheet statement also declares two XML namespaces (thats what the xmlns stands for). Namespaces are an important XML concept, but like most of the finer points of XML, namespaces are a bit too complex a topic for us to spend much time on in this book. Suffice it to say that both the namespaces declared here are necessary. The second namespace, which is abbreviated xsl, is common to all XSL stylesheets, and is used to distinguish all the XSL stylesheet commands from other forms of XML. (These commands, again, begin with the same xsl: prefix thats specified by the namespace.) And the fmp namespace declaration is crucial as well because it matches the namespace declaration that appears at the start of any XML document output by FileMaker. Well have a bit more to say about the FileMaker namespace farther on.
Note
Notice also that the stylesheet declaration includes a statement called exclude-result-namespaces. This rather important command prevents namespaces declared in the source document from being carried through to the output document. In general, we recommend you use this command in the
FileMakers Custom Web Publishing is covered in depth in Chapter 23, "Custom Web Publishing," p. 699. |
Specifying the Output Type
The xsl:output statement tells the XSL processor what type of document is being output. If you e trying to produce XML output, you need to include a statement like this one so that the XSL processor adds the appropriate XML declaration to the final document. The output statement also includes an attribute called indent when this is set to yes, the XSL processor tries to format the XML output in a pleasing and readable way.
Using a Template to Find the Result Set
The concept of a template is crucial to XSL. Templates are a way for the stylesheet writer to specify which parts of the source document shes interested in. In this case, we
e telling the processor we want to find the element called
Just inside the xsl:template statement is some actual XML, in the form of a
Of course, inside the
Using xsl:for-each to Loop Over a Result Set
The
For each
Using xsl:choose to Determine Output
At this point, we
e inside the
This is a little trickier because in the FileMaker XML, a
Once again we use
If we were trying to program this type of multiple choice in a FileMaker calculation, wed use a Case() statement, or perhaps a Choose(). Here we use the XSL equivalent, which is called
The rest of the
Applying an Export Transformation to FileMaker XML
FileMaker lets you use XSL stylesheets to transform your data when moving data into or out of FileMaker with XML. Lets consider the export example first. If you have a table of FileMaker data (such as the widget data weve been using), and you choose to export the data as XML, youll see the dialog box shown in Figure 22.6.
Figure 22.6. When exporting XML from FileMaker, you may also choose to apply a stylesheet to transform the outbound XML.
Here you can choose your XML export grammar, as weve already seen, but you can also choose whether to apply an XSL stylesheet to transform the XML as its being output. If you want to apply a stylesheet, you can pick a local file (in other words, a file resident on your local hard drive or on a mounted server volume). You can also pick a stylesheet file thats available over HTTP (namely, on a web server somewhere).
Using XSL stylesheets in the export process in this way, you can transform FileMaker data into a wide variety of output formats: other variants of XML, or HTML, or XML suitable for import into applications such as Excel or Quark Xpress, or even a complex text format such as PDF.
Категории