|
|||||||||||||
Applying a custom stylesheetYou can provide XSLT stylesheets for any resource. If you do not provide one, a default stylesheet is used. Using custom XSLT, you can transform XML representations into another XML document, HTML, WML, CSV (comma-separated values), and other text formats. You may need some knowledge of XSLT to understand this section. However, the XSLT shown is quite basic. Matching stylesheets to resourcesDefault match
By default, the server matches a stylesheet to a resource based on the resource type. For example, the
default HTML rendering of a
All XML generated by the WaterkenTM server specifies the resource type, as well as any compatible resource types. When asked to generate HTML output, the server searches for a stylesheet matching the most specific type. If one is not found, it tries to match the more general types. If no match is found, the server uses a general stylesheet that can handle any type of resource. For most resources, this stylesheet prints out each branch in a table. For invokable resources, a default HTML FORM is created. This default rendering is useful during application development. Forced matchSometimes different views are needed for displaying different representations of the resource. You can force the server to use a stylesheet other than its default one. For the weekly sales resource, we would like to create a rendering that is specific to a map containing the weekly sales data, rather than rendering the weekly sales as a generic map. We can easily apply any stylesheet to a resource by using a "stylesheet" argument in the resource's capability URL. The value of the argument is the stylesheet identifier.
View the custom rendering of the weekly sales resource by appending this query string to the URI:
Viewing server-generated XMLThe XML representation of the weekly sales map looks like: |
<list>
<doc schema="http://waterken.com/adt/hashed/Map">
<element>
<key>Colombian_Decaf</key>
<value schema="http://waterken.com/Integer">3</value>
</element>
...
</doc>
</list>
|
You can view the actual XML in your browser. To do so, open the weekly sales in your browser. In your browser location bar, substitute ".xml" for the ".html" file extension and press "return". If you are using Mozilla, you may need to view the source code to see the XML. Matching templates by element type
The preferred style for WaterkenTM application XSLT is to match
templates to elements using the element's type, or
All stylesheets should have at least three templates. The first template strips off the
<xsl:template match="list">
<xsl:apply-templates select="doc" />
</xsl:template>
Having stripped off the
The template that matches on the unexpected type has two functions. It tests for an element named
<xsl:template match="*[@schema != 'http://waterken.com/adt/hashed/Map']">
<xsl:choose>
<!-- Compatible? -->
<xsl:when test="super">
<xsl:apply-templates select="super" />
</xsl:when>
<xsl:otherwise>
<!-- Constructs error page -->
<xsl:otherwise>
</xsl:choose>
</xsl:template>
The map in HTMLThe template that matches on the expected type creates an HTML table that prints out the coffee names and sales amounts. The XML is simple, so the XSLT is quite simple as well. This code excerpt contains the highlights of this template.
<xsl:template match="*[@schema = 'http://waterken.com/adt/hashed/Map']">
...
<xsl:for-each select="element">
<tr bgcolor="#FFFFFF">
<th align="left">
<!-- Coffee name. -->
<span class="arial">
<xsl:value-of select="normalize-space(key)" />
</span>
</th>
<td align="right">
<!-- Coffee sales. -->
<span class="arial">
<xsl:value-of select="normalize-space(value)" />
</span>
</td>
</tr>
</xsl:for-each>
...
</xsl:template>
We will look at some more stylesheet examples as we build other resources. The next section describes a query that uses composite datatypes. |
|
top
Copyright 2002 - 2003 Waterken Inc. All rights reserved. |