【问题标题】:Casting a field to XML, querying it returns NULL records将字段转换为 XML,查询它返回 NULL 记录
【发布时间】:2013-09-18 09:40:01
【问题描述】:

我的表中有一个字段是 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

谢谢,马可

【问题讨论】:

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


    【解决方案1】:
    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 表达式中为元素添加前缀。

    【讨论】:

      【解决方案2】:

      我做过一次,但没有使用命名空间

      我的输入是 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);
      

      【讨论】:

      • 感谢您的努力 Chrisopher,Mikael 的回答完全符合要求!
      猜你喜欢
      • 2021-04-08
      • 2021-02-19
      • 1970-01-01
      • 1970-01-01
      • 2011-04-15
      • 2018-11-09
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多