-- 简单的生成一个XML的格式内容.
--带有属性值的构造方法。
select 'en' as "English/@xml:lang",
'food' as "English",
'ger' as "German/@xml:lang",
'Essen' as "German",
'Female' as "who/@Gender", --属性值的构造。
'Angelia' as who
for xml path (N'')
--一条记录生成一个xml结点
declare @vendorMaster xml
--生成复杂XML
--解析XML的方法1:
SELECT @CustomerNumber = @Msg.value('(/Body/CustomerNumber/text())[1]',N'INT')
--解析XML的方法2:
--修改结点,向结点插入值
SET @PORequestMessage.modify(' declare default element namespace "http://soa.amazon.com/SOA/USA/InfrastructureService/V30/PubSubService";
insert sql:variable("@PO")
into (/Publish/Node/Body/PORequest/ContentNew)[1] ')
--插入一个结点:
declare @poTran xml
select @poTran = ( select
ItemCode = rtrim(item),
[Description] = rtrim(descrip),
from #POTransaction
FOR XML PATH('Item'),TYPE )
SET @PO.modify('
insert sql:variable("@poTran")
into (/PO/POItems)[1] ')
/*
BYDBA 1 解析XML时获取单一值时,不要使用nodes()
DECLARE @xml xml
SET @xml = N'<b>text1
<c>text2
<d>text3
<e>text4</e>
</d>
<f>text5</f>
</c>
</b>'
SELECT
@xml.value('(/b/c/d/text())[1]','char(5)')
,@xml.value('(/b/c/f/text())[1]','char(5)')
*/
/*About namespace 命名空间
DECLARE @xml xml
SET @xml = N'<b xmlns="https://soa.newegg.com/dba">text1
<c>text2
<d xmlns="https://soa.neweg.com">text3
<e>text4</e>
</d>
<f>text5</f>
</c>
</b>'
;WITH xmlnamespaces(DEFAULT 'https://soa.newegg.com/dba'
,'https://soa.neweg.com' AS ns)
SELECT
@xml.value('(/b/c/text())[1]','char(5)') AS xml_col_val
,@xml.value('(/b/c/ns:d/text())[1]','char(5)') AS xml_col_va2
*/
/*retrieve value 取单个值
DECLARE @xml xml
SET @xml = N'<b>text1
<c>text2
<d>text3
<e>text4</e>
</d>
<f>text5</f>
</c>
</b>'
SELECT @xml.value('(/b/c/text())[1]','char(5)') AS xml_col_val
*/