6

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.

mikee
  • 335
  • 3
  • 11

1 Answers1

4

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.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • I tried the fn:parse-xml function, my xml has a namespace prefix and the parse-xml function returns an error – mikee Dec 10 '15 at 14:53
  • I tried the fn:parse-xml function, my xml has a namespace prefix (asy) and the parse-xml function returns an error FODC0006 (Line 1): The prefix "asy" for element "asy:part" is not bound. If I add a declare namespace statement for asy ahead of the xml I get a different error FODC0006 (Line 1): Content is not allowed in prolog. However, if I use the query:eval($part) function it appears to work. – mikee Dec 10 '15 at 15:03
  • 1
    If the content has an undeclared namespace prefix then parse-xml is quite right to reject it. query:eval() sounds like a vendor extension, so it's allowed to do anything it likes. – Michael Kay Dec 10 '15 at 16:07