Using HTML Forms to Post Templates

XML and Internet Support

XML and Internet Support

Using HTML Forms to Post Templates

HTML forms can be used to post templates. The input mechanism of HTML forms can be used to obtain user input for the values of the parameters that can be passed to an SQL statement. In the TEXTAREA element of the HTML form, template is used as the variable name for the NAME attribute. The body of the TEXTAREA is then sent as a value for template.

Examples

In the following examples, nwind is a virtual directory created using the IIS Virtual Directory Management for SQL Server utility (any name can be given to a virtual name when it is created). For more information, see Using IIS Virtual Directory Management for SQL Server Utility.

A. Post a simple template in a form

The HTML form in this example prompts the user to enter an employee ID. The ID value is used as an input parameter to the SELECT statement in the template. The query returns the first and last name of employees from the Employees table in the Northwind database. This form can be saved in an .htm file and opened in the browser.

<head>
<TITLE>Sample Form </TITLE>
</head>
<body>
For a given employee ID, employee first and last name is retrieved.
<form action="http://IISServer/nwind" method="POST">
<B>Employee ID Number</B>
<input type=text name=EmployeeID value='1'>
<input type=hidden name=contenttype value=text/xml>
<input type=hidden name=template value='
<ROOT xmlns:sql="urn:schemas-microsoft-com:xml-sql" >
<sql:header>
    <sql:param name="EmployeeID">1</sql:param>
</sql:header>
<sql:query>
  SELECT FirstName, LastName 
  FROM    Employees 
  WHERE    EmployeeID=@EmployeeID 
  FOR XML AUTO
</sql:query>
</ROOT>
'>
<p><input type="submit">
</form>
</body>
B. Post a template in an HTML form and provide an XSL file to process the output

In this example, a simple HTML form is used to post a template. The template contains a SELECT statement that returns first and last names from the Employees table in the Northwind database.

When this HTML document is opened in the browser, the user can specify the content-type and the Extensible Stylesheet Language (XSL) file to process the result set. If the content-type is specified as text/html, the XSL file processes the result set and produces a two-column table as output.

If the content-type is specified as text/xml, the result is displayed in form of an XML document.

Note  The XSL file must reside in the physical directory (or one of its subdirectories) associated with the virtual directory. If the file is stored in the physical directory, only the file name has to be specified. If the file is stored in one of the subdirectories of the physical directory, the directory path relative to the physical directory is specified.

<body>

Hi there

<form action="http://IISServer/nwind" method="POST">

contenttype

<input name=contenttype value="text/html">

<br>

xsl

<input name=xsl value="emp.xsl"><br>

<input type=hidden name=template value='

<ROOT>

<sql:query xmlns:sql="urn:schemas-microsoft-com:xml-sql">

Select FirstName, LastName from Employees for xml auto</sql:query>

</ROOT>

'>

<p><input type="submit">

</form>

</body>

The XSL file is given below. The XSL transformation is applied to the result set.

<?xml version="1.0"  encoding="ISO-8859-1" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match = "*">
   <xsl:apply-templates />
</xsl:template>

<xsl:template match = "Employees">
   <TR>
   <TD><xsl:value-of select = "@FirstName" /></TD>
   <TD><B><xsl:value-of select = "@LastName" /></B></TD>
   </TR>
</xsl:template>

<xsl:template match = "/">
    <HTML>
    <HEAD>
    <STYLE>th { background-color: #CCCCCC }</STYLE>
    </HEAD>
    <BODY>
   <TABLE border="1" style="width:300;">
   <TR><TH colspan="2">Employees</TH></TR>
   <TR><TH >FirstName</TH><TH>LastName</TH></TR>
   <xsl:apply-templates select = "ROOT" />
   </TABLE>
    </BODY>
    </HTML>
</xsl:template>
</xsl:stylesheet>

See Also

Using IIS Virtual Directory Management for SQL Server Utility

Accessing SQL Server Using HTTP

Retrieving XML Documents Using FOR XML

Using XPath Queries