Из вашего собственного кода я беру это SQL-Server.По крайней мере, синтаксис выглядит следующим образом.
Вы можете попробовать это:
DECLARE @xml XML=
N'<Attributes>
<Map>
<entry key="Division" value="TRAINING"/>
<entry key="IIQDisabled">
<value>
<Boolean>true</Boolean>
</value>
</entry>
<entry key="accountExpires" value="123456789L"/>
<entry key="accountFlags">
<value>
<List>
<String>Normal User Account</String>
<String>User Account is Disabled</String>
</List>
</value>
</entry>
<entry key="department" value="LOYALTY CLUB"/>
<entry key="distinguishedName" value="CN=Account02\,TM_Test02,OU=SailpointQA,OU=Users...."/>
<entry key="employeeID" value="333223"/>
<entry key="givenName" value="TM_Test02"/>
<entry key="memberOf"/>
<entry key="mobile" value="9"/>
<entry key="sAMAccountName" value="TM_Test02.Account02"/>
<entry key="sAMAccountType" value="805306368"/>
<entry key="sn" value="Account02"/>
<entry key="userAccountControl" value="514"/>
</Map>
</Attributes>';
- Запрос
SELECT entr.value('@key','nvarchar(100)') AS AttrKey
,entr.value('@value','nvarchar(500)') AS AttrValue
,HasValueElement.value('local-name(.)','nvarchar(100)') AS ValueType
,HasValueElement.value('text()[1]','nvarchar(500)') AS ValueTypeValue
,IsAList.value('local-name(.)','nvarchar(100)') AS ListValueType
,IsAList.value('text()[1]','nvarchar(500)') AS ListValueValue
FROM @xml.nodes(N'/Attributes/Map/entry') A(entr)
OUTER APPLY A.entr.nodes(N'value/*') B(HasValueElement)
OUTER APPLY B.HasValueElement.nodes('*') C(IsAList);
результат
+--------------------+-----------------------------------------------------+---------+------+--------+--------------------------+
| Division | TRAINING | | NULL | | NULL |
+--------------------+-----------------------------------------------------+---------+------+--------+--------------------------+
| IIQDisabled | NULL | Boolean | true | | NULL |
+--------------------+-----------------------------------------------------+---------+------+--------+--------------------------+
| accountExpires | 123456789L | | NULL | | NULL |
+--------------------+-----------------------------------------------------+---------+------+--------+--------------------------+
| accountFlags | NULL | List | NULL | String | Normal User Account |
+--------------------+-----------------------------------------------------+---------+------+--------+--------------------------+
| accountFlags | NULL | List | NULL | String | User Account is Disabled |
+--------------------+-----------------------------------------------------+---------+------+--------+--------------------------+
| department | LOYALTY CLUB | | NULL | | NULL |
+--------------------+-----------------------------------------------------+---------+------+--------+--------------------------+
| distinguishedName | CN=Account02\,TM_Test02,OU=SailpointQA,OU=Users.... | | NULL | | NULL |
+--------------------+-----------------------------------------------------+---------+------+--------+--------------------------+
| employeeID | 333223 | | NULL | | NULL |
+--------------------+-----------------------------------------------------+---------+------+--------+--------------------------+
| givenName | TM_Test02 | | NULL | | NULL |
+--------------------+-----------------------------------------------------+---------+------+--------+--------------------------+
| memberOf | NULL | | NULL | | NULL |
+--------------------+-----------------------------------------------------+---------+------+--------+--------------------------+
| mobile | 9 | | NULL | | NULL |
+--------------------+-----------------------------------------------------+---------+------+--------+--------------------------+
| sAMAccountName | TM_Test02.Account02 | | NULL | | NULL |
+--------------------+-----------------------------------------------------+---------+------+--------+--------------------------+
| sAMAccountType | 805306368 | | NULL | | NULL |
+--------------------+-----------------------------------------------------+---------+------+--------+--------------------------+
| sn | Account02 | | NULL | | NULL |
+--------------------+-----------------------------------------------------+---------+------+--------+--------------------------+
| userAccountControl | 514 | | NULL | | NULL |
+--------------------+-----------------------------------------------------+---------+------+--------+--------------------------+
Некоторое объяснение:
Существует три вида данных:
- Простые пары ключ-значение
- Типизированные пары ключ-значение
- Типизированные значения списка
В запросе будет использоваться .nodes()
, чтобы погрузиться во все <entry>
элементы и вернуть их в виде производной таблицы .Первый OUTER APPLY
вернет дополнительные строки / столбцы в тех случаях, когда ниже <entry>
есть элемент <value>
.Этот элемент может иметь значение (например, логическое «истина») или содержать типизированный список.Второй OUTER APPLY
погружается - если он существует - в подузлы <value>
и возвращает их в виде дополнительных строк.
Такой запрос вернул бы его больше в EAV-стиле
SELECT entr.value('@key','nvarchar(100)') AS AttrKey
,COALESCE(entr.value('@value','nvarchar(500)'),HasValueElement.value('text()[1]','nvarchar(500)'),IsAList.value('text()[1]','nvarchar(500)')) AS AttrValue
,HasValueElement.value('local-name(.)','nvarchar(100)') AS ValueType
,IsAList.value('local-name(.)','nvarchar(100)') AS ListValueType
FROM @xml.nodes(N'/Attributes/Map/entry') A(entr)
OUTER APPLY A.entr.nodes(N'value/*') B(HasValueElement)
OUTER APPLY B.HasValueElement.nodes('*') C(IsAList);
Результат
+--------------------+-----------------------------------------------------+-----------+---------------+
| AttrKey | AttrValue | ValueType | ListValueType |
+--------------------+-----------------------------------------------------+-----------+---------------+
| Division | TRAINING | | |
+--------------------+-----------------------------------------------------+-----------+---------------+
| IIQDisabled | true | Boolean | |
+--------------------+-----------------------------------------------------+-----------+---------------+
| accountExpires | 123456789L | | |
+--------------------+-----------------------------------------------------+-----------+---------------+
| accountFlags | Normal User Account | List | String |
+--------------------+-----------------------------------------------------+-----------+---------------+
| accountFlags | User Account is Disabled | List | String |
+--------------------+-----------------------------------------------------+-----------+---------------+
| department | LOYALTY CLUB | | |
+--------------------+-----------------------------------------------------+-----------+---------------+
| distinguishedName | CN=Account02\,TM_Test02,OU=SailpointQA,OU=Users.... | | |
+--------------------+-----------------------------------------------------+-----------+---------------+
| employeeID | 333223 | | |
+--------------------+-----------------------------------------------------+-----------+---------------+
| givenName | TM_Test02 | | |
+--------------------+-----------------------------------------------------+-----------+---------------+
| memberOf | NULL | | |
+--------------------+-----------------------------------------------------+-----------+---------------+
| mobile | 9 | | |
+--------------------+-----------------------------------------------------+-----------+---------------+
| sAMAccountName | TM_Test02.Account02 | | |
+--------------------+-----------------------------------------------------+-----------+---------------+
| sAMAccountType | 805306368 | | |
+--------------------+-----------------------------------------------------+-----------+---------------+
| sn | Account02 | | |
+--------------------+-----------------------------------------------------+-----------+---------------+
| userAccountControl | 514 | | |
+--------------------+-----------------------------------------------------+-----------+---------------+