xquerymarklogicentityreference

XQuery file returning invalid entity reference using special characters


I have the following query in a MarkLogic XQuery file, and I am seeing the following error message returned

XDMP-ENTITYREF: (err:XPST0003) Invalid entity reference " " . See the MarkLogic server error log for further detail.

The following is the code I am using in the XQuery file.

xquery version "1.0-ml";

declare variable $query := 

  cts:or-query
  ((
    cts:element-word-query(xs:QName("lines"),"l&l"),
    cts:element-word-query(xs:QName("lines"),"pool & cue"),
    cts:element-word-query(xs:QName("lines"),"look")
  ));

declare function local:do-query(){
  element xml {
    for $i in cts:uris( (), (), $query)
    let $item := doc($i)

    return
      element item {
        element title { $item/title/string() }
    }
  }
};

local:do-query()

Obviously the 2x tags i am looking for are l&l and pool & cue. I have also looked into the repair-full suggestion in another question posted, but couldn't figure out how that fits into this query. If I removed the ones with special characters, it works as expected.

Any ideas?


Solution

  • Based on the additional info in the comments to the question, this is not an issue with the execution of the code, but rather with deployment of the code.

    This happens often if you insert code using QConsole, or some other ways in which you evaluate XQuery code. The & get interpreted, and translated to the & character it represents. If you then write that into a .xqy file into some Modules database, it does not get escaped back into & again, since XQuery files are stored as plain text in MarkLogic, and & doesn't get escaped in plain text.

    A better way to deploy code is by uploading or inserting from disk. That way characters like &, >, and { inside XML won't get interpreted, but preserved and inserted as is. There are tools like ml-gradle and Roxy that make deploying MarkLogic code very easy. Consider using these. Alternatively you could also look into using Curl against the Management REST api.

    If you want to use QConsole after all, escape characters like & twice. E.g. & becomes &amp;amp;, and < becomes &amp;lt;.

    HTH!