ili.M
ili.M

Reputation: 11

XQuery request opens whole file

Here is my code which aims to create an XQuery request in order to find a word in a xml document (personne.xml, given below), but I have a problem: the variable $var contains the whole xml file, even if I have selected the node $books-doc/Dictionnaire/mot.

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

Upvotes: 1

Views: 93

Answers (1)

dirkk
dirkk

Reputation: 6218

It is a namespace issue. You include a default namespace at

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

You will either have to define the correct namespace or you could use the wildcard operator, e.g.

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>

};

Upvotes: 1

Related Questions