XQuery/XPath:元素 ID 在输出中丢失

XQuery/XPath: Element id gets lost in output

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

我想展示所有售价至少比标价低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>

我不知道为什么我丢失了商店 ID 值。

我认为在表达式 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 元素,然后构造新结果 <shop id="{@id}"> 的表达式将 select id [=14] 的属性=].

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

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