2013-06-27 281 views
1

我有以下XML遍歷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]實例有關,但無法確定如何返回所有項目。

+0

您是否嘗試刪除'[1]'? – harpo

+0

是的。這會產生以下錯誤:XQuery [value()]:'value()'需要一個單例(或空序列),找到類型爲'xdt:untypedAtomic *'的操作數 – user2516387

回答

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)