开发者

Is there straightforward XQuery in Marklogic that produces JSON output out of simple XML sequence to be used with JQuery autocompete

开发者 https://www.devze.com 2023-02-21 18:40 出处:网络
I am trying to get the jQuery autocomplete widget to get the data from XML source stored in the Marklogic XML server.

I am trying to get the jQuery autocomplete widget to get the data from XML source stored in the Marklogic XML server.

The XML data is very simple looks like this:

<id>Bank ATM</id>
<id>PostageShipping</id>
<id>WebHosting</id>
<id>ClientParking</id>

Markllogic server does have a function xdmp:to-json that should do something like that, however 开发者_如何学Pythonwhen used like this

let $ex := fn:collection()//ex:Expense
return xdmp:to-json($ex/ex:id)

It returns the output that looks like that

    ["fn:doc("/expenses/Expenses-Combined.xml")/ex:Expenses/ex:Expense[1]/ex:id",
"fn:doc("/expenses/Expenses-Combined.xml")/ex:Expenses/ex:Expense[2]/ex:id", 
"fn:doc("/expenses/Expenses-Combined.xml")/ex:Expenses/ex:Expense[3]/ex:id", 
"fn:doc("/expenses/Expenses-Combined.xml")/ex:Expenses/ex:Expense[4]/ex:id", 
"fn:doc("/expenses/Expenses-Combined.xml")/ex:Expenses/ex:Expense[5]/ex:id"]

I tried other jSon serializers for XQuery

  • https://github.com/isubiker/mljson
  • https://github.com/marklogic/commons

and they both have problems of returning very complicated json structures instead of the simple array that jQuery's autocomplete widget would take. Could somebody suggest something?

https://github.com/isubiker/mljson


How about:

xquery version "1.0-ml";

let $ids := 
<ids>
  <id>Bank ATM</id>
  <id>PostageShipping</id>
  <id>WebHosting</id>
  <id>ClientParking</id>
</ids>
return xdmp:to-json(fn:data($ids/id))
==>
["Bank ATM", "PostageShipping", "WebHosting", "ClientParking"]

You may want to loop through your collection with a FLWOR, and replace &lt;ids&gt; in the example above with your &lt;ex:Expense&gt;


Good to see that you found a solution, but figured I'd pass along some additional info that would allow you to use the mljson library if you'd like.

The primary goal of the mljson library is to turn MarkLogic into a server for storing and searching over JSON. That said, it can be used to generate JSON via XQuery. However, because the library is built to consume the XML that the library itself generates, it requires the XML to be in a specific format in order to convert it to JSON. To generate your array, here's what the XML would need to look like:

<json type="array">
    <item>Bank ATM</item>
    <item>PostageShipping</item>
    <item>WebHosting</item>
    <item>ClientParking</item>
</json>

You'd just pass that XML to the json:xmlToJSON() function and out comes your JSON array.

As for the other JSON library that you found (the one under commons), it's not quite as flexible and doesn't fit your needs as well (although it is a bit more forgiving with it's input format).

0

精彩评论

暂无评论...
验证码 换一张
取 消