This page describes how to use the Transform() function to convert XML data to HTML within Second Site.
The following information is intended for advanced users.
The Transform() function is useful when you have information that you want to maintain in XML, but publish in HTML. For example, consider an address list. You can keep the information in HTML, but it's not convenient: HTML does not include elements that are specific to the address data you might want to store. You may also want to publish a subset of the information, keeping telephone numbers or street addresses private, but HTML is not designed for that.
In XML, you can store all the information in an appropriate format:
<document> <person> <name>John Doe</name> <sort-by>Doe, John</sort-by> <street>101 Main Street</street> <city>Anytown</city> <state>MA</state> <zip>02000</zip> <phone>800-555-4321</phone> </person> <person> <name>Jim Doe</name> <sort-by>Doe, Jim</sort-by> <street>101 Groovy Street</street> <city>Cooltown</city> <state>CA</state> <zip>99999</zip> <phone>800-555-1234</phone> </person> </document>
Use NotePad or a similar text editor to save the text above in a file called "addresses.xml".
Then, using XSLT, you can convert it to HTML.
<table> <tr> <td>Jim Doe</td> <td>Cooltown, CA 99999</td> </tr> <tr> <td>John Doe</td> <td>Anytown, MA 02000</td> </tr> </table>
Which might look like this:
|Jim Doe||Cooltown, CA 99999|
|John Doe||Anytown, MA 02000|
Note that the street address and telephone number are omitted, and the entries are sorted by the contents of the "SORT-BY" element. Such output would be easy to produce with XSLT.
Here's the XSLT to convert the XML above to the HTML above:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" indent="yes" encoding="iso-8859-1"/> <xsl:template match="/document"> <table class="grid"> <xsl:apply-templates select="person"> <xsl:sort select="sort-by"/> </xsl:apply-templates> </table> </xsl:template> <xsl:template match="person"> <tr> <td> <xsl:value-of select="name"/> </td> <td> <xsl:value-of select="city"/> <xsl:text>, </xsl:text> <xsl:value-of select="state"/> <xsl:text> </xsl:text> <xsl:value-of select="zip"/> </td> </tr> </xsl:template> </xsl:stylesheet>
Use NotePad or a similar text editor to save the text above in a file called "addresses.xsl".
To see the XML above transformed into HTML, follow the steps below.
- Make an Include subfolder in the Input (-i) folder for your site if you do not already have one:
- Save the XML and XSL shown above in text files in the "include" folder:
- Add a Custom Page User Item to your site and set the Main Content to the following script statement:
- Make the site.
The site should now include a custom page that shows an address list similar to the one shown above.
This page last changed on 16 Jan 2011.