Dear list,
I have an XML document in the database which contains datas that has the form below. The attribute "site" represents the location from where the captured data originates, the element "num" contains the number of the measure (the identifier. it is the same as a primary key), "value" contains the value (xs:token even if no WS are allowed inside of it) and "time" the time of the acquisition (xs:dateTime).
<datas site="xxx">
<data>
<num>5</num>
<value>700</value>
<time>2013-05-01T21:43:13</time>
</data>
...
</datas>
I want to update specific values at a given time. I usually have 300 or 400 values to update. That means, changing the content of child elements "value" and "time" but not "num" of many "data" elements. I wrote an XQuery script named update.xq that I placed in the Web repository. It will receive the site, the time, the numbers of the values to be updated (PK) and of course the values themselves as variables with a POST HTTP request. I do not use much typing as pretty much everything is xs:string. I guess it could be better but my problem is not there.
declare variable $nums as xs:string external;
declare variable $values as xs:string external;
declare variable $time as xs:dateTime external;
declare variable $site as xs:token external;
let $n := tokenize($nums,','), $v := tokenize($values,',') return
for $x at $i in $n
return
(replace value of node /datas[@site=$site]/data[num=$x]/value with $v[$i],
replace value of node /datas[@site=$site]/data[num=$x]/time with $time)
The problem is that sometimes it works and sometimes it gives me an HTTP 400 error with body :
Stopped at update.xq, 9/73:
[XUTY0008] Single element, text, attribute, comment or pi expected as replace target.
I know the replace target has to have the expected format and I think it has. I don't understand why I get this. I though it was due to too long stuff so I divided the list of datas to update into sublists of size 50. Now, the problem appears sometimes but not everytime.
When I use another XQuery script test.xq to display what was received, everything looks ok. The test script is :
declare variable $nums as xs:string external;
declare variable $values as xs:string external;
declare variable $time as xs:dateTime external;
declare variable $site as xs:token external;
let $n := tokenize($nums,','), $v := tokenize($values,',') return
for $x at $i in $n
return ($x,$v[$i])
For example, this update is ok:
<?xml version="1.0"?>
<run xmlns="http://basex.org/rest">
<text>update.xq</text>
<variable name="site" value="xxx"/>
<variable name="time" value="2013-05-02T09:58:38"/>
<variable name="nums" value="439,375,128,426,362,243,115,413,349,230,102,400,336,272,217,153,387,259,204,76,438,374,255,127,425,361,114,412,229,101,399,335,271,216,152,386,258,203,75,11,437,373,254,126,424,360,113,411,228,100"/>
<variable name="values" value="1515.200,408,true,973.806,3.892,false,false,0.000,0.0,6,true,430.400,false,2,327.670,true,410,true,45.410,true,0.992,408,false,false,575.060,3.545,false,0.995,87.270,false,436.800,false,94.000,45.410,false,410,false,14.135,true,true,2152.800,0,true,true,4.924,3.639,false,0.992,87.070,true"
/>
</run>
But this one is not :
<?xml version="1.0"?>
<run xmlns="http://basex.org/rest">
<text>update.xq</text>
<variable name="site" value="xxx"/>
<variable name="time" value="2013-05-02T09:58:38"/>
<variable name="nums"
value="36,398,334,215,385,202,74,436,372,253,125,423,359,240,112,442,410,378,227,131,99,429,397,365,333,214,118,54,416,384,352,288,233,201,105,73,9,435,403,371,339,275,220,156,124,422,390,358,294,239"/>
<variable name="values" value="true,428.400,false,45.059,410,12.710,true,2152.800,0,false,false,2.437,-0.101,false,true,4907.327,0.994,409,0.000,false,true,1012.437,448.800,0.000,false,62.260,false,true,304.600,410,0.0,58,87.249,13.180,true,true,true,2152.800,454.200,0,false,2,757,true,true,0.326,0.000,0.0,41,false"
/>
</run>
I think those documents are similar and I shouldn't see a difference. Either it works for both of them or it doesn't.
Any idea to point me in the right direction is welcome.
Thank in advance,
Ludovic Kuty