0

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

谢谢,马可

4

2 回答 2

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 表达式中为元素添加前缀。

于 2013-09-18T09:49:47.277 回答
0

我做过一次,但没有使用 namespaces

我的输入是 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);
于 2013-09-18T09:51:02.263 回答