Search code examples
xmlxpathxqueryexist-db

New records added via XQuery update insert are not indexed ('seen') by eXist-db until I manually edit the file on oXygen


I'm running eXist-db 3.6.1 on CentOS 7.4.1708 (Core), Java(TM) SE Runtime Environment (build 1.8.0_152-b16). I want to be able to add a new record after the last one in an persons.xml structured like so:

<person xml:id="pe0472"> [...] </person>
<person xml:id="pe0473"> [...] </person>

I have a html form inside a function in my app.xql called via data-template in editpers.html which grabs the data:

declare function app:newpers($node as node(), $model as map(*), $searchkey as xs:string?)

{

let $surname := doc(concat($config:app-root, '/input/persons.xml'))//tei:listPerson/tei:person[@xml:id=$searchkey]/tei:persName/tei:surname

[...]

return

<div class="container">

<form action="add.html" method="POST">  

<div class="form-group">

Surname:<br/>
<input type="text" name="surname" value="{$surname}"/>
[...]
<input class="btn btn-default" type="submit" value="Submit"/>
</div>
</form>
</div>
};

The form's submit sends to add.html, which references to the app:addpers function via data-template:

declare function app:addpers($node as node(), $model as map(*)) {

let $peid := request:get-parameter('peid', '')
let $surname := request:get-parameter('surname', '')
[...]
let $on-disk := doc(concat($config:app-root, '/input/persons.xml'))

let $insert := update insert

<person xml:id="{$peid}" >
<persName xmlns="http://www.tei-c.org/ns/1.0">
<surname>{$surname}</surname>
[...]
</persName>
</person> 

following $on-disk//tei:person[@xml:id][last()]

return

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Added</title>
    </head>
    <body>
         <div>
            <h3>Success!</h3>
        </div>
    </body>
</html>

};

I get the nice message, and if I open the master persons.xml file in oXygen I can see the new record nicely added. Nevertheless it doesn't show up in my webpage which shows all the records via this query:

declare function app:listPers($node as node(), $model as map(*)) {

for $person in doc(concat($config:app-root, '/input/persons.xml'))//tei:listPerson/tei:person
    return

    <tr>
        <td>{$person/tei:persName/tei:forename}</a></td>
    </tr>    
};

This shows all the records but not the latest one added via the app:addpers function. Refreshing the cache on the browser, which helps when I update values on existing records with:

let $update := update value $on-disk//tei:person[@xml:id eq $peid]/tei:persName/tei:surname with $surname

(the edited records are not shown until I clear the cache) doesn't help this time.

In order to show the new records on my front-end (app:listPers) is to do something stupid to the master file in oXygen (say, add and delete a space anywhere, then save it), and the new record will magically appear on my browser's page. Is this expected behaviour? I find it very puzzling and I cannot think of anything else I can try to fix this issue.

I have tested it on Firefox 58.0.1 and Safari 11.0.3.


Solution

  • My workaround, for now, is to add a

        <script type="text/javascript">
            window.addEventListener('load', function (e) {
                window.applicationCache.addEventListener('updateready', function (e) {
                    window.location.reload(true);
                    }, false);
                console.log("loaded");
            }, false);
        </script>
    

    to the head of the affected pages, which forces a no-cache reload of the page without having to do it manually.