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.
精彩评论