通过xquery从数据库的XML列中选择

问题描述 投票:0回答:1

我在SQL Server中使用Northwind数据库。我做了这样的一张新桌子:

WITH XMLNAMESPACES(DEFAULT 'some http://')
SELECT p.ProductID, CAST((SELECT p.* FOR XML PATH('Product')) AS XML) AS Data
INTO XmlProducts
FROM Products AS p

现在我必须从Data列中取出该表,像ProductIDProductNameUnitPriceDiscontinued之类的信息(我只需要选择这些记录,其中Discontinued=1 )。比我不得不将其设置为新的额外原始格式。它需要看起来像这样:

<ExtraRaw>
 <Product>
  <ProductID xmlms="some http://">1</ProductID>
  <ProductName xmlms="some http://">Name1</ProductName>
  <UnitPrice xmlms="some http://">UnitPrice1</UnitPrice>
  <Discontinued> xmlms="some http://">1</Discontinued>
 </Product>
 <Product>
  <ProductID xmlms="some http://">2</ProductID>
  <ProductName xmlms="some http://">Name2</ProductName>
  <UnitPrice xmlms="some http://">UnitPrice2</UnitPrice>
  <Discontinued> xmlms="some http://">2</Discontinued>
 </Product>
 ... and so on
</ExtraRaw>

我尝试了以下代码:

DECLARE @t xml
SET @t = (SELECT Data FROM XmlProducts FOR xml AUTO)
SELECT @t.query('for $x in //Product where $x/Discontinued=1 
return <Product>{($x/ProductID, $x/ProductName, $x/UnitPrice, $x/Discontinued)}</Product>')
FOR xml RAW('DiscontinuedProducts')
GO

但是它给了我这样的结果:

<ExtraRaw></ExtraRaw>

当我将此行添加到代码中时:WITH XMLNAMESPACES(DEFAULT 'some http://')

DECLARE @t xml
SET @t = (SELECT Data FROM XmlProducts FOR xml AUTO)
WITH XMLNAMESPACES(DEFAULT 'some http://')
SELECT @t.query('for $x in //Product where $x/Discontinued=1 
return <Product>{($x/ProductID, $x/ProductName, $x/UnitPrice, $x/Discontinued)}</Product>')
FOR xml RAW('DiscontinuedProducts')
GO

它给我一个更好的结果,接近我想要的:

<ExtraRaw xmlns="some http://">
  <p1:Product xmlns:p1="some http://" xmlns="">
    <p1:ProductID>5</p1:ProductID>
    <p1:ProductName>Chef Anton's Gumbo Mix</p1:ProductName>
    <p1:UnitPrice>21.3500</p1:UnitPrice>
    <p1:Discontinued>1</p1:Discontinued>
  </p1:Product>
  <p2:Product xmlns:p2="some http://" xmlns="">
    <p2:ProductID>9</p2:ProductID>
    <p2:ProductName>Mishi Kobe Niku</p2:ProductName>
    <p2:UnitPrice>97.0000</p2:UnitPrice>
    <p2:Discontinued>1</p2:Discontinued>
  </p2:Product>
</ExtraRaw>

但是在ExtraRaw中是一个xmlns,我不要这个。在产品中也有两个xmlns,但是我什么都不想要,在ProductID,ProductName,UnitPrice和Discontinued中,我需要一个xmlns。

SELECT @t看起来像这样:

<XmlProducts>
  <Data>
    <Product xmlns="some http://">
      <ProductID>1</ProductID>
      <ProductName>Chai</ProductName>
      <SupplierID>1</SupplierID>
      <CategoryID>1</CategoryID>
      <QuantityPerUnit>10 boxes x 20 bags</QuantityPerUnit>
      <UnitPrice>19.8000</UnitPrice>
      <UnitsInStock>39</UnitsInStock>
      <UnitsOnOrder>0</UnitsOnOrder>
      <ReorderLevel>10</ReorderLevel>
      <Discontinued>0</Discontinued>
    </Product>
  </Data>
</XmlProducts>
<XmlProducts>
  <Data>
    <Product xmlns="some http://">
      <ProductID>2</ProductID>
      <ProductName>Chang</ProductName>
      <SupplierID>1</SupplierID>
      <CategoryID>1</CategoryID>
... and so on

我该如何解决?

sql-server xml tsql xquery
1个回答
0
投票

如果要删除名称空间,则需要重新声明元素并通过data()提取原始值,例如:

© www.soinside.com 2019 - 2024. All rights reserved.