SQL SERVER XML修改OUTPUT添加NameSpace和root

时间:2017-11-15 16:39:13

标签: sql-server xml xml-namespaces for-xml-path

我需要在SQL Server 2016中修改我的XML以添加命名空间和几个标头标记。目前我有一个Select Query,它给了我这个块

选择@MAXML,@ TRANSXML,@ HOSTSETTINGSXML FOR XML PATH

  <merchantAuthentication>
  <name>myName</name>
  <transactionKey>myKey</transactionKey>
 </merchantAuthentication>
      <transactionRequest>
    <amount>5.500000000000000e+000</amount>
  </transactionRequest>
  <hostedPaymentSettings>
    <settingName>hostedPaymentBillingAddressOptions</settingName>
    <settingValue>{"show": true, "required":true}</settingValue>
  </hostedPaymentSettings>
  <hostedPaymentSettings>
    <settingName>hostedPaymentButtonOptions</settingName>
    <settingValue>{"text": "Pay"}</settingValue>
  </hostedPaymentSettings>
  <hostedPaymentSettings>
    <settingName>hostedPaymentReturnOptions</settingName>
    <settingValue>anyValue</settingValue>
  </hostedPaymentSettings>  

这应该是我的输出:

<Request>
    <Body>
        <Xml>
            <getHostedPaymentPageRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">
                 My xml from above
        </getHostedPaymentPageRequest>
    </Xml>
</Body>
</Request>

我被困住了。尝试了很多次,但无法得到确切的xml。谢谢你的到来。

1 个答案:

答案 0 :(得分:1)

我会给出自己的答案,这可能对其他人有所帮助。 步骤1:将整个XML转换为字符串并将标头添加为字符串 第2步:将新字符串转换回XML

步骤1:

Set @strXML = '<Request><Body><Xml><getHostedPaymentPageRequest 
xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">'+
                Convert(nvarchar(MAX),@x)+
                 '</getHostedPaymentPageRequest></Xml></Body></Request>'

步骤2:

SET @RequestXML = (SELECT CONVERT(XML,(SELECT CAST(@strXML AS XML))))