2017-10-15 41 views
0

我明白XMLNAMESPACES子句的主要目的是聲明命名空間並進一步使用它。如何不顯示XMLNAMESPACES別名

我有以下代碼

DECLARE @XMLFINAL VARCHAR(MAX) 
SET @XMLFINAL='' 
DECLARE @NUMBER NVARCHAR(100) 
DECLARE @XML VARCHAR(MAX) 

DECLARE Records CURSOR FAST_FORWARD FOR 

SELECT TOP 1 GID FROM PurchasesDocumentsLines 

OPEN Records 
FETCH NEXT FROM Records INTO @NUMBER 
WHILE @@FETCH_STATUS = 0 
BEGIN 
SET @XML=''  


;WITH XMLNAMESPACES ('OrderedProductSection' as q17) 

SELECT @XML= (

SELECT 
ProductCode as 'q17:ProductCode', OrderedQuantity 'q17:OrderedQuantity' 

FROM PurchasesDocumentsLines WHERE [email protected] 
FOR 
XML RAW('q17:OrderedProductSection'), ELEMENTS 
) 

產生以下輸出

<q17:OrderedProducts xmlns:q17="http://ITrack.Transmission/2011/02/25/Objects"> 
<q17:OrderedProductSection xmlns:q17="OrderedProductSection"> 
<q17:ProductCode>19A0010000</q17:ProductCode> 
<q17:OrderedQuantity>2</q17:OrderedQuantity> 
</q17:OrderedProductSection> 
<q17:OrderedProductSection xmlns:q17="OrderedProductSection"> 
<q17:ProductCode>1G5Y010000</q17:ProductCode> 
<q17:OrderedQuantity>3</q17:OrderedQuantity> 
</q17:OrderedProductSection> 
<q17:OrderedProductSection xmlns:q17="OrderedProductSection"> 
<q17:ProductCode>1G5Y020000</q17:ProductCode> 
<q17:OrderedQuantity>4</q17:OrderedQuantity> 
</q17:OrderedProductSection> 
<q17:OrderedProductSection xmlns:q17="OrderedProductSection"> 
<q17:ProductCode>1G5Y030000</q17:ProductCode> 
<q17:OrderedQuantity>5</q17:OrderedQuantity> 
</q17:OrderedProductSection> 
<q17:OrderedProductSection xmlns:q17="OrderedProductSection"> 
<q17:ProductCode>1G5Y040000</q17:ProductCode> 
<q17:OrderedQuantity>6</q17:OrderedQuantity> 
</q17:OrderedProductSection> 
</q17:OrderedProducts> 

而現在,愚蠢的問題,希望能夠找到一個解決方案:有沒有什麼辦法,對於<q17:OrderedProductSection xmlns:q17="OrderedProductSection">標籤,不顯示xmlns:q17="OrderedProductSection"?因爲擁有一個空的URI是不允許的。由於

LE

REPLACE功能實現它。

編輯

<q17:OrderedProducts xmlns:q17="http://ITrack.Transmission/2011/02/25/Objects"> 
    <q17:ProductCode>19A0010000</q17:ProductCode> 
    <q17:OrderedQuantity>2</q17:OrderedQuantity> 
    <q17:ProductCode>1G5Y010000</q17:ProductCode> 
    <q17:OrderedQuantity>3</q17:OrderedQuantity> 
</q17:OrderedProducts> 

回答

0

不要在CURSOR做到這一點!他們是壞的和邪惡的,直接來自程序思維的地獄,並由意大利麪條代碼的魔鬼發明...

每個單獨的電話FOR XML將引入命名空間。這是設計!

這可以更快完成,更清潔,更好地與一組爲基礎的方法:

DECLARE @table TABLE(ProductCode VARCHAR(100),OrderedQuantity INT); 
INSERT INTO @table VALUES 
('19A0010000',2) 
,('1G5Y010000',3); 

WITH XMLNAMESPACES('http://ITrack.Transmission/2011/02/25/Objects' AS q17) 
SELECT ProductCode AS [q17:ProductCode] 
     ,OrderedQuantity AS [q17:OrderedQuantity] 
FROM @table AS t 
FOR XML PATH('q17:OrderedProductSection'),ROOT('q17:OrderedProducts'); 

將返回該

<q17:OrderedProducts xmlns:q17="http://ITrack.Transmission/2011/02/25/Objects"> 
    <q17:OrderedProductSection> 
    <q17:ProductCode>19A0010000</q17:ProductCode> 
    <q17:OrderedQuantity>2</q17:OrderedQuantity> 
    </q17:OrderedProductSection> 
    <q17:OrderedProductSection> 
    <q17:ProductCode>1G5Y010000</q17:ProductCode> 
    <q17:OrderedQuantity>3</q17:OrderedQuantity> 
    </q17:OrderedProductSection> 
</q17:OrderedProducts> 
+0

有什麼辦法避免'root',使代碼看起來像我的編輯?謝謝 – cdrrrrr

+0

@cdrrrrr哪裏有? – Shnugo

+1

@cdrrrrr如果你確實需要它,那很容易(但我不會建議這個結構!)。只需將最後一行代碼替換爲FOR XML PATH(''),ROOT('q17:OrderedProducts');'。空的'PATH('')'將返回,沒有行包裝節點 – Shnugo