【问题标题】:XQuery/XPath: Element id gets lost in outputXQuery/XPath:元素 id 在输出中丢失
【发布时间】:2020-09-02 09:47:54
【问题描述】:

我将举一个例子来更好地解释自己:

我想展示所有售价值至少比标价低 20% 的产品,并且必须是相同的产品才能比较价格。最后显示商店 ID 及其产品 ean、售价和上市价格。

<company>
    <products>
        <product ean="111">
            <listingPrice>100</listingPrice>
        </product>
        <product ean="222">
            <listingPrice>500</listPrice>
        </product>
        <product ean="333">
            <listingPrice>1000</listingPrice>
        </product>
    </products>
    <shops>
       <shop id="1">
        <collection>
         <product>
           <ean>111</ean>
           <sellingPrice>90</sellingPrice>      
         </product>
        </collection>
       </shop>

       <shop id="2">
         <collection>
         <product>
           <ean>888</ean>
           <sellingPrice>10</sellingPrice>      
        </product>
         <product>
           <ean>222</ean>
           <sellingPrice>300</sellingPrice>     
        </product>
       </collection>
       </shop>

        <shop id="3">
         <collection>
         <product>
           <ean>222</ean>
           <sellingPrice>600</sellingPrice>     
        </product>
        </collection>
       </shop>


       <shop id="4">
         <collection>
         <product>
           <ean>111</ean>
           <sellingPrice>20</sellingPrice>      
        </product>

         <product>
           <ean>333</ean>
           <sellingPrice>140</sellingPrice>     
        </product>
        </collection>

       </shop>
   </shops>
</company>

XQuery:

declare variable $factor as xs:decimal external := 0.8;

declare function local:listing-price($product as element(product)) as xs:decimal?
{
    root($product)/company/products/product[@ean = $product/ean]/listingPrice
};

declare function local:check-price($product as element(product), $factor as xs:decimal) as xs:boolean
{
    $product/sellingPrice < local:listing-price($product) * $factor
};


doc('shop.xml')/company/shops/shop/collection[product[local:check-price(., $factor)]] 
!
<shop id="{@id}">{
    product[local:check-price(., $factor)] 
    ! 
    <product ean="{ean}" sellingPrice="{sellingPrice}" listingPrice="{local:listing-price(.)}" />
}</shop>

解决办法是:

<shop id="2">
     <product ean="222" sellingPrice="300" listingPrice="500"/>
  </shop>

  <shop id="4">
     <product ean="111" sellingPrice="20" listingPrice="100"/>
     <product ean="333" sellingPrice="140" listingPrice="1000"/>
  </shop>

但我明白了:

<shop id="">
     <product ean="222" sellingPrice="300" listingPrice="500"/>
  </shop>

  <shop id="">
     <product ean="111" sellingPrice="20" listingPrice="100"/>
     <product ean="333" sellingPrice="140" listingPrice="1000"/>
  </shop>

我不知道为什么我失去了 shop id 值。

【问题讨论】:

    标签: xml xslt xpath xsd xquery


    【解决方案1】:

    我认为在表达式doc('shop.xml')/company/shops/shop/collection[product[local:check-price(., $factor)]] 中,您希望将collection 移动到谓词中:doc('shop.xml')/company/shops/shop[collection/product[local:check-price(., $factor)]]。这样,映射运算符!shop 元素起作用,然后构造新结果&lt;shop id="{@id}"&gt; 的表达式将选择idid 属性。

    在您当前的尝试中,collection 元素是 ! 运算符右侧的上下文项,它没有 id 属性。

    当然,一旦您将外部表达式的上下文项更改为shop 元素,您必须将内部表达式从product[local:check-price(., $factor)] 调整为collection/product[local:check-price(., $factor)]

    【讨论】:

    • 好的,谢谢,我现在得到了 id,但我在输出中丢失了产品 ean、sellingPrice 和 listingPrice。 xqueryfiddle.liberty-development.net/nc4P6y7/5
    • @Lyaso,看编辑,如果外部上下文项发生了变化,内部表达式也需要调整。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-24
    • 2020-08-31
    • 1970-01-01
    • 1970-01-01
    • 2019-12-04
    • 1970-01-01
    相关资源
    最近更新 更多