MarkLogic Server 11.0 Product Documentation
es:serialize-attachments

es:serialize-attachments(
   $instance as map:map,
   $format as xs:string
) as map:map

Summary

This function is deprecated and will be removed in a future release.
A utility function used to serialize the attachments on an instance into either JSON or XML envelopes.

Parameters
instance The instance whose attachments are to be serialized.
format The format in which to serialize the attachments. Must be one of "json" or "xml".

Usage Notes

You will not usually call this function outside of code generated by Entity services. The generated code uses this function to serialize attachments when constructing envelope documents.

If the data format specified in the format does not match the format of an attachment, then that attachment is serialized as a string. For example, if an attachment is an XML element and the requested format is JSON, then the attachment is serialized as the string representation of the XML.

See Also

Example

xquery version "1.0-ml";
import module namespace es = "http://marklogic.com/entity-services"
  at "/MarkLogic/entity-services/entity-services.xqy";
import module namespace person = 
  "http://example.org/example-person/Person-0.0.1"
  at "/es-gs/person-0.0.1-conv.xqy";

let $source := fn:doc('/es-gs/raw/1234.xml')
let $instance := person:extract-instance-Person($source)
let $envelope-format := "xml"
let $attachments := es:serialize-attachments($instance, "xml")

return ($instance, $attachments)

(: Returns the following output. Notice that xml attachment stored
 : in the instance as a string has been reconstitued as an XML element
 : node in the generated attachments.
 :
 : ( {"$type":"Person", 
 :    "$attachments":"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<person>\n  <pid>1234</pid>\n  <given>George</given>\n  <family>Washington</family>\n</person>", 
 :    "id":"1234", 
 :    "firstName":"George", 
 :    "lastName":"Washington", 
 :    "fullName":"George Washington"},
 :
 :   <es:attachments xmlns:es="http://marklogic.com/entity-services">
 :     <person><pid>1234</pid><given>George</given><family>Washington</family></person>
 :   </es:attachments> )
 :)
  

Example

xquery version "1.0-ml";
import module namespace es = "http://marklogic.com/entity-services"
  at "/MarkLogic/entity-services/entity-services.xqy";
import module namespace person = 
  "http://example.org/example-person/Person-0.0.1"
  at "/es-gs/person-0.0.1-conv.xqy";

let $source := fn:doc('/es-gs/raw/1234.xml')
let $instance := person:extract-instance-Person($source)
let $envelope-format := "json"
let $attachments := es:serialize-attachments($instance, "xml")

return ($instance, $attachments)

(: Returns the following output. Notice that XML attachment stored
 : in the instance is returned as a string in the attachments when
 : you request JSON serialization.
 :
 : ( {"$type":"Person", 
 :    "$attachments":"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<person>\n  <pid>1234</pid>\n  <given>George</given>\n  <family>Washington</family>\n</person>", 
  :   "id":"1234", 
  :   "firstName":"George", 
  :   "lastName":"Washington", 
  :   "fullName":"George Washington"},
  :
  :  {"attachments":[
  :    "<person><pid>1234</pid><given>George</given><family>Washington</family></person>"
  :  ]} )
 :)
  
Powered by MarkLogic Server | Terms of Use | Privacy Policy