xmlxqueryexist-dbtei

Write TEI compliant XML File with xQuery using xmldb:store while iterating over documents


So I need to iterate over a given set of XML documents, search for encoding errors and then create a XML file, that's compliant with TEI standards and has a table in its body containing the file names and the errors of each file structured like this:

<table xmlns="http://www.tei-c.org/ns/1.0" rows="30" cols="10">
    <row role="label">
        <cell role="data">file name</cell>
        <cell role="data">error 1</cell>
        <cell role="data">error 2</cell>
    </row>
    <row role="data">
        <cell role="label">file 1</cell>
        <cell role="data">xPath to error 1 in file 1</cell>
        <cell role="data">xPath to error 2 in file 1</cell>
    </row>
    <row role="data">
        <cell role="label">file 2</cell>
        <cell role="data"><!-- empty if no error in file --></cell>
        <cell role="data">xPath to error 2 in file 2</cell>
    </row>
</table>

My problem is with writing the result file. Since it needs to be TEI compliant I need a TEI root node and so on. I only want to write the head of the file once into the file, but need to iterate over each document to get the errors. So when I try:

    for $doc in $douments
    (: whole bunch of code here to get the errors :)
    let $output := 
    <TEI xmlns="http://www.tei-c.org/ns/1.0"> 
    (: everything put into the wanted order :) 
    </TEI>
    return
        xmldb:store("db/Output/", "result.xml", $output)

I naturally only get the errors of the last file in result.xml, since I iterate over every file and each time it does that, it creates a new file with the head but only the errors from the current file. I managed to get the correct output without writing it into a file (using oXygen).


So how can I write the head of the file just once?

I would need to get it out of the for loop but I can't find a way to do that. I'm fairly new to xQuery and was told to write the output into a variable and use it like it's done above but then I encounter this problem.

Can someone help me find a solution? Thank you in advance!

Greetings from Bavaria


Solution

  • If you want to create a single document then

    let $output := document { 
      <TEI xmlns="http://www.tei-c.org/ns/1.0">
      {
      for $doc in $douments
        (: whole bunch of code here to get the errors :)
      }
      </TEI> 
    }
    return xmldb:store("db/Output/", "result.xml", $output)
    

    might be what you are looking after.