没有根元素的XML仍然有效?

时间:2015-09-30 21:54:48

标签: sql-server xml split

我找到了分割字符串的非常好的方法:

DECLARE @x VARCHAR(100)='a,b,c';

--This is the "normal" way
WITH SplitXml(x) AS
(SELECT CAST('<root><x>' + REPLACE(@x,',','</x><x>') + '</x></root>' AS XML))
SELECT SplitXml.x, a.b.value('.','varchar(1)')
FROM SplitXml
CROSS APPLY SplitXml.x.nodes('/root/x') AS a(b);

然后我找到了相同的方法,没有&#34; root&#34;而且我认为这不起作用,但它确实......

--Now the same without "root"
--This leads to invalid XML, but works without problems
WITH SplitXml(x) AS
(SELECT CAST('<x>' + REPLACE(@x,',','</x><x>') + '</x>' AS XML))
SELECT SplitXml.x, a.b.value('.','varchar(1)')
FROM SplitXml
CROSS APPLY SplitXml.x.nodes('/x') AS a(b)

SplitXml.x中的XML是有效还是无效? XML总是需要root-singleton吗?

0 个答案:

没有答案