2017-10-15 8 views
0

私はXMLNAMESPACES節の主な目的がネームスペースを宣言し、さらにそれを使用することを理解しています。XMLNAMESPACESエイリアスを表示しない方法

私は次のように出力

<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> 

を生成そして今、解決策を見つけることを望んでばかな質問は、次のコード

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: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

関連する問題