XSL Device Formatting Using the Microsoft XSL ISAPI Filter - Our sample application
(Page 3 of 4 )
The following code example represents a small application that presents a list of numbers on a single page. Because both WML devices (WAP phones) and HTML devices (Web browsers) will use this application, we will need two stylesheets. In addition, we will also need the .pasp file that will generate the XML as well as a configuration file that will specify which stylesheet should be applied to which device.
The ASP source for generating the XML page (test.pasp) looks like this:
<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" server-config="test-Config.xml" href="test-IE5.xsl" ?>
<numbers>
<%
i = 5
while (i > 0)
response.write "<number>" & i & "</number>"
i = i - 1
wend
%>
</numbers>The code shown represents the .pasp file. This will generate an XML document. In fact, it's simply an ASP file that will generate XML instead of the "regular" HTML.
The XML generated by test.asp looks like this (Note that you will never actually see this XML):
<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" server-config="test-Config.xml" href="test-IE5.xsl" ?>
<numbers>
<number>5</number>
<number>4</number>
<number>3</number>
<number>2</number>
<number>1</number>
</numbers>For each XML document, it's necessary to specify a configuration file that will determine the appropriate XSL stylesheet to use for any given device. The configuration file for our sample application is called test-Config.xml and is shown below:
<?xml version="1.0" ?>
<server-styles-config>
<!-- for WML 1.1 browsers -->
<device target-markup="WML1.1">
<stylesheet href="test-WML11.xsl"/>
</device>
<!-- for Others browsers -->
<device>
<stylesheet href="test-IE5.xsl"/>
</device>
</server-styles-config>The target-markup attribute value of the device element in the code above must exist in the browscap.ini file. The XSL ISAPI filter uses the associated information as the Content-type for the requests output.
IIS will apply the associated stylesheet to the first matching device. In the above case, if the device accepts WML1.1, IIS will apply test-WML11.xsl -- otherwise it will apply test-IE5.xsl. For browsers like IE 5.5 that can process XSL on their own, developers can choose to leave the XML untouched. For such client-side XSL processing, the XML will be sent to the browser, which will use the specified stylesheet to format the document.
Here's the HTML XSL stylesheet, which is called test-IE5.xsl:
<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<HTML>
<BODY>
<xsl:for-each select="numbers/number">
<xsl:value-of select="text()"/>
<br/>
</xsl:for-each>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>Here, the XSL file is associated with the default browser. It will output the content of the NUMBERS/NUMBER elements as HTML:

The HTML that's sent to the browser looks like this:
<HTML>
<BODY>
5
<br />
4
<br />
3
<br />
2
<br />
1
<br />
</BODY>
</HTML>The WML XSL stylesheet (test-WML11.xsl) looks like this:
<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<xsl:pi name="xml">version="1.0"</xsl:pi>
<xsl:doctype>wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml"</xsl:doctype>
<wml>
<card>
<p>
<xsl:for-each select="numbers/number">
<xsl:value-of select="text()"/>
<br/>
</xsl:for-each>
</p>
</card>
</wml>
</xsl:template>
</xsl:stylesheet>This stylesheet will be applied to a WML enabled device, as shown below:

The WML generated by the XSL stylesheet looks like this:
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card>
<p>
5
<br />
4
<br />
3
<br />
2
<br />
1
<br />
</p>
</card>
</wml>Next: Conclusion >>
More ASP Articles
More By Jean-Baptiste Minchelli