Loading TOC...

xdmp:save

xdmp:save(
   $path as xs:string,
   $node as node(),
   [$options as (element()|map:map)?]
) as empty-sequence()

Summary

Serializes a node as text and saves it to a file. The node can be any node, including a document node, an element node, a text node, or a binary node.

Parameters
$path The output file pathname. The path can be fully qualifed or relative. Relative pathnames are resolved from the directory in which MarkLogic Server is installed.
$node The node to be serialized.
$options Options with which to customize this operation. You can specify options as either an options XML element in the "xdmp:save" namespace, or as a map:map. The options names below are XML element localnames. When using a map, replace the hyphens with camel casing. For example, "an-option" becomes "anOption" when used as a map:map key. This function supports the following options:
output-encoding
Specifies the encoding to use when saving the document.
output-sgml-character-entities
Specifies if character entities should be output upon serialization of the XML. Valid values are normal, none, math, and pub. By default (that is, if this option is not specified), no SGML entities are serialized on output, unless the App Server is configured to output SGML character entities.
method
Valid values are xml, html, xhtml, and text. This is like the corresponding part of both the XSLT xsl:output instruction and the MarkLogic XQuery xdmp:output prolog statement.
cdata-section-elements
A list of space-separated QNames to output as CDATA sections. This is like the corresponding part of both the XSLT xsl:output instruction and the MarkLogic XQuery xdmp:output prolog statement.
encoding
The encoding. This is like the corresponding part of both the XSLT xsl:output instruction and the MarkLogic XQuery xdmp:output prolog statement.
use-character-maps
One or more of the following values, separated by spaces. Valid values are xdmp:sgml-entities-normal, xdmp:sgml-entities-math, and xdmp:sgml-entities-pub. This is like the corresponding part of both the XSLT xsl:output instruction and the MarkLogic XQuery xdmp:output prolog statement.
media-type
A mimetype representing a media type. For example, text/plain or application/xml (or other valid mimetypes). This is like the corresponding part of both the XSLT xsl:output instruction and the MarkLogic XQuery xdmp:output prolog statement.
byte-order-mark
Valid values are yes or no. This is like the corresponding part of both the XSLT xsl:output instruction and the MarkLogic XQuery xdmp:output prolog statement.
indent
Specifies if typed XML (that is, XML for which there is an in-scope schema) should be pretty-printed (indented). Valid values are yes or no. This is like the corresponding part of both the XSLT xsl:output instruction and the MarkLogic XQuery xdmp:output prolog statement.
indent-untyped
Specifies if untyped XML (that is, XML for which there is no in-scope schema) should be pretty-printed (indented). Valid values are yes or no. This is like the corresponding part of both the XSLT xsl:output instruction and the MarkLogic XQuery xdmp:output prolog statement.
indent-tabs
Specifies if tab characters should be used instead of 8 consecutive spaces when indenting. Valid values are yes or no.
include-content-type
Include the content-type declaration when serializing the node. Valid values are yes or no.
escape-uri-attributes
Valid values are yes or no. This is like the corresponding part of both the XSLT xsl:output instruction and the MarkLogic XQuery xdmp:output prolog statement.
doctype-public
A public identifier, which is the public identifier to use on the emitted DOCTYPE. This is like the corresponding part of both the XSLT xsl:output instruction and the MarkLogic XQuery xdmp:output prolog statement.
doctype-system
A system identifier, which is the system identifier to use on the emitted DOCTYPE. This is like the corresponding part of both the XSLT xsl:output instruction and the MarkLogic XQuery xdmp:output prolog statement.
omit-xml-declaration
Valid values are yes or no. This is like the corresponding part of both the XSLT xsl:output instruction and the MarkLogic XQuery xdmp:output prolog statement.
standalone
Valid values are yes or no. This is like the corresponding part of both the XSLT xsl:output instruction and the MarkLogic XQuery xdmp:output prolog statement.
normalization-form
Valid values are NFC, NFD, and NFKD. This is like the corresponding part of both the XSLT xsl:output instruction and the MarkLogic XQuery xdmp:output prolog statement.
default-attributes
Specifies whether attributes defaulted with a schema should be included in the serialization. Valid values are yes or no. This is like the corresponding part of both the XSLT xsl:output instruction and the MarkLogic XQuery xdmp:output prolog statement.

Required Privileges

http://marklogic.com/xdmp/privileges/xdmp-save

Example

(: serialize an XML document in the database to a file on disk :)
let $mynode := doc("/mydocs/example.xml")
return xdmp:save("hello.txt", $text)

Example

(: save a text file :)
let $text := text { "hello" }
return xdmp:save("hello.txt", $text)

Example

(: save a text document stored in the database to disk, explicitly
   specifying the output encoding in an XML options node. :)
let $pdf := doc("/mydocs/stuff.pdf")
return
xdmp:save("mystuff.txt", $txt,
    <options xmlns="xdmp:save">
      <output-encoding>utf-8</output-encoding>
    </options>)

Example

(: save a text document stored in the database to disk, explicitly 
   specifying the output encoding in an options map. :)
let $txt := doc("/mydocs/stuff.txt")
return
xdmp:save("mystuff.txt", $txt, 
          map:map() => map:with("outputEncoding", "utf-8"))

Stack Overflow iconStack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.

Comments

The commenting feature on this page is enabled by a third party. Comments posted to this page are publicly visible.
  • well Disqus messed-up my message. I was gtring to say that I try to use #10 to end a line ML is adding #13 before it (presumably only the Windows version of ML); but STILL adds it if I already have #13#10 combination in the text. Is that a bug?
  • Is it possible to write lines to a file from within a for-loop? I know we can concatenate the lines to a variable and write that out, but that would probably use up too much memory for larger number of records.
    • xdmp:save does not have an append option; it expects to write the whole file at once. If you have a middle tier, say in Java, then an alternative approach is to have MarkLogic return blocks of content and have Java write it to the file system. Are you working with anyone at MarkLogic who could file an RFE with your company details?
      • Thanks David. I was thinking of using this for some ad-hoc reports that we need to create from time to time, and hoping we could do it without Java. Guess I'll see if I can write a configurable Java program where I can specify the xquery and the program fetches record by record to write to a file. We have an ML support contract, but I haven't raised this as a support ticket, if that's what you're asking
        • An RFE (request for enhancement) is more likely to happen if customer requests are attached to it. If you'd like to see xdmp:save have append functionality, you can ask ML Support to file an RFE for that.
  • What happens if the target file already exists? Is there any way to tell xdmp:save to fail instead of overwriting? (to be sure not to erase anything existing)
  • in the example where a PDF file is saved, what is the importance (if any) of the output encoding option? Given that it's a binary file, I guess this option should have no influence whatsoever, right? Jakob.
    • You are right Jakob, that example would make more sense if it were an html file or some other text file.