【问题标题】:How to insert new line when generating xml from SQLPLUS从 SQLPLUS 生成 xml 时如何插入新行
【发布时间】:2013-01-07 13:52:59
【问题描述】:

使用生成的XML

XMLAgg(XMLElement('student', ...)...)

将所有内容吐出一行。鉴于我有一个非常大的表,它在假脱机时达到了行长限制。

我想将每个 ... 节点放在单独的行上。 This page 建议使用 XMLText(x'0A') 插入新行,但 SQLPlus 似乎无法识别它。

我已经试过了:

set long 2000000000
set linesize 32767
set wrap on
set trimspool on

【问题讨论】:

  • 什么 oracle 版本作为 xmlserialize with indent 仅适用于 11g
  • Oracle 数据库 10g 企业版版本 10.2.0.4.0 - 产品

标签: xml oracle sqlplus


【解决方案1】:

10g 中的常用技巧是在您正在执行的外部 xml 操作上添加 .extract('/*'),例如

xmlagg(....).extract('/*')

但这在 11g 中不起作用。对于使用 xsl 转换的交叉版本兼容的版本,请参阅Generate XML file with Customized XML tags out of oracle database table

10.2.0.4:

SQL> create table foo (id) as select rownum from dual connect by level <= 2;

Table created.

SQL> select xmlagg(xmlelement("id", xmlelement("id2", id))).extract('/*') a from foo;

A
--------------------------------------------------------------------------------
<id>
  <id2>1</id2>
</id>
<id>
  <id2>2</id2>
</id>

SQL> select xmlserialize(content xmlagg(xmlelement("id", xmlelement("id2", id))).extract('/*') indent) a from foo;
select xmlserialize(content xmlagg(xmlelement("id", xmlelement("id2", id))).extract('/*') indent) a from foo
                                                                                                *
ERROR at line 1:
ORA-00907: missing right parenthesis


SQL> select xmlagg(xmlelement("id", xmlelement("id2", id))).transform(xmltype('<xsl:stylesheet version="1.0"
  2   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  3   <xsl:output omit-xml-declaration="yes" indent="yes"/>
  4   <xsl:template match="node()|@*">
  5    <xsl:copy>
  6     <xsl:apply-templates select="node()|@*"/>
  7    </xsl:copy>
  8   </xsl:template>
  9  </xsl:stylesheet>')) a from foo;

A
--------------------------------------------------------------------------------
<id>
  <id2>1</id2>
</id>
<id>
  <id2>2</id2>
</id>

和 11.2.0.2/3:

SQL> select xmlagg(xmlelement("id", xmlelement("id2", id))).extract('/*') a from foo;

A
--------------------------------------------------------------------------------
<id><id2>1</id2></id><id><id2>2</id2></id>

SQL> select xmlserialize(content xmlagg(xmlelement("id", xmlelement("id2", id))).extract('/*') indent) a from foo;

A
--------------------------------------------------------------------------------
<id>
  <id2>1</id2>
</id>
<id>
  <id2>2</id2>
</id>

SQL> select xmlagg(xmlelement("id", xmlelement("id2", id))).transform(xmltype('<xsl:stylesheet version="1.0"
  2   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  3   <xsl:output omit-xml-declaration="yes" indent="yes"/>
  4   <xsl:template match="node()|@*">
  5    <xsl:copy>
  6     <xsl:apply-templates select="node()|@*"/>
  7    </xsl:copy>
  8   </xsl:template>
  9  </xsl:stylesheet>')) a from foo;

A
--------------------------------------------------------------------------------

<id>
 <id2>1</id2>
</id>
<id>
 <id2>2</id2>
</id>

简而言之,要执行此版本不可知论,您应该使用 XSL。如果您只是尝试将其用于临时内容,那么extract 在 10g 上的输入更短,xmlserialize 在 11g 上更短。

【讨论】:

  • 10g 部分的临时解决方案效果很好,谢谢。我没有 11g 环境,所以无法验证那部分。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-19
  • 1970-01-01
相关资源
最近更新 更多