XQuery/员工搜索
外观
< XQuery
此示例展示了 JavaScript 和 XQuery 相结合,提供直接更新的网页。 AJAX 用于一种有时称为AHAH 的形式,其中服务器端 XQuery 脚本返回一个 XHTML 节点(在本例中,一个包含有关员工信息的表格),该节点使用 innerHTML 更新到 DOM 中。
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Emp query using AJAX</title>
<script language="javascript" src="ajaxemp.js"/>
<style>
th {{background-color:yellow}}
</style>
</head>
<body>
<h1>Emp query using AJAX</h1>
<form action="javascript:getEmp();">
<label for="EmpNo" title="e.g. 7369, 7499 and 7521."> Employee Number</label>
<input type="text" size="5" name="empNo" id="empNo" />
<input type="submit" value="Find"/>
</form>
<div id="emp"/>
</body>
</html>
function updateEmp() {
if (http.readyState == 4) {
var divlist = document.getElementById('emp');
divlist.innerHTML = http.responseText;
isWorking = false;
}
}
function getEmp() {
if (!isWorking && http) {
var empNo = document.getElementById("empNo").value;
http.open("GET", "getemp.xq?empNo=" + empNo, true);
http.onreadystatechange = updateEmp;
isWorking = true;
http.send(null);
}
}
function getHTTPObject() {
var xmlhttp;
/*@cc_on
@if (@_jscript_version >= 5)
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@else
xmlhttp = false;
@end @*/
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
try {
xmlhttp = new XMLHttpRequest();
xmlhttp.overrideMimeType("text/xml");
} catch (e) {
xmlhttp = false;
}
}
return xmlhttp;
}
var http = getHTTPObject(); // create the HTTP Object
var isWorking = false;
declare function local:element-to-table($element) {
<table>
{for $node in $element/*
return
<tr>
<th>{name($node)}</th>
<td>
{ $node/text() }
</td>
</tr>
}
</table>
};
let $empNo := request:get-parameter("empNo",())
let $emp := //Emp[EmpNo=$empNo]
return
if (exists($emp))
then local:element-to-table($emp)
else
<p>Employee Number {$empNo} not found.</p>