Here's my XML noob approach.
If you only trust the element sequencing, and not the coordinate values themselves being a sequence:
select
coordinate = max(case when element = 'coordinate' then elemval end)
, value = max(case when element = 'Value' then elemval end)
from (
select
element = row.value('local-name(.)','varchar(32)')
, elemval = row.value('.','int')
, position = row.value('for $s in . return count(../*[. << $s]) + 1', 'int')
from @xml.nodes('/myroot/scene/item/*/*') a (row)
) a
group by position
Alternatively written as two .nodes()
and a JOIN
(you get the idea).
If do you trust the coordinate numbering to be a sequence starting at zero:
select
coordinate = row.value('for $s in . return count(../*[. << $s]) + 1', 'int')
- 1
, value = row.value('.','int')
from @xml.nodes('/myroot/scene/item/Values/*') a (row)
If you only trust the coordinate numbering to be a sequence, but from an arbitrary seed:
select
coordinate = row.value('for $s in . return count(../*[. << $s]) + 1', 'int')
+ row.value('(/myroot/scene/item/coordinates/coordinate)[1]','int')
- 1
, value = row.value('.','int')
from @xml.nodes('/myroot/scene/item/Values/*') a (row)
Paths can be abbreviated:
/myroot/scene/item/*/*
-> //item/*/*
/myroot/scene/item/Values/*
-> //Values/*
/myroot/scene/item/coordinates/coordinate
-> //coordinate
But I don't know the wisdom of this either way.
//item/*/*
can probably be made more specific, so that it only includes coordinate
and Value
edge nodes, but I don't know the syntax.