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 5843219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:07:27+00:00 2026-05-22T12:07:27+00:00

Consider the following xml: <Persons num=3> <Person age=5 /> <Person age=19 /> </Persons> There

  • 0

Consider the following xml:

<Persons num="3">
  <Person age="5" />
  <Person age="19" />
</Persons>

There is a need to extract this xml into a relational table:

Persons table (Age1 int, Age2 int, Age3 int , Age4 int)

Parsing has to satisfy the following constraints:

  • all persons with age >=18 must be assigned to columns with smallest column number and the value has to be 18
  • if the age of the person is not given it is equal to 18
  • all persons with age <18 must follow
  • if there are less than 4 persons, those which are not provided must have age=-1

In a given example, there are 3 persons, ages of 2 of them are provided: 5 and 19 respectively. The content of the table Persons has to be the following:

18 18 5 -1

Is there the best way to do so with xpath?

Till now I can parse the xml and assign ages but what is not clear is to how make ordering:

declare @XmlData xml = 
'<Persons num="3">
    <Person age="5" />
    <Person age="19" />
</Persons>'

declare @Persons table (Age1 int, Age2 int, Age3 int , Age4 int)
insert into @Persons (Age1, Age2, Age3, Age4)
select ISNULL(Age1, case when Num>= 1 then 18 else -1 end) Age1
    , ISNULL(Age2, case when Num>= 2 then 18 else -1 end) Age2
    , ISNULL(Age3, case when Num>= 3 then 18 else -1 end) Age3
    , ISNULL(Age4, case when Num>= 4 then 18 else -1 end) Age4
from (
    select Persons.Person.value('@num','smallint') as Num
          ,Persons.Person.value('Person[@age<18][1]/@age','smallint') as Age1
          ,Persons.Person.value('Person[@age<18][2]/@age','smallint') as Age2
          ,Persons.Person.value('Person[@age<18][3]/@age','smallint') as Age3
          ,Persons.Person.value('Person[@age<18][4]/@age','smallint') as Age4
    from @XmlData.nodes('/Persons') Persons(Person)
 ) Persons  

select *
from @Persons

Result is

5 18 18 -1
  • 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-05-22T12:07:28+00:00Added an answer on May 22, 2026 at 12:07 pm

    Another solution requires a bit more sql code but costs only ~80 in estimated execution plan.

    There is one constraint wrt the problem statement: Persons/@num has to be equal to a number of Person tags

    Limitations are:

    • limited number of persons per room

    Here is sql code:

    --//initial xml data
    declare @XmlData xml = 
    '<Persons roomid="1" num="3">
        <Person age="19" />
        <Person age="10" /> 
        <Person age="5" />
    </Persons>
    <Persons roomid="4" num="4">
        <Person age="17" />
        <Person age="10" /> 
        <Person age="5" />
        <Person age="1" />
    </Persons>'
    
    --//shade xml into temporal table: rank is applied to an age in descreasing order
    declare @tmp table (age int, roomid int, orderid int)
    insert into @tmp(age,roomid,orderid)
    select Persons.age
          ,Persons.roomid
          ,ROW_NUMBER () over (partition by Persons.roomid order by Persons.age desc)
    from(
        select Ps.P.value('(@age)[1]','smallint') age       
              ,Ps.P.value('(../@roomid)[1]','smallint') roomid
        from @XmlData.nodes('/Persons/Person') Ps(P)
    )Persons
    order by Persons.roomid,Persons.age desc    
    
    --//provide ordering for roomid: since roomid may be different (the only thing that is required that roomid is unique)
    declare @roomidmapping table (roomid int, roomorderid int)
    insert into @roomidmapping(roomid, roomorderid)
    select roomid, ROW_NUMBER () over  (order by roomid asc)
    from @tmp
    group by roomid
    declare @roomnumber int = @@ROWCOUNT
    --//final result
    ;WITH ConsequtiveNums AS
    (
        SELECT 1 AS Number
        UNION ALL
        SELECT Number+1
        FROM ConsequtiveNums
        WHERE Number<@roomnumber
    )
    select (select case when age>18 then 18 else age end from @tmp T inner join @roomidmapping M on T.roomid = M.roomid where T.orderid = 1 and M.roomorderid = CN.Number)
          ,(select case when age>18 then 18 else age end from @tmp T inner join @roomidmapping M on T.roomid = M.roomid where T.orderid = 2 and M.roomorderid = CN.Number)
          ,(select case when age>18 then 18 else age end from @tmp T inner join @roomidmapping M on T.roomid = M.roomid where T.orderid = 3 and M.roomorderid = CN.Number)
          ,(select case when age>18 then 18 else age end from @tmp T inner join @roomidmapping M on T.roomid = M.roomid where T.orderid = 4 and M.roomorderid = CN.Number)
    from ConsequtiveNums CN
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this scenario: I've an XML file called person.xml with the following data in
Consider the following XML: <a> <b>2</b> <c></c> </a> I need to deserialize this xml
Using MSSQL 2008 and XQUERY Consider the following XML stored in a table: <ROOT>
Please consider the following XML-- <table class=rel_patent><tbody> <tr><td>Name</td><td>Description</td></tr> <tr><td>A</td><td>Type-A</td></tr> <tr><td>B</td><td>Type-B</td></tr> <tr><td>C</td><td>Type-C</td></tr> <tr><td>AC</td><td>Type-C Type-A</td></tr> <tr><td>D</td><td></td></tr>
Consider the following query: /solr/select?q=linux It returns this XML response: <response> <lst name=responseHeader> <int
Consider the following situation: There is an xml that contains data @XmlData The @XmlData
Consider the following function prototype for caching object from cached RSS(XML) feed: function cacheObject($xml,$name,$age
Consider the following xml document. I need to get the value of Kolumbi. It
Consider the following TSQL: declare @xml xml select @xml = '<test xmlns=http://this-is-the-default-namespace-uri>some data</test>' select
Consider the following script for outputting some XML code: var xmlAsString = '<?xml version=1.0?><person><name

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.