can restxq add a document to the db and return an http response?

Thursday, 6 September 2012 · 5 messages · last reply 7 Sep 2012
CG
Christian Grünin reply to Colin McEnearney, 6 Sep 2012, 23:176 Sep 2012 21:37 #
Hi Colin,
Quoted text · Colin McEnearney · 2 lines
> Is it possible with restxq to add a document to a db? And if so, to also > return an http response?
sure; you need to mark your function as updating, and the db:output() function will allow you to return data. Your updated example is shown below. Hope this helps, Christian ___________________________________ module namespace page = 'http://basex.org/modules/web-page';
declare %updating
        %restxq:path("form/")
        %restxq:POST
        %restxq:form-param("content","{$form-input}", "'no message delivered'")
        function page:add-doc($form-input as xs:string) {

  let $new-doc:=
<root>
  <tag>
    <form-data>{ $form-input }</form-data>
   </tag>
</root>

  return (
    db:add("test_db", $new-doc, "test.xml"),
    db:output("database was updated.")
  )
};
CM
Colin McEnearney6 Sep 2012 23:17 #
Hi, Is it possible with restxq to add a document to a db? And if so, to also return an http response? I am trying to get the first part working with something along these lines:
declare %rest:path("form/")
        %rest:POST
        %rest:form-param("content","{$form-input}", "'no message
delivered'")
        function page:add-doc($form-input as xs:string) {

  let $new-doc:=
<root>
  <tag>
    <form-data>{ $form-input }</form-data>
   </tag>
</root>

  return db:add("test_db", $new-doc, "test.xml")
};
Many thanks, Colin
CM
Colin McEnearneyin reply to Christian Grün, 6 Sep 2012, 21:376 Sep 2012 23:44 #
That's great - thanks! I was looking here for list of all annotations: http://docs.basex.org/wiki/RESTXQ But now I see these - %public, %private, or %updating - on this page too: http://docs.basex.org/wiki/XQuery_3.0#Annotations Are there any other annotations recognized by basex? On Thu, Sep 6, 2012 at 3:37 PM, Christian Grün <christian.gruen at gmail.com>wrote:
Quoted text · Christian Grün · 35 lines
> Hi Colin, > > > Is it possible with restxq to add a document to a db? And if so, to also > > return an http response? > > sure; you need to mark your function as updating, and the db:output() > function will allow you to return data. Your updated example is shown > below. > > Hope this helps, > Christian > ___________________________________ > > module namespace page = 'http://basex.org/modules/web-page'; > > declare %updating > %restxq:path("form/") > %restxq:POST > %restxq:form-param("content","{$form-input}", "'no message > delivered'") > function page:add-doc($form-input as xs:string) { > > let $new-doc:= > <root> > <tag> > <form-data>{ $form-input }</form-data> > </tag> > </root> > > return ( > db:add("test_db", $new-doc, "test.xml"), > db:output("database was updated.") > ) > }; >
CM
Colin McEnearney7 Sep 2012 00:21 #
Hi Christian, I am getting this error with the below code [FOUP0001] Document expected, <root>...</root> found. Is this a problem with the way $new-doc is declared? Thanks, Colin module namespace page = 'http://basex.org/modules/web-page'; declare %updating %restxq:path("form/") %restxq:POST %restxq:form-param("content","{$form-input}", "'no message delivered'") function page:add-doc($form-input as xs:string) {
let $new-doc:=
    <root>
      <tag>
        <form-data>{ $form-input }</form-data>
       </tag>
    </root>

      return (
        db:add("test_db", $new-doc, "test.xml"),
        db:output("database was updated.")
      )
    };
On Thu, Sep 6, 2012 at 3:44 PM, Colin McEnearney <colinmcenearney at gmail.com>wrote:
Quoted text · Colin McEnearney · 54 lines
> That's great - thanks! > > I was looking here for list of all annotations: > > http://docs.basex.org/wiki/RESTXQ > > But now I see these - %public, %private, or %updating - on this page > too: > > http://docs.basex.org/wiki/XQuery_3.0#Annotations > > Are there any other annotations recognized by basex? > > > > On Thu, Sep 6, 2012 at 3:37 PM, Christian Grün <christian.gruen at gmail.com>wrote: > >> Hi Colin, >> >> > Is it possible with restxq to add a document to a db? And if so, to also >> > return an http response? >> >> sure; you need to mark your function as updating, and the db:output() >> function will allow you to return data. Your updated example is shown >> below. >> >> Hope this helps, >> Christian >> ___________________________________ >> >> module namespace page = 'http://basex.org/modules/web-page'; >> >> declare %updating >> %restxq:path("form/") >> %restxq:POST >> %restxq:form-param("content","{$form-input}", "'no message >> delivered'") >> function page:add-doc($form-input as xs:string) { >> >> let $new-doc:= >> <root> >> <tag> >> <form-data>{ $form-input }</form-data> >> </tag> >> </root> >> >> return ( >> db:add("test_db", $new-doc, "test.xml"), >> db:output("database was updated.") >> ) >> }; >> > >
CG
Christian Grün7 Sep 2012 02:49 #
You need to additionally wrap your constructed element into a document node:
  let $new-doc:= document {
<root>
  <tag>
    <form-data>{ $form-input }</form-data>
   </tag>
</root> }
Hope this helps, Christian
Quoted text · Colin McEnearney · 96 lines
> Hi Christian, > > I am getting this error with the below code > > [FOUP0001] Document expected, <root>...</root> found. > > Is this a problem with the way $new-doc is declared? > > Thanks, > Colin > > > module namespace page = 'http://basex.org/modules/web-page'; > > declare %updating > %restxq:path("form/") > %restxq:POST > %restxq:form-param("content","{$form-input}", "'no message > delivered'") > function page:add-doc($form-input as xs:string) { > > let $new-doc:= > <root> > <tag> > <form-data>{ $form-input }</form-data> > </tag> > </root> > > return ( > db:add("test_db", $new-doc, "test.xml"), > db:output("database was updated.") > ) > }; > > > > > On Thu, Sep 6, 2012 at 3:44 PM, Colin McEnearney <colinmcenearney at gmail.com> > wrote: >> >> That's great - thanks! >> >> I was looking here for list of all annotations: >> >> http://docs.basex.org/wiki/RESTXQ >> >> But now I see these - %public, %private, or %updating - on this page >> too: >> >> http://docs.basex.org/wiki/XQuery_3.0#Annotations >> >> Are there any other annotations recognized by basex? >> >> >> >> On Thu, Sep 6, 2012 at 3:37 PM, Christian Grün <christian.gruen at gmail.com> >> wrote: >>> >>> Hi Colin, >>> >>> > Is it possible with restxq to add a document to a db? And if so, to >>> > also >>> > return an http response? >>> >>> sure; you need to mark your function as updating, and the db:output() >>> function will allow you to return data. Your updated example is shown >>> below. >>> >>> Hope this helps, >>> Christian >>> ___________________________________ >>> >>> module namespace page = 'http://basex.org/modules/web-page'; >>> >>> declare %updating >>> %restxq:path("form/") >>> %restxq:POST >>> %restxq:form-param("content","{$form-input}", "'no message >>> delivered'") >>> function page:add-doc($form-input as xs:string) { >>> >>> let $new-doc:= >>> <root> >>> <tag> >>> <form-data>{ $form-input }</form-data> >>> </tag> >>> </root> >>> >>> return ( >>> db:add("test_db", $new-doc, "test.xml"), >>> db:output("database was updated.") >>> ) >>> }; >> >> >

⚡Generated with XQuery