循环遍历XML以返回所有节点

时间:2013-06-27 14:20:52

标签: sql xml tsql nodes

我有以下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]实例有关,但无法确定如何返回所有项目。

1 个答案:

答案 0 :(得分: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 )