【问题标题】:Is there any way to fetch data in XML format from PostgreSQL table which has different field not the XML data type field?有没有办法从具有不同字段而不是 XML 数据类型字段的 PostgreSQL 表中获取 XML 格式的数据?
【发布时间】:2020-06-23 02:17:32
【问题描述】:

我想使用 JDBC 和 Postgresql 从数据库中获取 XML 格式的数据,但数据以 id、name、age 格式存储。

【问题讨论】:

    标签: sql xml postgresql


    【解决方案1】:

    另一种选择是使用 Saxon 的 XSLT 扩展:

    <persons>
       <sql:query connection="sql:connect(....)" table="t"/>
    </persons>
    

    http://www.saxonica.com/documentation/index.html#!sql-extension

    然后您将 XML 转换为 XSLT 样式表,该样式表可以将其操作为您真正想要的最终格式。

    【讨论】:

      【解决方案2】:

      欢迎来到 SO。

      使用xmlelementxmlforest 的组合,如下所示。

      数据样本

      CREATE TEMPORARY TABLE t 
      (id INTEGER, name TEXT, age INTEGER);
      INSERT INTO t VALUES (1,'John',25);
      INSERT INTO t VALUES (2,'Jane',22);
      

      查询:

      SELECT 
        xmlelement(name person,
          xmlforest(id, name, age) 
        )
      FROM t;
                              xmlelement                         
      -----------------------------------------------------------
       <person><id>1</id><name>John</name><age>25</age></person>
       <person><id>2</id><name>Jane</name><age>22</age></person>
      (2 Zeilen)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-19
        • 1970-01-01
        • 2019-05-04
        • 1970-01-01
        相关资源
        最近更新 更多