栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

T-SQL遍历XML数据列以派生唯一的路径集

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

T-SQL遍历XML数据列以派生唯一的路径集

在这里,过时的方法

FROMOPENXML
可能是一个选择。检查此答案。

在此链接中,您会发现一个不时发布的功能John
Cappelletti,该功能将切碎所有XML(该功能代码下方的功劳)。

但我不确定,您真正想要实现的目标…为什么需要这条路?如果您对所有目标节点的值感兴趣,则可以执行以下操作(使用进行深度搜索

//
不需要确切的
XPath

 SELECt t.value(N'(text())[1]','nvarchar(max)') FROM @xml.nodes('//target') AS A(t);

如果您真的需要 所有的一切 ,可以检查以下内容:

CREATE FUNCTION [dbo].[udf-XML-Hier](@XML xml)Returns Table As Returnwith  cte0 as (        Select Lvl       = 1  ,ID        = Cast(1 as int)   ,Pt        = Cast(NULL as int)  ,Element   = x.value('local-name(.)','varchar(150)')  ,Attribute = cast('' as varchar(150))  ,Value     = x.value('text()[1]','varchar(max)')  ,XPath     = cast(concat(x.value('local-name(.)','varchar(max)'),'[' ,cast(Row_Number() Over(Order By (Select 1)) as int),']') as varchar(max))  ,Seq       = cast(1000000+Row_Number() over(Order By (Select 1)) as varchar(max))  ,AttData   = x.query('.')   ,XMLData   = x.query('*')        From   @XML.nodes('@*') a(x)   ) A     )Select A.R1      ,R2  = IsNull((Select max(R1) From cte1 Where Seq Like A.Seq+'%'),A.R1)      ,A.Lvl      ,A.ID      ,A.Pt      ,A.Element      ,A.Attribute      ,A.XPath      ,Title = Replicate('|---',Lvl-1)+Element+IIF(Attribute='','','@'+Attribute)      ,A.Value From  cte1 AGODECLARE @xml XML='<log>  <clients>   <client>    <section name ="Apps">      <questions>      <groupone>       <question>        <target>Age</target>       </question>       <question>        <target> Height</target>       </question>       <question>        <target> Weight</target>       </question>      </groupone>      <grouptwo name = "exercise">       <wording>what is your name</wording>        <question>         <id>1</id>         <target>def</target>        </question>      </grouptwo>     </questions>    </section>   </client>  </clients> </log>'; SELECt * FROM dbo.[udf-XML-Hier](@xml);GO


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/456313.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号