Not completely clear what you’re trying to do, but:

1) you can’t have reserved characters like “<“ , “>” in an XML attribute, so you should use &gt;, &lt; entities — something like:

<variable name="$message" value="&lt;Id&gt;CTN55&lt;/Id&gt;" />

2) If you bind to the variable message with that statement, then you don’t need the ‘let $message” , but you do need to declare the variable as external, as it’s being bound with that variable element: 

<text>declare variable $message external; 
. . . 


3) and then, since that variable is a text string, you can use Fn:parse-xml-fragment() to convert it into a node that you can insert somewhere:


insert node  fn:parse-xml-fragment($message) into . . . 

I’m not sure that the destination node for your insert into clause is going to do what you want, but I can’t tell without knowing what the initial value of the doc node is. 

I *think* this may answer your question, but if not, try to post what the initial value of that doc node is and what you want the final result to look like. 

— Steve M. 



On May 14, 2020, at 3:44 PM, Ramzi Hammouda <Ramzi.Hammouda@se.linedata.com> wrote:

Hello BaseX team,
 
Using Resful API, I'm trying to make POST method update BaseX database, I want to insert an xml node to an existing document calastone.xml in calastone database , but the node contains '<', you replied to a previous email that I should use fn:parse-xml to make this happen without error.
 
I tried using the function fn:parse-xml, I had success with using it in BaseX GUI like this:
 
let $message := fn:parse-xml('<Id>CTN55</Id>')
return insert nodes $message as last into doc("calastone/calastone.xml")
 
But when I tried to do the same with POST method from my application with using this xquery:
 
string xquery = "<query><text>let $message:= fn:parse-xml('<Id>CTN58</Id>')" +
                    "return insert nodes $message as last into doc(\"calastone/calastone.xml\")</text>" +
                    "<variable name = 'message' value = fn:parse-xml('<Id>CTN58</Id>'))/></query>";
 
I get the error: "" (Line 1): Element type "variable" must be followed by either attribute specifications, ">" or "/>"
 
Can you help on how to do this insertion?
 
Thanks.
 

P
 Respectons ensemble l'environnement. N'imprimez ce message que si nécessaire. Let's respect the environment together. Only print this message if necessary.