Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xquery insert node from external xs:string variable (basex)

Tags:

xquery

basex

I am attempting to insert new elements into an xml document using BaseX.

declare variable $part external; 
insert nodes $part as first into db:open("PARTDB")/assembly[@name="ZB09010"]

I am using the BaseX GUI for my testing and have defined the $part variable (by clicking on the $ icon).

If I use a "local" variable using for example

let $up := <Employee Name="Joe">
    <Personal>
      <SSN>666-66-1234</SSN>
    </Personal>
    <StaffInfo>
      <Position>Doctor</Position>
      <AccountableTo>Jeff</AccountableTo>
    </StaffInfo>
  </Employee>
  return
insert node $up as last into doc('office')/Staff

then the insert works correctly, however, with the external variable each character that is a reserved xml character is converted to the xml escape character sequence e.g. example: < becomes &lt;

I have succeeded in making it work by wrapping the variable with a function xquery:eval($part) but this feels to me like a hack.

Is there a type other than xs:string I should use to prevent the translation? Or is there some function I need to use with the external variable to prevent the translation. I also tried to wrap the $part xml content with CDATA but the xml was still converted to escape characters.

like image 604
mikee Avatar asked Dec 09 '15 23:12

mikee


1 Answers

It looks to me as if the value you are supplying for $part is not a node, but a string containing lexical XML. Converting lexical XML to a node tree is called parsing, so you need to parse the string to create a node (tree). You could do this by calling the fn:parse-xml function.

like image 185
Michael Kay Avatar answered Oct 24 '22 07:10

Michael Kay