xquerymarklogicmarklogic-8

Query generate 'XDMP-CHILDNODEKIND: $final -- element nodes cannot have binary node children' error


Below is my simple query, that reads all files from a directory and holds all files in $final Variable save in one single file.

But when run this query, after taking some time, it prompt [1.0-ml] XDMP-CHILDNODEKIND: $final -- element nodes cannot have binary node children error.

let $input-dir :=xdmp:filesystem-directory("d:\work\may\06-05-2019\all- 
 feeds-input-output\clc\log\clc-true-ouput\")/dir:entry
let $final :=      
      for $each at $i in $input-dir
      return  
        xdmp:document-get($each/dir:pathname/text(), 
          <options xmlns="xdmp:document-get">
            <repair>full</repair>
            <encoding>UTF-8</encoding>
          </options>)
return 
  xdmp:save("D:\WORK\MAY\06-05-2019\ALL-FEEDS-INPUT-OUTPUT\CLC\LOG\COMBINE-XMLs\Combine-CLC-TRUE-INPUT.xml", 
       document{<records>{$final}</records>})

Actually, I have 10000 small files in the local system and I want to Merge in single file.


Solution

  • The directory likely contains binary documents (i.e. PDF, images, etc). When you read those documents with xdmp:document-get(), you will get a binary() node.

    As the error message indicates, binary() nodes cannot be children of an XML element.

    Your $final variable will be a sequence of documents, and at least one of them is a binary() node.

    You could exclude those binary() nodes. For instance, by adding a predicate filter to the results of the xdmp:document-get():

    let $final :=      
      for $each at $i in $input-dir
      return 
        xdmp:document-get($each/dir:pathname/text(), 
          <options xmlns="xdmp:document-get">
            <repair>full</repair>
            <encoding>UTF-8</encoding>
          </options>
        )[not(. instance of binary())]
    

    or you could base64 encode the binary data, so that it can be added to the XML:

    let $final :=      
      for $each at $i in $input-dir
      let $doc := 
        xdmp:document-get($each/dir:pathname/text(), 
          <options xmlns="xdmp:document-get">
            <repair>full</repair>
            <encoding>UTF-8</encoding>
          </options>)
      return
        if ($doc instance of binary()) 
        then xdmp:base64-encode($doc)
        else $doc