2016-11-09 66 views
0
SELECT 
    XMLQUERY ('$x/test/xml[1]/data(@type)' PASSING XMLCOLUMN as "x") type, 
    XMLQUERY ('$x/test/xml[1]/text()' PASSING XMLCOLUMN as "x") text 
FROM 
    XML 

我想選擇父節點的所有子節點。我在思索着什麼看起來像這樣:xQuery數組[*]?

SELECT 
    XMLQUERY ('$x/test/xml[*]/data(@type)' PASSING XMLCOLUMN as "x") type, 
    XMLQUERY ('$x/test/xml[*]/text()' PASSING XMLCOLUMN as "x") text 
FROM 
    XML 

編輯: XML:

<test> 
    <xml type = 1>a</xml> 
    <xml type = 2>b</xml> 
    <xml type = 3>c</xml> 
    <xml type = 4>d</xml> 
    <xml type = 5>e</xml> 
    ... 
</test> 

預期的結果:

type | text 
-----|----- 
1 | a 
2 | b 
3 | c 
4 | d 
... 
+0

直接子節點還是全部?看看http://stackoverflow.com/questions/5119655/xpath-to-get-all-child-nodes-elements-comments-and-text-without-parent,如果你要返回多個列,我推薦XMLTABLE而不是 –

+0

XMLQUERY('$ x/xml [*]/child :: node()'傳遞XMLCOLUMN爲「x」)文本 給了我相同的結果 –

+0

預期的結果是什麼? –

回答

1

嘗試像下面這樣的基礎上,XMLTABLE:

select t.* from x, 
     xmltable('$X/test/xml' 
      columns "type" int path 'data(@type)', 
        "text" varchar(10) path 'text()') as t 

它應該返回一個具有所需值和結構的表格。