Oracle-XML:SQL查询中的XMLAGG / XMLELEMENT / XMLATTRIBUTES按特定字段

时间:2016-04-20 21:40:58

标签: sql arrays json xml aggregate

以下SQL:

SELECT
XMLELEMENT("classics", xmlattributes('xxxxxxxxx' AS "eventId"),             
XMLELEMENT("author",xmlattributes(FIRST_NAME AS "firstName", LAST_NAME AS "lastName", BIRTH AS "dob", DEATH AS "dod" )),
XMLELEMENT("bibliography",
XMLELEMENT("type",xmlattributes(DESCRIPTION AS "desc"),
XMLELEMENT("award", xmlattributes(NOBEL AS "nobelPrize"))),
XMLELEMENT("books",xmlattributes(BOOK_TITLE AS "title", PUBLISHED_DATE AS "published" )))) 
FROM CLASSICS 
WHERE AUTHOR_ID=23;

没有正确按书分组,我想要实现的是将此XML作为结果:

<?xml version="1.0" encoding="UTF-8"?>
<classics  eventId="234567890">
   <author firstName="Ernest " lastName="Hemingway" dob="1899-07-21" dod="1961-07-02" />
   <bibliography>
      <inner>
         <type desc="Novel" >
            <award nobel="true"  />
         </type>
         <books>
            <inner title="The Old Man And The Sea" published="1952" />
            <inner title="For Whom The Bell Tolls" published="1940" />
            <inner title="A Farewell To Arms" published="1929" />
         </books>
      </inner>
   </bibliography>
</classics>

目前我得到3条记录 - 每本书都有3个不同的XML - 我尝试使用XMLAGG按AUTHOR_id进行分组(此字段是表CLASSICS表中每本书的链接,用于authir) - 它是一个非常简单的结构ONE AUTHOR已经出版了至少一本书或者很多 - 而且我需要为包含另一个数组“书目”中的书籍数组的作者存储“经典”xml对象

这是我试图在XML“书目”的XMLELEMENT“参考书目”中使用的代码:没有运气

选择 XMLAGG( XMLELEMENT( “书”, xmlattributes(BOOK_TITLE AS“title”,PUBLISHED_DATE AS“已发布”) )来自CLASSICS GROUP BY AUTHOR_ID

最终的主要目标是在获得XML之后达到此JSON结构:

{
   "eventId": "234567890",
   "author": {
      "firstName": "Ernest",
      "lastName": "Hemingway",
      "dob": "1899-07-21",
      "dod": "1961-07-02"
   },
   "bibliography": [
      {
         "type": {
            "desc": "Novel",
            "award": {
               "nobelPrize": "1954"
            }
         },
         "books": [
            {
               "title": "The Old Man And The Sea",
               "published": "1952"
            },
            {
               "title": "For Whom The Bell Tolls",
               "published": "1940"
            },
            {
               "title": "A Farewell To Arms",
               "published": "1929"
            }
         ]
      }
   ]
}

但似乎很安静复杂地声明XML / SQL中的对象数组。

有什么建议吗?

0 个答案:

没有答案