Figure 3 shows sample results. Notice that the XML document automatically contains the <Titles> root element.
The template in Listing 1 performs a simple query, but you can create templates that perform more complex queries. For example, you can create a template that searches for titles that match a specific string. By specifying the search string in the URL and not the template, you can use the same template for different queries. The search string you specify in the query string is passed to the template as a parameter.
To perform this more advanced query, you need to make several changes to the template so that it accepts passed-in parameters. Listing 2 shows the modified template, Query2.xml. In Query2.xml, you need to add the <sql:header> element, which acts as a container that holds all the parameter definitions you want to use. You then use the <sql:param> element to define each parameter. You can define any number of parameters within the <sql:header> element. In this case, I've defined only one parameter, Title, set to the default value of Sushi.
In Listing 2's <sql:query> element, I modified the SQL query to accept the Title parameter. I preceded the parameter with the at sign (@) because it refers to the named parameter defined in the <sql:param> element.
To use the Query2.xml template, you can type the URL with or without an appended Title parameter. If you type the URL with a Title parameter (e.g., http://localhost/xmlquery/templates/query2.xml?Title=Computer), SQL Server retrieves all the titles that contain the word you specified (in this case, all the titles that contain the word Computer). If you don't specify the Title parameter, SQL Server uses the default value that you defined in the <sql:param> element. So, in this case, if you type
http://localhost/xmlquery/templates/query2.xml
SQL Server retrieves all the titles that contain the word Sushi.
Our template isn't finished yet, however. As an experienced database developer, you likely know about the efficiency of storing SQL queries as stored procedures in the database server. Listing 3 shows the stored procedure for the SQL query in Listing 2. To execute this stored procedure from the XML template, you have to further modify the template. Listing 4 contains the new version of the template, Query3.xml.
Creating the Search Application
With the template completed, you can create the application that uses the template. Suppose you want to provide an application that searches the Pubs database. You can create an HTML file that produces a search form like the one that Figure 4 shows. Listing 5, page 58, contains a file, Search.html, that creates this search form. As callout A in Listing 5 shows, the <form> element contains the action attribute, which points to the Query3.xml template.
When you enter a string in the search form and click Search, the template returns the records in an XML document, similar to the one that Figure 3 shows. However, displaying records in this format isn't useful for viewers of Web or WAP browsers. Thus, you need to format the XML document into HTML code for Web browsers and into WML code for WAP browsers. With SQL Server 2000's XML support, you can simply attach an XSLT style sheet to the XML results to transform those results into the format you need.
To transform the XML document into content for Web browsers, you can use the XSLT style sheet HTML.xsl, which Web Listing 1 shows. (For download instructions, see "More on the Web.") If you're unfamiliar with XSLT, see the XSLT Developer's Guide and the XSLT Reference on the Microsoft Developer Network (MSDN). You can find links to both resources at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk30/htm/xmmscxmloverview.asp. You can also check out the paper "An Introduction to XSLT and XPath" (http://www.wirelessdevnet.com/channels/wap/training/xslt.html). Figure 5 shows sample results from HTML.xsl.
To transform the XML document into content for WAP browsers, you can use the XSLT style sheet WML.xsl, which Web Listing 2 shows. However, taking advantage of the wireless platform involves more than just writing WML code and hoping that WAP-enabled devices can display the pages. The various WAP browsers differ greatly in how they look and act. To achieve the same user experience regardless of the WAP browser, you need to test and tailor the content to suit the different WAP devices. One tool that lets you test WML code is Openwave System's Openwave Simulator (formerly named UP.Simulator). The simulator is part of the Openwave software development kit (SDK), which you can download for free at http://developer.openwave.com/download/index.html#sdk. Different configurations for the Openwave Simulator are available for the various WAP-enabled phones on the market. You can download the configurations you need at http://developer.openwave.com/download/simconfig.html.
After you download the necessary files, you can test the WML code. In this case, you enter the following into the URL text box:
http://localhost/xmlquery/templates/query3.xml?Title=Computer
&contenttype=text/vnd.wap.wml&xsl=wml.xsl
Web Listing 3 shows the WML code that the simulator receives, and Figure 6 shows the screen you'll see.
No matter whether you use an XSLT style sheet to transform the XML document into HTML or WML code, you must tell IIS about that style sheet by referencing it in either the template or the URL. To reference an XSLT style sheet in a template, you place the style sheet in the same directory as the template (e.g., C:\inetpub\sqlxml\templates). You then replace the first line of code in the Query3.xml template with code such as
<Titles xmlns:sql="urn:schemas-microsoft-com:xml-sql"
sql:xsl="HTML.xsl">
Prev. page
1
[2]
3
next page