Consider this XML:
<Employees>
    <Person>
        <ID>1000</ID>
        <Name>Nima</Name>
        <LName>Agha</LName>
    </Person>
    <Person>
        <ID>1001</ID>
        <Name>Ligha</Name>
        <LName>Ligha</LName>
    </Person>
    <Person>
        <ID>1002</ID>
        <Name>Jigha</Name>
        <LName>Jigha</LName>
    </Person>
    <Person>
        <ID>1003</ID>
        <Name>Aba</Name>
        <LName>Aba</LName>
    </Person>
</Employees>
I declare a XML variable and assign this XML to that. How I can get count of ID elements in this XML variable using Sql Server 2008 (TSQL)?
Count the XML elements (XPath)newXPath(); NodeList nodes = (NodeList) xpath. evaluate("//staff", doc, XPathConstants. NODESET); int count = nodes. getLength();
To find nodes in an XML file you can use XPath expressions. Method XmlNode. SelectNodes returns a list of nodes selected by the XPath string.
SELECT @XMLVariable.value('count(/Employees/Person/ID)', 'int') AS IDCount
                        try this:
declare @xmlvar XML;
set @xmlvar ='<YOUR XML>';
select @xmlvar.value('count(/Employees/Person/ID)', 'INT') AS 'Count'
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With