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 <
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With