如何在XMLTYPE oracle中提取包含空格的文本

时间:2014-06-19 11:43:30

标签: oracle oracle11g xquery xmltype

当我使用以下查询时,

SELECT XMLCast(XMLQuery('/Books/Book' PASSING XMLCOLUMN RETURNING content)
               AS CLOB) "BOOK"  FROM samplexml;

对照下面的xml,

<books>
       <book>     <title>basics</title> <price>10</price> </book>
</books>

我希望将数据作为

返回

basics 10,由于标题标签和价格标签之间的距离, 但相反,我得到了 basics10

编辑: This is just an example, There may be any number of tags inside them. But I want the spaces to be included even if they are between two tags

2 个答案:

答案 0 :(得分:0)

试试这个

SELECT x.title || ' ' || x.price
  FROM samplexml s,
   XMLTable('/books/book' passing XMLTYPE('<books><book>     <title>basics</title> <price>10</price> </book></books>') 
         COLUMNS title VARCHAR2(150) PATH '/*/./title',
                 price VARCHAR2(1000) path '/*/price') x

进入XMLTible,从samplexml表中输入列的名称,例如:如果列名为xdata 变换是这样的

SELECT x.title || ' ' || x.price
  FROM samplexml s,
   XMLTable('/books/book' passing XMLTYPE(s.xdata) 
         COLUMNS title VARCHAR2(150) PATH '/*/./title',
                 price VARCHAR2(1000) path '/*/price') x

答案 1 :(得分:0)

循环书籍,并返回字符串而不是可以任意连接的书籍。

SELECT XMLCast(XMLQuery('for $book in /Books/Book return concat($book/title, ' ', $book/price)' PASSING XMLCOLUMN RETURNING content)
               AS CLOB) "BOOK"  FROM samplexml;