Monday, March 12, 2012

Is there query I can write to remove empty tags from a xml field?

I want to remove empty tags:

<name></name>

from an xml type field in a table. Can I do it using a query and a modify?

Use the modify method and the XQuery delete instruction as follows (uses an XML variable but can of course also be done with a column of type XML):

Code Snippet

DECLARE @.xml xml;

SET @.xml = '<root>

<name/>

<name></name>

<element>

<name />

</element>

</root>';

SET @.xml.modify('

delete //*[not(node())]

');

SELECT @.xml;

|||

PERFECT!!! I am thrilled. Easy and simple and solved my problem.

No comments:

Post a Comment