我有一个名为“project”的表,其列名为“path”,其XML数据存储为text(nvarchar(max))。
这是“path”列
下的其中一行的内容<?xml version="1.0" encoding="utf-16"?>
<ProjectPerfoceSetting xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Server>101.0.101.101</Server>
<Port>1625</Port>
</ProjectPerfoceSetting>
我想得到以下输出,
服务器端口 101.0.101.101 1625
以下是我正在使用的查询,它返回null,
SELECT
CAST(path AS XML).value('declare namespace ns="http://www.w3.org/2001/XMLSchema";(/ProjectPerfoceSetting/Server/@Text)[1]', 'varchar(200)')
FROM project
有人可以建议上面的查询有什么问题。提前谢谢。
更新
不知何故,查询将为以下XML数据返回null,
<?xml version="1.0" encoding="utf-16"?>
<ProjectTFSSetting xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://DT">
<Server>45.25.211.222</Server>
<Port>8080</Port>
</ProjectTFSSetting>
查询
Select A.[ProjectName]
,Server = B.XMLData.value('(ProjectTFSSetting/Server)[1]', 'varchar(200)')
,URL = B.XMLData.value('(ProjectTFSSetting/Url)[1]' , 'varchar(200)')
From [CxDB].[dbo].[Broadridge_Scans_View] A
Cross Apply (Select XMLData = cast(Path as xml)) B
另外,列“路径”也存储了一些文本数据。除了解析XML数据之外,我该如何访问它们?
现在是另一种情况,
这就是表“项目”在下面的列中的样子。
ID路径
1 <?xml version="1.0" encoding="utf-16"?><ProjectPerfoceSetting xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><Server>101.0.101.101</Server><Port>1625</Port></ProjectPerfoceSetting>
2 <?xml version="1.0" encoding="utf-16"?><ProjectGITSetting xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><URL>http://test.com</Server><folder>/customer</folder></ProjectPerfoceSetting>
3 <?xml version="1.0" encoding="utf-16"?><ProjectTFSSetting xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://DT"><Server>45.25.211.222</Server><Port>8080</Port></ProjectTFSSetting>
4有些文字不是xml
是否有可能低于输出?
ID Server Port
1 101.0.101.101 1625
2 http://test.com /customer
3 http://londbcntfs:8080/tfs/BCN/ 8080
4 some text not xml
答案 0 :(得分:0)
最好将XML数据存储为xml。但是,通过添加CROSS APPLY(OUTER APPLY以显示NULL),促进转换变得很小。
示例(添加第二行以说明)
Declare @Project table (ID int,Path nvarchar(max))
Insert Into @Project values
(1,'<ProjectPerfoceSetting xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><Server>101.0.101.101</Server><Port>1625</Port></ProjectPerfoceSetting>'),
(2,'<ProjectTFSSetting xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://DT"><Server>http://londbcntfs:8080/tfs/BCN/</Server><Port>8080</Port></ProjectTFSSetting>')
Select A.ID
,Server = B.XMLData.value('(*/Server)[1]', 'varchar(200)')
,Port = B.XMLData.value('(*/Port)[1]' , 'varchar(200)')
From @Project A
Cross Apply (Select XMLData = cast(replace(Path,'xmlns="','dummy="') as xml)) B
<强>返回强>
ID Server Port
1 101.0.101.101 1625
2 http://londbcntfs:8080/tfs/BCN/ 8080
答案 1 :(得分:0)
提供的ID 2 XML不正确。
对OUTER APPLY和IsNull()的小改动
您还需要学习如何更好地格式化问题。
Declare @Project table (ID int,Path nvarchar(max))
Insert Into @Project values
(1,'<?xml version="1.0" encoding="utf-16"?><ProjectPerfoceSetting xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><Server>101.0.101.101</Server><Port>1625</Port></ProjectPerfoceSetting>'),
(3,'<?xml version="1.0" encoding="utf-16"?><ProjectTFSSetting xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://DT"><Server>45.25.211.222</Server><Port>8080</Port></ProjectTFSSetting>'),
(2,'<?xml version="1.0" encoding="utf-16"?><ProjectGITSetting xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><URL>http://test.com</URL><folder>customer</folder></ProjectGITSetting>'),
(4,'some text not xml')
Select A.ID
,Server = isnull(B.XMLData.value('(*/Server)[1]', 'varchar(200)'),isnull(B.XMLData.value('(*/URL)[1]', 'varchar(200)'),Path))
,Port = isnull(B.XMLData.value('(*/Port)[1]' , 'varchar(200)'),B.XMLData.value('(*/folder)[1]', 'varchar(200)'))
From @Project A
Outer Apply (Select XMLData = cast(replace(Path,'xmlns="','dummy="') as xml)) B
<强>返回强>
ID Server Port
1 101.0.101.101 1625
3 45.25.211.222 8080
2 http://test.com customer
4 some text not xml NULL