如何从xml获取标记属性值

时间:2017-05-15 14:24:07

标签: sql sql-server xml tsql xml-parsing

我有一个xml,其中给出了值:

                  urlkey                   name    id
1            selfdefense           Self-Defense   443
2                martial           Martial Arts   681
3                jujitsu              Jiu Jitsu  9615
4     mixed-martial-arts     Mixed Martial Arts 15514
5             kickboxing             Kickboxing 18225
6              jiu-jitsu              Jiu-jitsu 21219
7     brazilian-jiujitsu    Brazilian Jiu-Jitsu 22237
8 mma-mixed-martial-arts MMA Mixed Martial Arts 35023
9    brazilian-jiu-jitsu    Brazilian Jiu Jitsu 46818

我需要来自 OutofOfficeSettings isOutofOffice = 的值(选项为false或true)。

我之前从未这样做过,有人在这里帮助我吗?

1 个答案:

答案 0 :(得分:2)

一个选项

Declare @DWUser  table (Active int, settings xml )
Insert Into @DWUser  values 
 (1,'<User version="5.1.0.1" uid="1" oid="2" w3uid="ADMIN" guid="05b6f2a2-cdf8-4b73-8d1b-86eab657654" oguid="43a0d394-e515-45ea-aa25-7822673c52a3" name="admin" active="true" eMail="admin@admin.com" distribution="NetworkStartup" securityLevel="Normal" defaultWebBasket="cc1c589a-549a-4957-959b-3b5acc4decc9"><Login usewinLogin="true" passwordNeverExpires="false" /><Misc /><AdditionalInfo /><OutOfOfficeSettings isOutOfOffice="false" startDateTime="0001-01-01T00:00:00.0000000Z" /><RegionalSettings language="de" culture="de" /></User>')
,(1,'<root><OtherXML>Hi</OtherXML></root>')


Select A.active
      ,Type = X.attr.value('@isOutOfOffice','varchar(100)')
 From @DWUser A
 Outer Apply A.settings.nodes('User/OutOfOfficeSettings') as X(attr)
 Where A.Active=1

返回

active  Type
1       false
1       NULL