将字段转换为XML,查询它返回NULL记录

时间:2013-09-18 09:40:01

标签: sql-server xml sql-server-2012

我的表上有一个nvarchar(max)字段,包含XML文档。不要问为什么它是nvarchar(max)而不是XML,因为我不知道它。 顺便说一句,这是一个示例XML的提取:

<?xml version="1.0" encoding="utf-16"?>
<ItemType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <AutoPay xmlns="urn:ebay:apis:eBLBaseComponents">true</AutoPay>
  <Country xmlns="urn:ebay:apis:eBLBaseComponents">IT</Country>
  <Currency xmlns="urn:ebay:apis:eBLBaseComponents">EUR</Currency>
  <HitCounter xmlns="urn:ebay:apis:eBLBaseComponents">BasicStyle</HitCounter>
  <ListingDuration xmlns="urn:ebay:apis:eBLBaseComponents">GTC</ListingDuration>
  <ListingType xmlns="urn:ebay:apis:eBLBaseComponents">FixedPriceItem</ListingType>
  <Location xmlns="urn:ebay:apis:eBLBaseComponents">Italy</Location>
  <PaymentMethods xmlns="urn:ebay:apis:eBLBaseComponents">PayPal</PaymentMethods>
  <PayPalEmailAddress xmlns="urn:ebay:apis:eBLBaseComponents">email@paypal.com</PayPalEmailAddress>
  <PrimaryCategory xmlns="urn:ebay:apis:eBLBaseComponents">
    <CategoryID>137084</CategoryID>
  </PrimaryCategory>
  <ShippingDetails xmlns="urn:ebay:apis:eBLBaseComponents">
    <ShippingServiceOptions>
      <ShippingService>StandardShippingFromOutsideUS</ShippingService>
      <ShippingServiceCost currencyID="EUR">0</ShippingServiceCost>
      <ShippingServiceAdditionalCost currencyID="EUR">0</ShippingServiceAdditionalCost>
      <FreeShipping>true</FreeShipping>
    </ShippingServiceOptions>
    <InternationalShippingServiceOption>
      <ShippingService>StandardInternational</ShippingService>
      <ShippingServiceCost currencyID="EUR">0</ShippingServiceCost>
      <ShippingServiceAdditionalCost currencyID="EUR">0</ShippingServiceAdditionalCost>
      <ShippingServicePriority>1</ShippingServicePriority>
      <ShipToLocation>Americas</ShipToLocation>
      <ShipToLocation>Europe</ShipToLocation>
    </InternationalShippingServiceOption>
    <ShippingType>Flat</ShippingType>
    <InsuranceDetails>
      <InsuranceFee currencyID="EUR">0</InsuranceFee>
      <InsuranceOption>NotOffered</InsuranceOption>
    </InsuranceDetails>
    <InternationalInsuranceDetails>
      <InsuranceFee currencyID="EUR">0</InsuranceFee>
      <InsuranceOption>NotOffered</InsuranceOption>
    </InternationalInsuranceDetails>
  </ShippingDetails>
  <Site xmlns="urn:ebay:apis:eBLBaseComponents">US</Site>
  <Storefront xmlns="urn:ebay:apis:eBLBaseComponents">
    <StoreCategoryID>2947535016</StoreCategoryID>
    <StoreCategory2ID>0</StoreCategory2ID>
  </Storefront>
  <DispatchTimeMax xmlns="urn:ebay:apis:eBLBaseComponents">4</DispatchTimeMax>
  <ReturnPolicy xmlns="urn:ebay:apis:eBLBaseComponents">
    <ReturnsAcceptedOption>ReturnsAccepted</ReturnsAcceptedOption>
    <Description>Accepted</Description>
    <ShippingCostPaidByOption>Buyer</ShippingCostPaidByOption>
  </ReturnPolicy>
  <ConditionID xmlns="urn:ebay:apis:eBLBaseComponents">1000</ConditionID>
</ItemType>

我很想查询该字段上的表格,例如提取CategoryID字段。 我尝试了所有我知道的东西,比如转换为ntext,删除utf-16,用utf-8替换它,添加命名空间和类似的东西,但结果总是一个NULL记录。

以下是我尝试的其中一个问题:

;WITH XMLNAMESPACES('urn:ebay:apis:eBLBaseComponents' AS ns, 
'http://www.w3.org/2001/XMLSchema-instance' as xsi, 
'http://www.w3.org/2001/XMLSchema' as xsd)
select CategoryVal = CONVERT(xml, [Template]).value('(/ItemType/PrimaryCategory/CategoryID)[1]', 'nvarchar(max)') FROM Templates where ID = 1

谢谢,Marco

2 个答案:

答案 0 :(得分:2)

with xmlnamespaces('urn:ebay:apis:eBLBaseComponents' as n)
select cast(Template as xml).value('(/ItemType/n:PrimaryCategory/n:CategoryID)[1]', 'nvarchar(max)')
from Templates 
where ID = 1

您需要在xpath表达式中为元素添加前缀。

答案 1 :(得分:0)

我已经完成了一次,但没有使用命名空间

我的输入为varchar(max)(nvarchar也应该起作用)

@Text AS varchar(MAX)

然后我使用了XML类型变量,转换就像这样简单:

DECLARE @XML XML
SELECT @XML = @Text

要查询您的CategoryID值,您可以使用:

SELECT itemtype.item.value('(/ItemType/PrimaryCategory/CategoryID)[1]', 'nvarchar(max)')    
FROM @XML.nodes('/ItemType') AS itemtype(item);