Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Query which deletes an attribute in XML

Tags:

sql

xml

oracle

I have column of XML type.There's a xml like this

<items>
   <item type="xxx"><items>
   <item type="xxx"><items>
</items>

I need to delete all type attributes. I know oracle has some functions for xml manipulation, but I don't get how to delete attributes.

How would such query look like ?

like image 976
user256034 Avatar asked Jul 07 '11 09:07

user256034


People also ask

How do I delete a row in XML?

To delete rows that contain XML documents, use the DELETE SQL statement. Include a WHERE clause when you want to delete specific rows. You can specify which rows are to be deleted based on values within XML columns. To find values within XML documents, you need to use XQuery expressions.

How can I get SQL query results in XML?

You can optionally retrieve formal results of a SQL query as XML by specifying the FOR XML clause in the query. The FOR XML clause can be used in top-level queries and in subqueries. The top-level FOR XML clause can be used only in the SELECT statement.

What SQL command deletes data from database?

The Delete command in SQL is a part of the Data Manipulation Language, a sub-language of SQL that allows modification of data in databases. This command is used to delete existing records from a table. Using this, you can either delete specific records based on a condition or all the records from a table.


2 Answers

Here is an example using the Oracle supplied SQL function deletexml

Acknowledgement to Jonas Lincoln as I am using his XPATH expression

SELECT deleteXML(xmltype.CREATEXML('<items>
                                       <item type="xxx">a</item>
                                       <item type="xxx">b</item>
                                   </items>'),
                                   '/items/item[@type="xxx"]/@type')
FROM dual  

<items>
   <item>a</item>
   <item>b</item>
</items>                                                         
like image 132
Ian Carpenter Avatar answered Oct 12 '22 05:10

Ian Carpenter


declare @xml as xml
set @xml = '
<items>
    <item type="xxx">3</item>
    <item type="xxx">4</item>
    </items>'

SET @xml.modify('delete (/items/item[@type="xxx"]/@type)')

select cast(@xml as nvarchar(100))

<items>
  <item>3</item>
  <item>4</item>
</items>
like image 28
Jonas Lincoln Avatar answered Oct 12 '22 03:10

Jonas Lincoln