【问题标题】:How to perform sort on documents based on type selected dynamically and then sort by amount如何根据动态选择的类型对文档进行排序,然后按数量排序
【发布时间】:2019-03-13 16:42:36
【问题描述】:

要求:

  1. 采购订单搜索结果返回列表
  2. 采购订单可能有也可能没有订单项(出于示例目的)
  3. 一个采购订单可以有数千个订单项
  4. 一种类型只有 1 个订单项(采购订单中没有重复项)
  5. 对于排序:
    1. 我们会知道选择了哪种类型进行排序,并且只希望根据该特定类型的数量进行排序
    2. 只会选择一种类型进行排序
    3. 其他用例可以对不同的元素进行排序

示例文档:

<purchase-order>
  <line-items>
    <line-item type=lamp  amount=3500 </line-item>
    <line-item type=couch  amount=50000 </line-item>
    <line-item type=chair  amount=40000 </line-item>
  </line-items>
  <other-stuff></other-stuff>
</purchase-order>

【问题讨论】:

  • 如果您能分享代码,展示您迄今为止所尝试的内容,我们将不胜感激。最好的学习方法,是从自己的错误中学习。注意:您的 xml 示例格式不正确..

标签: sorting marklogic


【解决方案1】:

首先,您的 xml 格式不正确: 我用过下面的xml和查询:

     let $input :=
          <purchase-order>
           <line-items>
            <line-item type="lamp"  amount="52"/>
            <line-item type="lamp"  amount="40000"/>
            <line-item type="lamp"  amount="3500"/>
            <line-item type="lamp"  amount="3500"/>
            <line-item type="couch"  amount="50000"/>
            <line-item type="chair"  amount="110000"/>
            <line-item type="chair"  amount="5000"/>
            <line-item type="chair"  amount="80000"/>
          </line-items>
          <other-stuff></other-stuff>
        </purchase-order>

         let $purchase-order := 
          for $each-type in distinct-values($input/line-items/line-item/@type)
          let $seq := $input/line-items/line-item[@type = $each-type]
          order by $each-type
          return 
             <line-items type="{$each-type}">
             {
             for $one in $seq
             order by xs:integer($one/@amount)
             return $one
             }
             </line-items>
             return <purchase-order>{$purchase-order}</purchase-order>

查询生成的输出:

              <purchase-order>
                <line-items type="chair">
                <line-item amount="5000" type="chair"/>
                <line-item amount="80000" type="chair"/>
                <line-item amount="110000" type="chair"/>
                </line-items>
                <line-items type="couch">
                <line-item amount="50000" type="couch"/>
                </line-items>
                <line-items type="lamp">
                <line-item amount="52" type="lamp"/>
                <line-item amount="3500" type="lamp"/>
                <line-item amount="3500" type="lamp"/>
                <line-item amount="40000" type="lamp"/>
                </line-items>
              </purchase-order>

希望您的要求能完整填写此查询。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-12
  • 1970-01-01
  • 2021-11-21
  • 2021-06-13
  • 1970-01-01
  • 1970-01-01
  • 2017-03-17
相关资源
最近更新 更多