Sunday, December 9, 2007

XSLT Examples for FileMaker Pro XML

Transform FMPDSORESULT into FMPXMLRESULT

We'll use the FMPDSORESULT export along with an XSL stylesheet to transform into an FMPXMLRESULT document. These examples will work in small steps so that you understand how to build an XSL stylesheet.

Create a new text document and name it Transform1.xsl.

Add the prolog and the root element for all XSL stylesheets:




The stylesheet element has several attributes that we need to include. The version and XSL namespace for XSL have required values.




We'll add two more attributes. The first one is the namespace for the XML source document elements. The second attribute tells the XSL processor to not include this namespace with any elements we create in the resulting XML.




Add the top-level XSL output element and its attributes. For this example we want to show the result as text, so the method attribute has a value of "text". Later we'll show the result as XML. The output element shows us the version and encoding for the resulting document. The indent attribute probably should be "no" for most result documents. Any indentation in the stylesheet is added for readability in the code listings and should not be included when you create your stylesheets.





Display Something for the Fields

Here, we'll take the above stylesheet, name it transform2.xsl, and add another XSL element, , to display text for the fields in the export. Just to make it easier to see what is happening, we'll use the name of the field elements as the text to display. Within the xsl:for-each loop for the rows/records, we'll add another xsl:for-each loop. The select attribute tells us to get any child element ("*") of the current path ("."). The XPath expression "name()" is a function that returns the name of each of these child elements.





TRANSFORM2.TXT
Find our rows and show the
fields.



We found a row!








XML to HTML

All HTML documents produced can be viewed in a browser. The pages don't need to be served by a web browser. You may find that some CSS and JavaScript may not render correctly, depending on the browser version. Test all the examples in this section to see your results.

FMPDSORESULT to HTML

the output will be to method=HTML. The export and transformation will produce an HTML document. The data will be placed into an HTML table similar to the simple_table example. Instead of ROWs, we'll use the HTML element ; and instead of COLs, we'll use the HTML element . This example will use the FMPDSORESULT instead of the FMPXMLRESULT export.

Example 1: Create a Simple HTML Table from FMPDSORESULT

  1. First make a copy of the transform3.xsl file and rename it dso2html1.xsl.

  2. Change the output method to "html" and indent to "yes".

  3. We need to make this an HTML document, so add these tags just after :


  4. The HTML document needs to be closed, so add these tags just before :


  5. Just before the first , add the HTML element

    . For convenience, we'll show the table borders. Just after the final end tag, , add the table close tag,
    .

  6. Change the ROW element into the tr element. Don't forget the end tag! We won't use the MODID and RECORDID attributes at this time, so delete them from the stylesheet.

  7. Change the elements into the element. Change into the element.

  8. Save the changes to the stylesheet

  1. Export some fields from any of your databases or use Export.fp5

  2. Choose File, Export Records and name your export dso2html1.htm.

  3. Select the FMPDSORESULT grammar.

  4. Check the Use XSL style sheet option and click the File button.

  5. Use the stylesheet dso2html1.xsl and click the Open button.

  6. Click the OK button and specify the fields to use in your new HTML table.






Fixed-width Text Export

You can find an example of a text export for column widths to be of the same width. You can change the variable to be any width, but you don't have a way to change each column independently. This stylesheet is called fixed_width.xsl and is found with the other example stylesheets in FileMaker Pro 6. Our example will use to pass values and to pass the width of each column.



No comments: