The best way to extract data from xml with xquery

青春壹個敷衍的年華 提交于 2020-01-14 18:47:12

问题


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:


I have found a bit dirty solution:

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('xs:integer(fn:number(@num))+1','int') as Num1
          ,Persons.Person.value('Person[@age<18][xs:integer(fn:number(../@num))][1]/@age','smallint') as Age1
          ,Persons.Person.value('Person[@age<18][xs:integer(fn:number(../@num))-1][1]/@age','smallint') as Age2
          ,Persons.Person.value('Person[@age<18][xs:integer(fn:number(../@num))-2][1]/@age','smallint') as Age3
          ,Persons.Person.value('Person[@age<18][xs:integer(fn:number(../@num))-3][1]/@age','smallint') as Age4 
    from @XmlData.nodes('/Persons') Persons(Person)
 ) Persons

The idea of a solution is to first extract those contacts that are >=18, then extract those that are 0 < age < 18 and finally set those that are not provided to -1

UPD: despite the fact that solution provided correct results, its cost is high: ~1000 in estimated execution plan




回答2:


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


来源:https://stackoverflow.com/questions/6070256/the-best-way-to-extract-data-from-xml-with-xquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!