Search code examples
restdeploymentxquerymarklogic

XQuery file inserted into MarkLogic using REST API causes unexpected syntax error


I have the following XQuery file which assembles a JATS XML document from a series of nodes (the content is saved as individual files in a content management system):

let $jatsManifest := xdmp:unquote(xdmp:get-request-body())
let $jatsDoc := xdmp:xslt-eval(

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xdmp="http://marklogic.com/xdmp" extension-element-prefixes="xdmp" version="2.0">

<xsl:template match="/">
  <article xmlns:xlink="http://www.w3.org/1999/xlink">
      <xsl:variable name="node"><xsl:value-of select="concat('/workspace/SpacesStore/', /jats-manifest/@id)"/></xsl:variable>
      <xsl:copy-of select="document($node)" />
<body>
    <xsl:apply-templates select="jats-manifest/segment" />
</body>
</article>
</xsl:template>

<xsl:template match="segment">
    <!-- Create an ID that passes the JATS manifest validation (workspace-SpacesStore-{UUID}) -->
    <xsl:variable name="nodeId"><xsl:value-of select="replace(replace(@id, '://', '-'), '/', '-')"/></xsl:variable>
    <sec id="{{$nodeId}}">
        <!-- Create the nodeName as known in ML (/workspace/SpacesStore/{UUID}) -->
        <xsl:variable name="node"><xsl:value-of select="replace(concat('/', @id), '://', '/')"/></xsl:variable>
        <title><xsl:copy-of select="document($node)"/></title>
        <xsl:apply-templates />
    </sec>
</xsl:template>

<xsl:template match="chunk">

  <!-- Create the nodeName as known in ML (/workspace/SpacesStore/{UUID}) -->
  <xsl:variable name="node"><xsl:value-of select="replace(concat('/', @id), '://', '/')"/></xsl:variable>
  <xsl:copy-of select="document($node)"/>
</xsl:template>

</xsl:stylesheet>
,
$jatsManifest)
let $articleId := $jatsManifest/jats-manifest/@id
let $jatsURL := fn:concat('/JATS/workspace/SpacesStore/', $articleId)
let $_ := xdmp:document-insert($jatsURL, $jatsDoc)
return $jatsURL

It is accessible on a MarkLogic server at the url http://localhost:8012/generateJATSDoc.xqy and accepts something like this in the HTTP body:

<?xml version="1.0" encoding="UTF-8"?>
<jats-manifest id="c4b3baaa-038c-43cf-8667-31b208cdb6ad">
    <title>English</title>
    <segment id="workspace://SpacesStore/bc4a3486-0c4d-4f95-b677-61269645fc26">
        <chunk id="workspace://SpacesStore/8535cbe4-0b90-4d4e-baf4-8de678d7438f"/>
    </segment>
</jats-manifest>

If the MarkLogic app server handling the XQuery file is set to use the file system as the modules database, this works correctly and a JATS document is assembled.

If, however, the app server uses a database for the modules and the XQuery file is inserted into the database using the MarkLogic REST API like so:

curl -v -X PUT --anyauth -u admin:admin --header "Content-Type:application/xquery" \
-d@"generateJATSDoc.xqy" \
"http://127.0.0.1:8012/v1/documents?uri=/generateJATSDoc.xqy&database=My-Modules-Database"

I get the following error when calling it in exactly the same way as before:

{"errorResponse":{"statusCode":400, "status":"Bad Request", "messageCode":"XDMP-UNEXPECTED", "message":"XDMP-UNEXPECTED: (err:XPST0003) Unexpected token syntax error, unexpected Dollar_"}}

Looking at the file in Query Console it looks like this:

Screenshot of XQuery file after inseting via REST

I would maybe expect it to be formatted like an XQuery file so I'm not sure if this is causing the problem or not.

I am authenticating with MarkLogic using an admin user to rule out any permission related problems but this doesn't seem to have any impact.

This is using MarkLogic 8.0-6.1.

I don't understand what could cause this different behaviour.


Solution

  • I believe that this is due to the way that your are posting in the XQuery. As you can see your line breaks have been removed which may mean that the XQuery including nested comments is not being processed correctly. If you PUT in using "--data-binary" instead of "-d" it should work.