nodes() Method (xml Data Type)

https://docs.microsoft.com/en-us/sql/t-sql/xml/nodes-method-xml-data-type

The nodes() method is useful when you want to shred an xml data type instance into relational data.

It allows you to identify nodes that will be mapped into a new row.

Every xml data type instance has an implicitly provided context node.

For the XML instance stored in a column or variable, this is the document node.

The document node is the implicit node at the top of every xml data type instance.

The result of the nodes() method is a rowset that contains logical copies of the original XML instances.

In these logical copies, the context node of every row instance is set to one of the nodes identified with the query expression, so that subsequent queries can navigate relative to these context nodes.

You can retrieve multiple values from the rowset.

For example, you can apply the value() method to the rowset returned by nodes() and retrieve multiple values from the original XML instance.

Note that the value() method, when applied to the XML instance, returns only one value.

Syntax

nodes (XQuery) as Table(Column)  

Arguments

XQuery
Is a string literal, an XQuery expression.

If the query expression
constructs nodes, these constructed nodes are exposed in the resulting
rowset.

If the query expression results in an empty sequence, the rowset
will be empty.

If the query expression statically results in a sequence
that contains atomic values instead of nodes, a static error is raised.

Table(Column)
Is the table name and the column name for the resulting rowset.

Example

 DECLARE @UsedRecords XML;
SET @UsedRecords = '<Record ID="107" /><Record ID="116" /><Record ID="410" />'; SELECT Result.Id.value(
'@ID' ,
'int'
)
FROM @UsedRecords.nodes('/Record') AS Result(Id)

query() Method (xml Data Type)

https://docs.microsoft.com/en-us/sql/t-sql/xml/query-method-xml-data-type

declare @myDoc xml
set @myDoc = '<Root>
<ProductDescription ProductID="1" ProductName="Road Bike">
<Features>
<Warranty>1 year parts and labor</Warranty>
<Maintenance>3 year parts and labor extended maintenance is available</Maintenance>
</Features>
</ProductDescription>
</Root>'
SELECT @myDoc.query('/Root/ProductDescription/Features')
DECLARE @Propertis XML;
SELECT @Propertis = WebPartProperties
FROM dbo.CMS_WebPart
WHERE WebPartName = 'LISA_GiftBrowser';
SELECT @Propertis;
SELECT @Propertis.query('/form/field/settings/controlname')

参考

https://stackoverflow.com/questions/15680259/parse-xml-in-sql-server

DECLARE @xml xml
SET @xml =
'<GespeicherteDaten>
<strategieWuerfelFelder Type="strategieWuerfelFelder">
<Felder X="3" Y="3" Z="3">
<Feld X="1" Y="1" Z="1">
<strategieWuerfelFeld Type="strategieWuerfelFeld">
<Name>Name</Name>
<Beschreibung>Test</Beschreibung>
</strategieWuerfelFeld>
</Feld>
<Feld X="1" Y="1" Z="2">
<strategieWuerfelFeld Type="strategieWuerfelFeld">
<Name>Name2</Name>
<Beschreibung>Test2</Beschreibung>
</strategieWuerfelFeld>
</Feld>
</Felder>
</strategieWuerfelFelder>
</GespeicherteDaten>' SELECT
b.value('@X', 'int') as X
, b.value('@Y', 'int') as Y
, b.value('@Z', 'int') as Z
, b.value('(./strategieWuerfelFeld/Name/text())[1]','Varchar(50)') as [Name]
, b.value('../@X','int') as Felder_X
, b.value('../@Y','int') as Felder_Y
, b.value('../@Z','int') as Felder_Z
FROM @xml.nodes('/GespeicherteDaten/strategieWuerfelFelder/Felder/Feld') as a(b)
<field column="CoverAllGiftBrand" fieldcaption="Cover All Brand" visible="true" columntype="text" fieldtype="CustomUserControl" allowempty="true" columnsize="50" fielddescription="A brand which can cover all brands of gift" publicfield="false" guid="734db2ca-5ce3-4326-a5c8-a821631e09ae" visibility="none">
<settings>
<controlname>textboxcontrol</controlname>
</settings>
</field>
DECLARE @Propertis XML;
DECLARE @Result XML;
SELECT @Propertis = WebPartProperties
FROM dbo.CMS_WebPart
WHERE WebPartName = 'LISA_GiftQuickSearch';
SELECT
b.value('@fieldcaption','nvarchar(50)') AS Property
,b.value('@column','nvarchar(50)') AS ColumnName
,b.value('(./settings/controlname/text())[1]', 'nvarchar(50)') AS ControlType
FROM @Propertis.nodes('/form/field') AS a(b)
WHERE b.value('(./settings/controlname/text())[1]', 'nvarchar(50)') LIKE '%lisa%'
ORDER BY ControlType;
DECLARE @Propertis XML;
DECLARE @Result XML;
SELECT @Propertis = WebPartProperties
FROM dbo.CMS_WebPart
WHERE WebPartName = 'LISA_GiftBrowser';
SELECT
DISTINCT b.value('(./settings/controlname/text())[1]', 'nvarchar(100)') AS ControlType
FROM @Propertis.nodes('/form/field') AS a(b)
WHERE b.value('(./settings/controlname/text())[1]', 'nvarchar(100)') LIKE '%lisa%'
ORDER BY ControlType;

Parse List

DECLARE @Parameters XML = '
<Request xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Orders>
<Order>
<Column>Column1</Column>
<Direction>Asc</Direction>
</Order>
<Order>
<Column>Column2</Column>
<Direction>Desc</Direction>
</Order>
</Orders>
<AccountId>1</AccountId>
</Request>
';
DECLARE @AccountId INT; SELECT @AccountId = b.value('(./AccountId/text())[1]', 'INT')
FROM @Parameters.nodes('/Request') AS a(b);
SELECT @AccountId AS AccountId SELECT b.value('(./Column/text())[1]', 'NVARCHAR(50)') AS ColumnName ,
b.value('(./Direction/text())[1]', 'NVARCHAR(50)') AS Direction
FROM @Parameters.nodes('/Request/Orders/Order') AS a(b);

parse audit criteria

DECLARE @Params XML
= N'<AuditCriteria xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><ProgramName /><UserName /><ActionType /></AuditCriteria>'; SELECT b.value('(./ProgramName/text())[1]', 'NVarchar(max)'),
b.value('(./UserName/text())[1]', 'NVarchar(max)'),
b.value('(./ActionType/text())[1]', 'NVarchar(max)')
FROM @Params.nodes('/AuditCriteria') AS a(b);

最新文章

  1. java关闭流,解压缩后的清除
  2. find command in linux terminal
  3. hdu 1242 Rescue
  4. hello iic
  5. intent 传参数
  6. [Javascript] Other functor
  7. codeforces D. Long Path
  8. openwrt interface
  9. 关于EventHandler的使用
  10. 最短路径---dijkstra算法模板
  11. Android开发——使用intent传递对象
  12. get请求02
  13. PHP : MySQLi【面向过程】操作数据库【 连接、建库、建表、增、删、改、查、关闭】
  14. 设计模式学习心得&lt;原型模式 Prototype &gt;
  15. 洛谷P1117 优秀的拆分
  16. 如何设置font-family
  17. twitter ads_campaign management
  18. 阿里云负载不支持 WebSocket 协议与 WSS 和 Nginx 配置问题
  19. 安全之路 —— 利用SVCHost.exe系统服务实现后门自启动
  20. Redis高可用之集群配置(六)

热门文章

  1. HTML提交表单
  2. SP687 REPEATS - Repeats(后缀数组)
  3. 队列(Queue)-c实现
  4. NIO框架之MINA源代码解析(一):背景
  5. Qt 3D教程(二)初步显示3D的内容
  6. [TypeScript] Asynchronous Iteration using for-await-of
  7. [leetcode][math] Add Digits
  8. 关于nth-of-type和nth-child的关系
  9. 当fastJson邂逅大写字段时
  10. UVa 1599 Ideal Path【BFS】