时间:2017-10-17 18:13:54

标签: sql-server xml xpath soap

我需要生成以下两行作为XML文件的一部分,我设法生成并完美地工作。

<q17:DestinationGLN xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<q17:DestinationCoord xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>

我一直在尝试与XML命名空间相关的所有方法,但我真的不知道该怎么做。如果我无法实现这一目标,我需要开始学习Java以生成XML。

到目前为止,这是我的其余代码:

    DECLARE @RootNode NVARCHAR(MAX)
    DECLARE @SOAPHeader NVARCHAR(MAX)
    DECLARE @SOAPBody NVARCHAR (MAX)
    DECLARE @XMLProductsLines NVARCHAR(MAX)
    DECLARE @XMLSalesOrders NVARCHAR(MAX)
    DECLARE @XMLSalesOrders2 NVARCHAR(MAX)
    DECLARE @XMLHQ NVARCHAR(MAX)
    DECLARE @XMLFinal NVARCHAR(MAX)



    SET @SOAPHeader='<s:Header xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">'
    +'<To s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://10.2.3.92:8201/FlexibleStorage/Transmission?wsdl</To>'
    +'<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://ITrack.Transmission/2011/02/25/Interfaces/TransmissionServer/StoreDocument</Action>'
    +'</s:Header>'

    SET @XMLProductsLines=''

    ;WITH XMLNAMESPACES ('http://ITrack.Transmission/2011/02/25/Objects' as q17)

    SELECT @XMLProductsLines=(SELECT ProductCode 'q17:ProductCode', OrderedQuantity 'q17:OrderedQuantity'
    FROM PurchasesDocumentsLines 
    FOR XML RAW ('q17:OrderedProductSection'), ROOT('q17:OrderedProducts'), ELEMENTS)

    ;WITH XMLNAMESPACES ('http://ITrack.Transmission/2011/02/25/Objects' as q17)

    SELECT @XMLSalesOrders=(SELECT DestCode 'q17:DestinationCode', DestDescr 'q17:DestinationName'
    FROM PurchaseDocumentsHeader
    FOR XML RAW ('q17:DestinationSection'), ROOT('q17:DestinationSection'), ELEMENTS)

    SET @SOAPBody=
    '<soapenv:Body>'
    +'<tns:StoreDocument xmlns:tns="http://ITrack.Transmission/2011/02/25/Interfaces">'
    +'<tns:StoreDocumentMessage>'
    +'<q1:SystemCode xmlns:q1="http://ITrack.Transmission/2011/02/25/Messages">ROM_ENTSOFT_PROD</q1:SystemCode>' -- system code
    +'<q1:TransmissionUID xmlns:q1="http://ITrack.Transmission/2011/02/25/Messages">'+convert(nvarchar(36), newid())+'</q1:TransmissionUID>' -- generated GID
    +'<q1:DocumentSection xmlns:q1="http://ITrack.Transmission/2011/02/25/Messages">' 
    +'<q17:DocumentType xmlns:q17="http://ITrack.Transmission/2011/02/25/Objects">1</q17:DocumentType>' -- fix value (1) 
    +'<q17:DocumentNumber xmlns:q17="http://ITrack.Transmission/2011/02/25/Objects">0000006965</q17:DocumentNumber>' -- ADCode of delivery note
    +'<q17:DocumentUID xsi:nil="true" xmlns:q17="http://ITrack.Transmission/2011/02/25/Objects" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>' -- not used
    +'<q17:CreationDateTimeUTC xmlns:q17="http://ITrack.Transmission/2011/02/25/Objects">'+format((SELECT esdcreated FROM PurchaseDate), 'yyyy-MM-dd HH:mm:ss')+'</q17:CreationDateTimeUTC>' -- timestamp of the order
    +'<q17:DocumentStatus xmlns:q17="http://ITrack.Transmission/2011/02/25/Objects">2</q17:DocumentStatus>' -- 1 = Open; 2 = Shipped;
    +'<q17:LatestGoodIssueDate xmlns:q17="http://ITrack.Transmission/2011/02/25/Objects">'+format(getdate(), 'yyyy-MM-dd HH:mm:ss')+'</q17:LatestGoodIssueDate>' -- the date when the goods are shipped
    +'<q17:Reference xmlns:q17="http://ITrack.Transmission/2011/02/25/Objects">0000013254</q17:Reference>' -- invoice number
    +'<q17:TotalOrderedProducts xsi:nil="true" xmlns:q17="http://ITrack.Transmission/2011/02/25/Objects" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>' -- not used
    +@XMLProductsLines
    +'<q17:RelatedDocumentSection xsi:nil="true" xmlns:q17="http://ITrack.Transmission/2011/02/25/Objects" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>' -- left empty (not used)
    +'<q17:StockOwnerSection xmlns:q17="http://ITrack.Transmission/2011/02/25/Objects">'
    +'<q17:StockOwnerCode>ITDR</q17:StockOwnerCode>'
    +'<q17:StockOwnerName>Imperial Tobacco Romania Distribution</q17:StockOwnerName>' 
    +'<q17:StockOwnerAddress1>140-160 Nicolae Canea Street, EOS Business Park, 1st Floor, 2nd District, Bucharest, ROMANIA</q17:StockOwnerAddress1>' 
    +'<q17:StockOwnerAddress2/>'
    +'<q17:StockOwnerZIP>023076</q17:StockOwnerZIP>' 
    +'<q17:StockOwnerCity>BUCHAREST</q17:StockOwnerCity>' 
    +'<q17:StockOwnerNameRomanized>Imperial Tobacco Romania Distribution</q17:StockOwnerNameRomanized>' 
    +'<q17:StockOwnerAddress1Romanized>140-160 Nicolae Canea Street, EOS Business Park, 1st Floor, 2nd District, Bucharest, ROMANIA</q17:StockOwnerAddress1Romanized>' 
    +'<q17:StockOwnerAddress2Romanized/>' 
    +'<q17:StockOwnerZIPRomanized>023076</q17:StockOwnerZIPRomanized>' 
    +'<q17:StockOwnerCityRomanized>BUCHAREST</q17:StockOwnerCityRomanized>'
    +'<q17:StockOwnerCountry>RO</q17:StockOwnerCountry>'
    +'<q17:StockOwnerGLN xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>' -- left empty (not used)
    +'</q17:StockOwnerSection>'
    +@XMLSalesOrders
    -- here I need the two lines
    --+'<q17:DestinationGLN xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>'
    --+'<q17:DestinationCoord xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>'
    +'<q17:SoldToSection xmlns:q17="http://ITrack.Transmission/2011/02/25/Objects">' -- the headquarter for each POS above
    +'<q17:SoldToCode>13400001</q17:SoldToCode>'
    +'<q17:SoldToName>Табако Трейд ООД</q17:SoldToName>'
    +'<q17:SoldToAddress1>Пловдив бул. Христо Ботев 94</q17:SoldToAddress1>'
    +'<q17:SoldToAddress2 xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>'
    +'<q17:SoldToZIP>4000</q17:SoldToZIP>'
    +'<q17:SoldToCity>Пловдив</q17:SoldToCity>'
    +'<q17:SoldToNameRomanized>Tobacco Trade LTD</q17:SoldToNameRomanized>'
    +'<q17:SoldToAddress1Romanized>137 Bresovsko shose Str.</q17:SoldToAddress1Romanized>'
    +'<q17:SoldToAddress2Romanized xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>'
    +'<q17:SoldToZIPRomanized>4000</q17:SoldToZIPRomanized>'
    +'<q17:SoldToCityRomanized>Plovdiv</q17:SoldToCityRomanized>'
    +'<q17:SoldToCountry>BG</q17:SoldToCountry>'
    +'<q17:SoldToGLN xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>'
    +'<q17:SoldToSameGroupAsStockOwner>false</q17:SoldToSameGroupAsStockOwner>'
    +'</q17:SoldToSection>'
    +'<q17:TrackingLocationSection xmlns:q17="http://ITrack.Transmission/2011/02/25/Objects">'
    +'<q17:TrackingLocationCode>Burda</q17:TrackingLocationCode>'
    +'<q17:TrackingLocationName>Импириъл Табако Дистрибуция ЕООД</q17:TrackingLocationName>'
    +'<q17:TrackingLocationAddress1>Tarlaua 41/1, CF 55639, Dragomiresti Vale, Numar cadastral 55639</q17:TrackingLocationAddress1>'
    +'<q17:TrackingLocationAddress2></q17:TrackingLocationAddress2>'
    +'<q17:TrackingLocationZIP></q17:TrackingLocationZIP>'
    +'<q17:TrackingLocationCity>Dragomiresti Vale</q17:TrackingLocationCity>'
    +'<q17:TrackingLocationNameRomanized>Burda</q17:TrackingLocationNameRomanized>'
    +'<q17:TrackingLocationAddress1Romanized>Tarlaua 41/1, CF 55639, Dragomiresti Vale, Numar cadastral 55639</q17:TrackingLocationAddress1Romanized>'
    +'<q17:TrackingLocationAddress2Romanized></q17:TrackingLocationAddress2Romanized>'
    +'<q17:TrackingLocationZIPRomanized></q17:TrackingLocationZIPRomanized>'
    +'<q17:TrackingLocationCityRomanized>Dragomiresti Vale</q17:TrackingLocationCityRomanized>'
    +'<q17:TrackingLocationCountry>RO</q17:TrackingLocationCountry>'
    +'<q17:TrackingLocationGLN xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>'
    +'<q17:TrackingLocationCoord xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>'
    +'</q17:TrackingLocationSection>'
    +'</q1:DocumentSection>'
    +'</tns:StoreDocumentMessage>'
    +'</tns:StoreDocument>'
    +'</soapenv:Body>'




    SET @RootNode='<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">'+@SOAPHeader+@SOAPBody+'</soapenv:Envelope>'

    DECLARE @XMLFileDescription varchar(250);
    SET @XMLFileDescription='StoreDocument'+'.xml

EXEC ExportXML @RootNode,'D:\TT', @XMLFileDescription'

我收到下面的错误,但我真的不知道在哪里定义命名空间,或者至少我的想法没有想到更多&#34;开箱即用&#34;。

  

DestinationGLN上的命名空间前缀q17未定义

1 个答案:

答案 0 :(得分:0)

除了意大利面条代码的恶魔在你的右耳耳语中关于CURSOR和程序性思考(见其他问题:-D)之外,还有一些错误的工具,在你的左耳中悄悄地谈论创建XML通过字符串连接。您有永远不应该这样做有几个原因!

如果你的一个连接值包含一个禁用字符(即&, < and >),这将会中断!这可能会通过所有内部测试并打破客户的系统。很糟糕......

在你身后,有一些错误类型的恶魔,在VARCHAR中对你的俄语字符低语。

尝试

SELECT 'Табако Трейд ООД',
      N'Табако Трейд ООД';

结果

varchar             nvarchar
?????? ????? ???    Табако Трейд ООД

明白我的意思?始终使用NVARCHAR

关于您的错误

如果存在未声明的名称空间前缀,则不允许将字符串视为XML(即使它看起来像有效的XML!)。