开发者

How to get the no of occurrences of element in xml data in sql server

开发者 https://www.devze.com 2023-03-21 03:31 出处:网络
In my xml i want to get the number of occuran开发者_如何转开发ces of an element how can i do this.

In my xml i want to get the number of occuran开发者_如何转开发ces of an element how can i do this.

for example in the below xml i want the number of times phone element is occuring

<Person>
 <Name>abc</Name>
 <Phone>1234</Phone>
 <Phone>9876</Phone>
 <Phone>5678</Phone>
</Person>

How can i achieve this ? Can someone help me .


declare @xml xml='<Person>
 <Name>abc</Name>
 <Phone>1234</Phone>
 <Phone>9876</Phone>
 <Phone>5678</Phone>
</Person>'

select @xml.value('count(//Person/Phone)','int')


You use XPATH. This site has a couple of examples of how to single out a specific type of node, by name. http://www.mycodeshare.com/item/1000000009/mssql-xml-query-using-xquery/

Start there and comment if it is not quite clear enough.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号