English 中文(简体)
Is there straightforward XQuery in Marklogic that produces JSON output out of simple XML sequence to be used with JQuery autocompete
原标题:

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 when 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

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).





相关问题
JQuery/MVC Search Issue

I have inherited a piece of work where the entry screen shows a summary of 20 calculated variables. E.g. Var A (250), Var B (79). Clicking on any of these links takes the user to a view with a ...

jQuery quicksearch plug-in tinkering with JSON

I ve implemented the quicksearch plugin by Rik Lomas and I love it for an application in a custom CMS I m building. I was wondering though, since I m going to have a bizillion items in the table if ...

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

PHP json_decode question

i m trying to use json_decode to combine a few json objects and then re-encode it. my json looks like: { "core": { "segment": [ { "id": 7, "...

Converting JSON data to Java object

I want to be able to access properties from a JSON string within my Java action method. The string is available by simply saying myJsonString = object.getJson(). Below is an example of what the string ...

热门标签