1

I want a function to pass a text (varchar2) and a tag name and return the text with the tag and their child tags removed Keep only

I don't know how levels of childs there are under tag.

Input :

set serverouput on
declare
v_input_str  varchar(4000) := '        <parent1>
            <Message>12ab</Message>
            <CreationDate>02/08/2015</CreationDate>
            <Total>500.45</Total>
            <Collect>
                <Name>Peter</Name>
            </Collect>
        </parent1>';
begin
 dbms_output.put_line(v_input_str);

end;
/


output :
'        <parent1>
            <Message>12ab</Message>
            <CreationDate>02/08/2015</CreationDate>
            <Total>500.45</Total>
            <Collect/>
        </parent1>'

Thanks in advance

1 Answer 1

2

You can use deleteXML for this.

Assuming your parameter tag is stored in tag variable, do:

declare
    v_input_str  clob := '        <parent1>
            <Message>12ab</Message>
            <CreationDate>02/08/2015</CreationDate>
            <Total>500.45</Total>
            <Collect>
                <Name>Peter</Name>
            </Collect>
        </parent1>';
    tag varchar2(20) := 'Collect';
begin
    select deleteXML(xmltype(v_input_str), '//'||tag||'/*').getClobVal() into v_input_str from dual;
    dbms_output.put_line(v_input_str);
end;

Output:

<parent1><Message>12ab</Message><CreationDate>02/08/2015</CreationDate><Total>500.45</Total><Collect/></parent1>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer. It works for me . But alignment is not as the initial.
Why does alignment matter?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.