Question: In eXist-db (4.7) I have a long TEI-XML file from which I am creating thousands of unique TEI-XML files. I am using a function like this:

```
let $list := doc(concat($globalvar:URIdata,"list_collections.xml"))
let $makefile := for $bib in $list//tei:bibl[@type="collection"]
                  let $newdoc := 
                            <listBibl xmlns="http://www.tei-c.org/ns/1.0" type="collection">{$bib}</listBibl>
             return xmldb:store($globalvar:URIdata, concat($bib//@xml:id,".xml"),$newdoc)
return $makefile

```

Everything works fine. 

However, I'd like to add a prologue to the top of each new file which points to my custom TEI schema:

```
<?xml-model href="../dictionaries/tei_thema.rng" 
            type="application/xml" 
            schematypens="http://relaxng.org/ns/structure/1.0"?>

```

Is there a way to do this within the function above? If not, is there a secondary process I can create to insert this at the top of the new file?

Many thanks in advance.

Answer: 
To construct a new document explicitly use `document { }` e.g. in your case

```
let $newdoc := document { <?xml-model href="../dictionaries/tei_thema.rng" type="application/xml" schematypens="http://relaxng.org/ns/structure/1.0"?>, <listBibl xmlns="http://www.tei-c.org/ns/1.0" type="collection">{$bib}</listBibl> }

```
To add the `processing-instruction` just add it as the first member of a sequence consisting of it and your content.

```
 let $doc = <demo></demo>
 let $newdoc := (<?xml-model href="../dictionaries/tei_thema.rng" 
        type="application/xml" 
        schematypens="http://relaxng.org/ns/structure/1.0"?>, $demo)
 return $newdoc

```