这里是我的代码,其目的是创建一个XQuery请求,以便在xml文档(personne.xml,下面给出)中找到一个单词,但我遇到问题:变量$var
包含整个xml文件,即使我选择了节点$books-doc/Dictionnaire/mot
。XQuery请求打开整个文件
declare namespace page = 'http://basex.org/modules/web-page/traitement.xq';
declare
%rest:path("")
%output:method("xhtml")
%output:omit-xml-declaration("no")
%output:doctype-public("-//W3C//DTD XHTML 1.0 Transitional//EN")
%output:doctype-system("http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd")
function page:start() as element(Q{http://www.w3.org/1999/xhtml}html)
{
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Recherche de mot</title>
</head>
<body>
<h3>Mon dictionnaire</h3>
<p>Veuillez écrire mot à chercher</p>
<form method="post" action="traitement">
<p>Votre mot:<br />
<input name="mot" size="50"></input>
<input type="submit" /></p>
</form>
</body>
</html>
};
declare
%rest:path("/traitement")
%rest:POST
%rest:form-param("mot","{$mot}", "(no mot)")
function page:recherche($mot as xs:string) as element(Q{http://www.w3.org/1999/xhtml}html)
{
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Recherche du mot: {$mot} </title>
</head>
<body>
<h1>Recherche du Mot: {$mot}</h1>
<p> Letraitement :
{
let $books-doc := doc("personne.xml")
for $var in $books-doc/Dictionnaire/mot
return
if ($var/genre != $mot) then
<li> {$var/genre}</li>
else
<li> {$var/genre} </li>
}
</p>
</body>
</html>
};
page:start()
XML文件personne.xml:
<Dictionnaire>
<mot>
<genre>Mot 1</genre>
<synonyme>syno 1</synonyme>
<definition>Def 1</definition>
</mot>
<mot>
<genre>Mot 2</genre>
<synonyme>syno 2</synonyme>
<definition>Def 2</definition>
</mot>
</Dictionnaire>
顺便说一句,你不应该把自己的代码放在“外星人”命名空间下。使用你自己的域名。 –
您还应该考虑在'page:recherche'函数中设置'output:'注释,特别是您至少应该将输出的MIME类型设置为'text/html'或'application/xml + xhtml'。 – adamretter