| home bookshelf slides |
| SXSW 2007 : MyTimes XSLT Widgets |
Page 1The widgets shown in the preceding examples used JSON data. The JSON data can then be read into an HTML template using the syntax $widget.data.etc. Simple control structures like {section} and {if} can be used to loop through the data or conditionally display data based on user preferences. However, many sources deliver data via XML. In this case, converting to the data to JSON and using the same template technology would be not beneficial, since a powerful language for manipulating XML already exists: XSLT. MyTimes widgets that receive data in XML use XSLT templates. Page 2Let's step through the creation of a GMail widget. Here is a screenshot of sample data delivered from the GMail service: Page 4Here's the way we want the data to display in our widget. We want:
Page 5We'll start building the XSLT stylesheet for this by creating a template element to match each of these. Here's the XSL:template element for the title. Page 7Next we'll work on creating a template element for an entry. To display the entry element, we want to show 3 items:
Page 8Here's the top level of the template element for atom:entry: Page 9The chunk for the author and time information is as shown in the screenshot. Note that we can use the same $widget.method syntax as with the HTML templates. In the MyTimes framework, the output of the XSLT processor is run through the HTML template processor. Page 11The XSLT has access to not just the XML from the source, but also all of the properties and preferences of the widget. By specifying a prompt for displayPrefs in the prop.js file, we can then use that property to conditionally display the summary. Page 12All that is necessary to finish our XSLT template is to create a root template that will cause the individual element templates to be picked up by the XSLT processor. Note the format for accessing the data returned by the widget source, e.g.: <xsl:apply-templates select="widget/data/atom:feed/atom:title"/> User preferences can be accessed via: widget/prefs Page 13The only other thing necessary to finish our GMail widget is to create a prop.js file. No code file is necessary. A portion of the prop.js file is shown. As with the weather widget, we specify a name, a source, and a few prompts (for example, for the user password). |