【问题标题】:I need to get distinct, sorted products in a list, only showing the products with the lowest price我需要在列表中获得不同的排序产品,只显示价格最低的产品
【发布时间】:2014-10-07 22:18:09
【问题描述】:

我正在努力解决 XQuery 问题。我有一个 XML 产品列表,其中包含来自 2 个不同产品目录系统的产品(由于并购)。一些产品是重复的(每个系统中有 1 个)。重复的产品名称相同,但价格不同。

我想返回显示最便宜产品价格的不同产品列表。例如,考虑这个输入 XML

<?xml version="1.0" encoding="UTF-8"?>
<ns1:ProductList xmlns:ns1="http://www.mycorp.com/bus/prod/" xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
  <ns1:Product>
    <ns1:Id>2</ns1:Id>
    <ns1:Name>Ace Ski Boot</ns1:Name>
    <ns1:Description>An intermediate ski boot</ns1:Description>
    <ns1:Price>109.99</ns1:Price>
    <ns1:Warehouse>CZE_PRAGUE</ns1:Warehouse>
    <ns1:Icon_url/>
    <ns1:Latitude>50.07554</ns1:Latitude>
    <ns1:Longitude>14.4378</ns1:Longitude>
  </ns1:Product>
  <ns1:Product>
    <ns1:Id>SUM10012</ns1:Id>
    <ns1:Name>Ace Ski Boot</ns1:Name>
    <ns1:Description>Intermediate ski boot</ns1:Description>
    <ns1:Price>200.0</ns1:Price>
    <ns1:Warehouse>Seattle</ns1:Warehouse>
    <ns1:Icon_url/>
    <ns1:Latitude>47.60621</ns1:Latitude>
    <ns1:Longitude>-122.33207</ns1:Longitude>
  </ns1:Product>
  <ns1:Product>
    <ns1:Id>4</ns1:Id>
    <ns1:Name>Ace Ski Pole</ns1:Name>
    <ns1:Description>A ski pole for skiers with an intermediate skill level</ns1:Description>
    <ns1:Price>29.99</ns1:Price>
    <ns1:Warehouse>FRA_PARIS</ns1:Warehouse>
    <ns1:Icon_url/>
    <ns1:Latitude>48.856613</ns1:Latitude>
    <ns1:Longitude>2.352222</ns1:Longitude>
  </ns1:Product>
  <ns1:Product>
    <ns1:Id>SUM10022</ns1:Id>
    <ns1:Name>Ace Ski Pole</ns1:Name>
    <ns1:Description>Intermediate ski pole</ns1:Description>
    <ns1:Price>21.950000762939453</ns1:Price>
    <ns1:Warehouse>Seattle</ns1:Warehouse>
    <ns1:Icon_url/>
    <ns1:Latitude>47.60621</ns1:Latitude>
    <ns1:Longitude>-122.33207</ns1:Longitude>
  </ns1:Product>

它显示了 2 个重复的产品。我希望返回以下 XML:

<?xml version="1.0" encoding="UTF-8"?>
<ns1:ProductList xmlns:ns1="http://www.mycorp.com/bus/prod/" xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
  <ns1:Product>
    <ns1:Id>2</ns1:Id>
    <ns1:Name>Ace Ski Boot</ns1:Name>
    <ns1:Description>An intermediate ski boot</ns1:Description>
    <ns1:Price>109.99</ns1:Price>
    <ns1:Warehouse>CZE_PRAGUE</ns1:Warehouse>
    <ns1:Icon_url/>
    <ns1:Latitude>50.07554</ns1:Latitude>
    <ns1:Longitude>14.4378</ns1:Longitude>
  </ns1:Product>
  <ns1:Product>
    <ns1:Id>SUM10022</ns1:Id>
    <ns1:Name>Ace Ski Pole</ns1:Name>
    <ns1:Description>Intermediate ski pole</ns1:Description>
    <ns1:Price>21.950000762939453</ns1:Price>
    <ns1:Warehouse>Seattle</ns1:Warehouse>
    <ns1:Icon_url/>
    <ns1:Latitude>47.60621</ns1:Latitude>
    <ns1:Longitude>-122.33207</ns1:Longitude>
  </ns1:Product>

我见过 XQuery 返回不同的值,但不是您需要选择子元素 (ns1:Price) 的最低值的地方。谁能指出我正确的方向?非常感谢!

【问题讨论】:

    标签: xquery


    【解决方案1】:

    如果您可以使用 XQuery 3.0,那么您可以使用 group by 表达式。例如:

    xquery version "3.0";
    
    declare namespace ns1 = "http://www.mycorp.com/bus/prod/";
    
    for $product in //ns1:Product
    let $product-name := $product/ns1:Name
    group by $product-name
    return
        <ns1:Product>
        {
            $product[xs:decimal(ns1:Price) eq min($product/xs:decimal(ns1:Price))]
        }
        </ns1:Product>
    

    如果您没有 XQuery 3.0,但有 XQuery 1.0,那么您需要首先使用 fn:distinct-values 标识您希望分组的唯一产品名称,从那里您可以遍历唯一名称并创建组(具有相同名称的产品),然后您可以使用谓词对其进行过滤以确定最便宜的产品。例如:

    xquery version "1.0";
    
    declare namespace ns1 = "http://www.mycorp.com/bus/prod/";
    
    let $unique-product-names := distinct-values(//ns1:Product/ns1:Name)
    return
        for $product-name in $unique-product-names
        let $grouped-products := //ns1:Product[ns1:Name eq $product-name]
        let $group-cheapest-price := min($grouped-products/xs:decimal(ns1:Price))
        return
            $grouped-products[xs:decimal(ns1:Price) eq $group-cheapest-price]
    

    【讨论】:

    • 很遗憾,我目前只能访问 XQuery 1.0。
    • 我刚刚也为您更新了一个 XQuery 1.0 示例
    • 优秀!那成功了。非常感谢您提供了一个漂亮的解决方案。
    猜你喜欢
    • 2015-11-05
    • 2021-12-17
    • 2018-04-26
    • 1970-01-01
    • 1970-01-01
    • 2020-10-16
    • 2020-04-07
    • 1970-01-01
    • 2011-02-25
    相关资源
    最近更新 更多