1

我有以下xml

<ListOrderItemsResult>
  <OrderItems>
    <OrderItem>
      <OrderItemId>01691605007219</OrderItemId>
      <GiftWrapPrice>
        <Amount>0.00</Amount>
        <CurrencyCode>GBP</CurrencyCode>
      </GiftWrapPrice>
      <QuantityOrdered>1</QuantityOrdered>
      <GiftWrapTax>
        <Amount>0.00</Amount>
        <CurrencyCode>GBP</CurrencyCode>
      </GiftWrapTax>
      <SellerSKU>WB0812005-2</SellerSKU>
      <Title>Solid Oak Silk Lined Wooden Gift Box for Whisky, Wine and Champagne (2 Pack)</Title>
      <ShippingTax>
        <Amount>0.00</Amount>
        <CurrencyCode>GBP</CurrencyCode>
      </ShippingTax>
      <ShippingPrice>
        <Amount>0.99</Amount>
        <CurrencyCode>GBP</CurrencyCode>
      </ShippingPrice>
      <ItemTax>
        <Amount>0.00</Amount>
        <CurrencyCode>GBP</CurrencyCode>
      </ItemTax>
      <ItemPrice>
        <Amount>43.99</Amount>
        <CurrencyCode>GBP</CurrencyCode>
      </ItemPrice>
      <PromotionDiscount>
        <Amount>0.00</Amount>
        <CurrencyCode>GBP</CurrencyCode>
      </PromotionDiscount>
      <ASIN>B00911O3IY</ASIN>
      <ConditionId>New</ConditionId>
      <QuantityShipped>1</QuantityShipped>
      <ConditionSubtypeId>New</ConditionSubtypeId>
      <ConditionNote>Free Postage and Packaging / Brand New / Original Packaging / Dispatched from UK Warehouse by Royal Mail or Parcelforce</ConditionNote>
      <ShippingDiscount>
        <Amount>0.00</Amount>
        <CurrencyCode>GBP</CurrencyCode>
      </ShippingDiscount>
    </OrderItem>
    <OrderItem>
      <OrderItemId>14086955545403</OrderItemId>
      <GiftWrapPrice>
        <Amount>0.00</Amount>
        <CurrencyCode>GBP</CurrencyCode>
      </GiftWrapPrice>
      <QuantityOrdered>1</QuantityOrdered>
      <GiftWrapTax>
        <Amount>0.00</Amount>
        <CurrencyCode>GBP</CurrencyCode>
      </GiftWrapTax>
      <SellerSKU>WB0812005-2</SellerSKU>
      <Title>Solid Oak Silk Lined Wooden Gift Box for Whisky, Wine and Champagne (2 Pack)</Title>
      <ShippingTax>
        <Amount>0.00</Amount>
        <CurrencyCode>GBP</CurrencyCode>
      </ShippingTax>
      <ShippingPrice>
        <Amount>1.00</Amount>
        <CurrencyCode>GBP</CurrencyCode>
      </ShippingPrice>
      <ItemTax>
        <Amount>0.00</Amount>
        <CurrencyCode>GBP</CurrencyCode>
      </ItemTax>
      <ItemPrice>
        <Amount>43.99</Amount>
        <CurrencyCode>GBP</CurrencyCode>
      </ItemPrice>
      <PromotionDiscount>
        <Amount>0.00</Amount>
        <CurrencyCode>GBP</CurrencyCode>
      </PromotionDiscount>
      <ASIN>B00911O3IY</ASIN>
      <ConditionId>New</ConditionId>
      <QuantityShipped>1</QuantityShipped>
      <ConditionSubtypeId>New</ConditionSubtypeId>
      <ConditionNote>Free Postage and Packaging / Brand New / Original Packaging / Dispatched from UK Warehouse by Royal Mail or Parcelforce</ConditionNote>
      <ShippingDiscount>
        <Amount>0.00</Amount>
        <CurrencyCode>GBP</CurrencyCode>
      </ShippingDiscount>
    </OrderItem>
    <OrderItem>
      <OrderItemId>15068544085995</OrderItemId>
      <GiftWrapPrice>
        <Amount>0.00</Amount>
        <CurrencyCode>GBP</CurrencyCode>
      </GiftWrapPrice>
      <QuantityOrdered>1</QuantityOrdered>
      <GiftWrapTax>
        <Amount>0.00</Amount>
        <CurrencyCode>GBP</CurrencyCode>
      </GiftWrapTax>
      <SellerSKU>WB0812005-2</SellerSKU>
      <Title>Solid Oak Silk Lined Wooden Gift Box for Whisky, Wine and Champagne (2 Pack)</Title>
      <ShippingTax>
        <Amount>0.00</Amount>
        <CurrencyCode>GBP</CurrencyCode>
      </ShippingTax>
      <ShippingPrice>
        <Amount>1.00</Amount>
        <CurrencyCode>GBP</CurrencyCode>
      </ShippingPrice>
      <ItemTax>
        <Amount>0.00</Amount>
        <CurrencyCode>GBP</CurrencyCode>
      </ItemTax>
      <ItemPrice>
        <Amount>43.99</Amount>
        <CurrencyCode>GBP</CurrencyCode>
      </ItemPrice>
      <PromotionDiscount>
        <Amount>0.00</Amount>
        <CurrencyCode>GBP</CurrencyCode>
      </PromotionDiscount>
      <ASIN>B00911O3IY</ASIN>
      <ConditionId>New</ConditionId>
      <QuantityShipped>1</QuantityShipped>
      <ConditionSubtypeId>New</ConditionSubtypeId>
      <ConditionNote>Free Postage and Packaging / Brand New / Original Packaging / Dispatched from UK Warehouse by Royal Mail or Parcelforce</ConditionNote>
      <ShippingDiscount>
        <Amount>0.00</Amount>
        <CurrencyCode>GBP</CurrencyCode>
      </ShippingDiscount>
    </OrderItem>
</ListOrderItemsResult>

我想选择每个并提出以下代码,该代码有效但仅检索第一项和数量

SELECT  doc.value('(OrderItems/OrderItem/OrderItemId)[1]','nvarchar(255)') AS 'OrderItemId' ,doc.value('(OrderItems/OrderItem/QuantityOrdered)[1]', 'int') AS 'QuantityOrdered' 
FROM    @xml.nodes('/ListOrderItemsResult') AS ref ( doc )

任何人都可以提供一个解决方案来返回所有项目,即 XML 中的 3 个项目。我知道它与 [1] 实例有关,但无法弄清楚如何返回所有项目。

4

1 回答 1

0
SELECT  doc.value('./OrderItemId[1]/text()[1]','nvarchar(255)') AS 'OrderItemId',
        doc.value('./QuantityOrdered[1]/text()[1]', 'int') AS 'QuantityOrdered' 
FROM    @xml.nodes('/ListOrderItemsResult/OrderItems/*') AS ref ( doc )
于 2013-07-10T01:48:22.023 回答