2

What is the correct way to remove all child nodes of the root node with a missing attribute from an xml field in Sql Server 2008?

My Xml looks like this, I want to remove all the child nodes of <root> that don't have the ln attribute specified

<root>
  <title />
  <title />
  <questionphrase ln="nl">
    <xhtml />
  </questionphrase>
  <questionphrase ln="en">
    <xhtml />
  </questionphrase>
  <information ln="nl">
    <xhtml />
  </information>
  <information ln="en">
    <xhtml />
  </information>
  <title />
  <title ln="en">
     value
  </title>
  <label ln="en">
     value
  </label>
  <title />
  <title />
</root>

After the delete the xml should look like this

<root>
  <questionphrase ln="nl">
    <xhtml />
  </questionphrase>
  <questionphrase ln="en">
    <xhtml />
  </questionphrase>
  <information ln="nl">
    <xhtml />
  </information>
  <information ln="en">
    <xhtml />
  </information>
  <title ln="en">
     value
  </title>
  <label ln="en">
     value
  </label>
</root>
2
  • Are you wanting to permanently remove them in the table, or just hide them during a SELECT? Commented Jan 29, 2013 at 9:51
  • I want to completely remove them, it's corrupt data that only causes overhead. Commented Jan 29, 2013 at 9:52

1 Answer 1

5

Try this:

DECLARE @xml XML = '....'
SET @xml.modify('delete //root/*[not(@ln)]')

SQL FIDDLE DEMO

Sign up to request clarification or add additional context in comments.

Comments

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.