使用Pl / SQL中的分隔符表示xmlcast结果

时间:2018-02-21 13:55:48

标签: sql xml oracle

我对PL / SQL有点新,我有以下问题:

我的xml具有以下结构(b.response):

<a>
  <b>
    ...
    <ruleList>
      <number>2</number>
      <rule>
        <name>test1</name>
      </rule>
      <rule>
        <name>test2</name>
      </rule>
    </ruleList>
  </b>
</a>

我使用以下脚本从XML解析所需的值:

SELECT 
xmlcast(xmlquery('/a/b/ruleList/number' passing b.response returning content)as varchar2(1000)) rules_number,
xmlcast(xmlquery('/a/b/ruleList/rule[*]/name'passing b.response returning content)as varchar2(1000)) values
FROM test b

并有以下结果:

rules_number | values
           2 | test1test2

我的问题是如何使用xmlcast输入名称之间的分隔符?所以结果应该是:

rules_number | values
           2 | test1, test2

1 个答案:

答案 0 :(得分:1)

SELECT 
xmlcast(xmlquery('string-join(/a/b/ruleList/number/text(),",")' passing b.response returning content)as varchar2(1000)) rules_number,
xmlcast(xmlquery('string-join(/a/b/ruleList/rule[*]/name/text(),",")' passing b.response returning content)as varchar2(1000)) values
FROM test b
  • /text()正在提取节点非完整节点的值
  • string-join - 使用分隔符
  • 连接字符串序列

在您的情况下更好的选择是xmltable。

select * from   xmltable( '/a/b/ruleList' passing xmltype( '<a>
  <b>

    <ruleList>
      <number>2</number>
      <rule>
        <name>test1</name>
      </rule>
      <rule>
        <name>test2</name>
      </rule>
    </ruleList>
  </b>
</a>') 
columns 
rules_number varchar2(1000) path 'string-join(number/text(),",")',
"values" varchar2(1000) path 'string-join(rule/name/text(),",")'
);