2014-02-24 36 views
1

这里是我的代码,其目的是创建一个XQuery请求,以便在xml文档(personne.xml,下面给出)中找到一个单词,但我遇到问题:变量$var包含整个xml文件,即使我选择了节点$books-doc/Dictionnaire/motXQuery请求打开整个文件

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> 
+1

顺便说一句,你不应该把自己的代码放在“外星人”命名空间下。使用你自己的域名。 –

+0

您还应该考虑在'page:recherche'函数中设置'output:'注释,特别是您至少应该将输出的MIME类型设置为'text/html'或'application/xml + xhtml'。 – adamretter

回答

1

这是一个命名空间的问题。你包括

<html xmlns="http://www.w3.org/1999/xhtml"> 

默认命名空间,你将不得不定义正确的命名空间,或者您可以使用通配符,例如

declare 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> 

}; 
+0

谢谢你的回答,它运作良好。 我很感激。 –

+0

@ user3346640很好用。你能接受它作为答案吗? – dirkk

相关问题