Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8397689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:51:32+00:00 2026-06-09T20:51:32+00:00

I have the following XML: <xmlRoot> <DomainName>sample.com</DomainName> <UserCountsByTemplate> <KeyValueOfstringint> <Key>default</d2p1:Key> <Value>20</d2p1:Value> </KeyValueOfstringint> <KeyValueOfstringint> <Key>basic</d2p1:Key>

  • 0

I have the following XML:

<xmlRoot>
  <DomainName>sample.com</DomainName>
  <UserCountsByTemplate>
    <KeyValueOfstringint>
      <Key>default</d2p1:Key>
      <Value>20</d2p1:Value>
    </KeyValueOfstringint>
    <KeyValueOfstringint>
      <Key>basic</d2p1:Key>
      <Value>66</d2p1:Value>
    </KeyValueOfstringint>
  </UserCountsByTemplate>
</xmlRoot>

I am trying to get two rows to be returned like so:

Domain      TemplateName  TemplateCount
----------  ------------  -------------
sample.com  default       20
sample.com  basic         66

I have tried the following:

DECLARE @info XML
SELECT @info = '<xmlRoot><DomainName>sample.com</DomainName><UserCountsByTemplate><KeyValueOfstringint><Key>default</Key><Value>20</Value></KeyValueOfstringint><KeyValueOfstringint><Key>basic</Key><Value>66</Value></KeyValueOfstringint></UserCountsByTemplate></xmlRoot>'
SELECT 
      row.value('DomainName[1]','NVARCHAR(255)') AS [DomainName]
    , row2.value('.','NVARCHAR(255)') AS [TemplateName]
    , row3.value('.','INT') AS [TemplateCount]
FROM @info.nodes('/xmlRoot[1]') T1(row)
CROSS APPLY @info.nodes('/xmlRoot/UserCountsByTemplate/KeyValueOfstringint/Key') T2(row2)
CROSS APPLY @info.nodes('/xmlRoot/UserCountsByTemplate/KeyValueOfstringint/Value') T3(row3)

But get the following results:

Domain      TemplateName  TemplateCount
----------  ------------  -------------
sample.com  default       20
sample.com  basic         20
sample.com  default       66
sample.com  basic         66

Any ideas as to where I am going wrong with this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:51:33+00:00Added an answer on June 9, 2026 at 8:51 pm

    You’re not correlating your access to Key and your access to value – so you’re effectively producing a cross join between all keys and all values.

    DECLARE @info XML
    SELECT @info = '<xmlRoot><DomainName>sample.com</DomainName><UserCountsByTemplate><KeyValueOfstringint><Key>default</Key><Value>20</Value></KeyValueOfstringint><KeyValueOfstringint><Key>basic</Key><Value>66</Value></KeyValueOfstringint></UserCountsByTemplate></xmlRoot>'
    SELECT 
          row.value('DomainName[1]','NVARCHAR(255)') AS [DomainName]
        , row2.value('Key[1]','NVARCHAR(255)') AS [TemplateName]
        , row2.value('Value[1]','INT') AS [TemplateCount]
    FROM @info.nodes('/xmlRoot[1]') T1(row)
    CROSS APPLY @info.nodes('/xmlRoot/UserCountsByTemplate/KeyValueOfstringint') T2(row2)
    

    Instead, I’m extracting the KeyValueOfstringInt nodes, and then accessing different elements in the value() calls. But both value() calls are operating on the same node (for each row).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following xml and I want to fetch the value of node which
I have some XML in the following format: <ObjectData> <ModelName>MODEL_123</ModelName> <ObjectName>OBJECT_A</ObjectName> <Values> <KeyValuePair> <Key>NAME</Key>
I have the following XML: http://pastebin.com/QiRK72BK which is generated in response to a REST
I have the following XML: <InterSection criteria=Microsoft-ASP>value</Intersection> <InterSection criteria=Microsoft-MVC>value</Intersection> <InterSection criteria=HP-MVC>value</Intersection> I need to
i have following xml file: <package> <reader> <id>r1007</id> <name>Robert</name> <email>robert@yahoo.com</email> </reader> <writer> <id>w1920</id> <name>Young</name>
assuming that I have following xml: <ns5:OrderTotal> <ns5:Name>AmountPaid</ns5:Name> <ns5:Description>Payment Received</ns5:Description> <ns5:SortOrder>4</ns5:SortOrder> <ns5:Value>16.95</ns5:Value> </ns5:OrderTotal> <ns5:OrderTotal>
I have a following XML (part of a .rdl report): <Report xmlns:rd=http://schemas.microsoft.com/SQLServer/reporting/reportdesigner xmlns=http://schemas.microsoft.com/sqlserver/reporting/2008/01/reportdefinition> <DataSources>
I have the following XML <map version=1.0> <properties> <property name=color value=blue /> <property name=size
I have following XML, and i wish to save its data in my SQL
I have following XML. I was able to remove all namespaces but not able

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.