【问题标题】:Oracle xmltype to display as columnOracle xmltype 显示为列
【发布时间】:2020-05-19 02:37:36
【问题描述】:

我的 xmltype 数据集如下:-

记录 1:

<row id=“1”>     
<c1>test|10</c1>   
<c2>teste|20</c2>    
</row>   

记录 2:

<row id=“2”>
<c1>2test|10</c1>
<c2>2teste|20</c2>
</row>

我需要以下面的格式显示数据,而且列不会被修复 但列中的值将采用相同的语法。

Id   Name    Value
------------------
1    test    10
1    teste   20
2    2test   10
2    2teste  20

【问题讨论】:

  • 请格式化您的预期结果。 >,你的意思是&lt;row&gt;下可以有子元素,比如b1、c3、c4等?
  • 嗨,是的, 下的子元素会像 c1...cn 一样递增。

标签: oracle xmltype


【解决方案1】:

您可以使用xmltable 将 xml 转换为表格形式。在这里,我添加了一个根元素&lt;R&gt; 和其他一些测试元素。您也可以按原样获取 SELECT 的值,然后使用 substr()(而不是 tokenize())。

select x.id, x.key, x.val from xmltable (
      '//row/*'

      passing xmltype (
      '<R>
        <row id="1">
          <c1>test|10</c1>
          <c2>teste|20</c2>
        </row> 
        <row id="2">
          <c1>2test|10</c1>
          <c2>2teste|20</c2>
          <c11>2test|11</c11>
          <c22>2teste|22</c22>
          <c33>2teste</c33>
          <c44>|44</c44>
          <c55></c55>
        </row>
      </R>') 

      columns 
        id number        path './../@id',
        key varchar2(30) path 'fn:tokenize(., "\|")[1]',
        val varchar2(30) path 'fn:tokenize(., "\|")[2]'
    ) x

            ID  KEY                             VAL                           
    ----------  ------------------------------  ------------------------------
             1  test                            10                              
             1  teste                           20                              
             2  2test                           10                              
             2  2teste                          20                              
             2  2test                           11                              
             2  2teste                          22                              
             2  2teste                                                          
             2                                  44                              
             2                                                                  

    9 rows selected. 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-08
    • 1970-01-01
    • 2015-02-08
    • 1970-01-01
    • 2016-11-25
    • 2012-07-19
    • 2015-06-26
    • 1970-01-01
    相关资源
    最近更新 更多