简体   繁体   English

Marklogic中是否存在直接的XQuery,它使用简单的XML序列生成JSON输出,以便与JQuery autocompete一起使用

[英]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. 我正在尝试获取jQuery 自动完成小部件以从存储在Marklogic XML服务器中的XML源获取数据。

The XML data is very simple looks like this: XML数据非常简单,如下所示:

<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 Markllogic服务器确实有一个函数xdmp:to-json应该做类似的事情,但是这样使用时

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 我为XQuery尝试了其他jSon序列化程序

and they both have problems of returning very complicated json structures instead of the simple array that jQuery's autocomplete widget would take. 并且他们都有返回非常复杂的json结构的问题,而不是jQuery的自动完成小部件将采用的简单数组。 Could somebody suggest something? 有人会提出什么建议吗?

https://github.com/isubiker/mljson 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; 您可能希望使用FLWOR遍历集合,并替换&lt;ids&gt; in the example above with your &lt;ex:Expense&gt; 在上面的示例中使用&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. 很高兴看到你找到了一个解决方案,但我认为我会传递一些额外的信息,如果你愿意,你可以使用mljson库。

The primary goal of the mljson library is to turn MarkLogic into a server for storing and searching over JSON. mljson库的主要目标是将MarkLogic转换为用于存储和搜索JSON的服务器。 That said, it can be used to generate JSON via XQuery. 也就是说,它可以用于通过XQuery生成JSON。 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. 但是,由于构建库是为了使用库本身生成的XML,因此需要XML采用特定格式才能将其转换为JSON。 To generate your array, here's what the XML would need to look like: 要生成数组,以下是XML需要的样子:

<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. 您只需将该XML传递给json:xmlToJSON()函数,然后输出您的JSON数组。

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). 至于你找到的另一个JSON库(公共下面的那个),它不是那么灵活,也不适合你的需求(尽管它对输入格式有点宽容)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM