1

Oracle 11g2. For example:

select XMLType( 
'<main>
  <article> 
    <title>1.txt</title>
      <type>market</type>
      <author>
        <name>J. Smith</name>   
        <age>32</age>
      </author>
  </article>
  <comment>For information</comment>
</main>').getStringVal() from dual;

Could you help me modify this select statement ? I'd like to get varchar output as :

<title>1.txt</title>
<type>market</type>
<author>
  <name>J. Smith</name>   
  <age>32</age>
</author>

unfortunately, using extract('/main/article') as:

select XMLType( 
'<main>
  <article> 
    <title>1.txt</title>
      <type>market</type>
      <author>
        <name>J. Smith</name>   
        <age>32</age>
      </author>
  </article>
  <comment>For information</comment>
</main>').extract('/main/article').getStringVal() from dual;

lose format of the text (all output in one row). Thank you in advance.

1
  • Out of curiosity, why do you want to get the nodes without a root node?
    – Boneist
    Feb 10, 2016 at 15:12

2 Answers 2

2

You can do this (well, including the article node) via XMLTABLE and XMLSERIALISE to do the pretty printing, e.g.:

Query

with sample_data as (select XMLType( 
'<main>
  <article> 
    <title>1.txt</title>
      <type>market</type>
      <author>
        <name>J. Smith</name>   
        <age>32</age>
      </author>
  </article>
  <comment>For information</comment>
</main>') xdata from dual)
select xmlserialize(document x.col1 as varchar2(4000) indent size = 2)
from   sample_data sd
       cross join xmltable('/main/article'
                           passing sd.xdata
                           columns col1 xmltype path '.') x;

Result

<article>
  <title>1.txt</title>
  <type>market</type>
  <author>
    <name>J. Smith</name>
    <age>32</age>
  </author>
</article>
1

As @Boneist shows you can use xmlserialize() for the formatting, but as you only seem to have a single article node in your document you can still use your extract():

select xmlserialize(document XMLType( 
'<main>
  <article> 
    <title>1.txt</title>
      <type>market</type>
      <author>
        <name>J. Smith</name>   
        <age>32</age>
      </author>
  </article>
  <comment>For information</comment>
</main>').extract('/main/article')
  as varchar2(200) indent) as article
from dual;

Or you can use an XMLQuery:

select xmlserialize(document xmlquery('/main/article'
  passing XMLType( 
'<main>
  <article> 
    <title>1.txt</title>
      <type>market</type>
      <author>
        <name>J. Smith</name>   
        <age>32</age>
      </author>
  </article>
  <comment>For information</comment>
</main>')
  returning content)
  as varchar2(200) indent) as article
from dual;

ARTICLE                                                                        
--------------------------------------------------------------------------------
<article>                                                                       
  <title>1.txt</title>                                                          
  <type>market</type>                                                           
  <author>                                                                      
    <name>J. Smith</name>                                                       
    <age>32</age>                                                               
  </author>                                                                     
</article>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.