Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于XML的T-SQL(使用XQuery)_Sql_Xml_Tsql - Fatal编程技术网

基于XML的T-SQL(使用XQuery)

基于XML的T-SQL(使用XQuery),sql,xml,tsql,Sql,Xml,Tsql,我有下面的XML <myroot> <scene> <sceneId>983247</sceneId> <item> <coordinates> <coordinate>0</coordinate> <coordinate>1</coordinate> <coordinate>2</coordinate> <coordinate>3</

我有下面的XML

<myroot>
<scene>
<sceneId>983247</sceneId>
<item>
<coordinates>
<coordinate>0</coordinate>
<coordinate>1</coordinate>
<coordinate>2</coordinate>
<coordinate>3</coordinate>
</coordinates>
<Values>
<Value>34</Value>
<Value>541</Value>
<Value>255</Value>
<Value>332</Value>
</Values>
</item>
</scene>
</myroot>
谢谢


M

此XPath 2.0表达式:

/myroot/scene/item/
   string-join(for $pos in (0 to max(*/count(*)))
               return string-join(for $col in (1 to max(count(*)))
                                  return if ($pos=0)
                                         then concat('Col',$col)
                                         else *[$col]/*[$pos],
                                  ' '),
               '&#xA;')
输出:

Col1 Col2
0 34
1 541
2 255
3 332

这是我的XMLNoob方法

如果您只信任元素序列,而不信任坐标值本身是序列:

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
如果您只相信坐标编号是序列,但来自任意种子:

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)
选择

coordinate=row.value('for$s in.return count(../*[。尝试这样运行它:选择@xml.query('/myroot/scene/item/string join(for$pos in(0到max(/count())))返回字符串join(for$col in(1到max(count(*))返回if($pos=0)然后concat('Col',$Col)else*[$Col]/*[$pos],'','','',''和#xA;'))似乎不起作用work@koumides:
fn:count()
必须有一个参数。第一个
fn:max
调用计算某个“列”中的最大元素数。因此它必须是
max(*/count(*)
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)
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)