不,没有一般的方法,至少我不知道…
假设两个XML看起来是相同的,但是其中一个有一个子元素,而另一个缺少该子元素。这可能是相同的模式,或者不是…
通过以下方法,您可以检索到一些
元数据
. 把这个写在边桌上,试着用
GROUP BY
找到你的
XML系列
DECLARE @tbl TABLE(ID INT IDENTITY, ShortDescr VARCHAR(100), YourXML XML);
INSERT INTO @tbl VALUES
('root and test', N'<root><test/></root>')
,('root and test and more', N'<root><test/><a /><b /></root>')
,('blah and test', N'<blah><test/></blah>')
,('no root (blah and blub)', N'<blah></blah><blub />')
,('no content', NULL)
;
SELECT t.ID
,t.ShortDescr
,CASE ISNULL(t.YourXML.value('count(/*)','int'),0)
WHEN 1 THEN 'HasRoot'
WHEN 0 THEN 'empty'
ELSE 'Fragment' END AS XmlFormat
,t.YourXML.value('local-name((/*)[1])','nvarchar(max)') AS RootName
,t.YourXML.value('local-name((/*[1]/*)[1])','nvarchar(max)') AS FirstChild
FROM @tbl t;
结果如下:
+----+-------------------------+-----------+----------+------------+
| ID | ShortDescr | XmlFormat | RootName | FirstChild |
+----+-------------------------+-----------+----------+------------+
| 1 | root and test | HasRoot | root | test |
+----+-------------------------+-----------+----------+------------+
| 2 | root and test and more | HasRoot | root | test |
+----+-------------------------+-----------+----------+------------+
| 3 | blah and test | HasRoot | blah | test |
+----+-------------------------+-----------+----------+------------+
| 4 | no root (blah and blub) | Fragment | blah | |
+----+-------------------------+-----------+----------+------------+
| 5 | no content | empty | NULL | NULL |
+----+-------------------------+-----------+----------+------------+