xml Data Type Methods in sql server

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);

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(231)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2015-08-10 VS中常用的快捷键
2015-08-10 wcf中的File-less Activation
2015-08-10 什么是SysWow64
2015-08-10 wcf托管在IIS上,提示未能加载
2015-08-10 To enable integrated Windows authentication in Windows Vista/IIS 7
点击右上角即可分享
微信分享提示