SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


XQuery: Deleting Empty Nodes From XML Document


XQuery: Deleting Empty Nodes From XML Document

Author
Message
ekeopara
ekeopara
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
Points: 14 Visits: 79
Hi peops,

I am new to XQuery and have a problem. I want to delete a child element (in this case called 'step') if and only if it is empty i.e. <step/>. So if there are 3 step elements and the last 2 are empty, then only these 2 elements should be deleted. In effect, the position of this child element is irrelevant. But at moment, I can only delete either all step elements or specified ones. I want to be able to delete the step child element regardless of it's ordinal position. Help any one...please? Unsure

Below is the sample document and code.

declare @xdata as xml = '
<root>
<location locationid="10">
<step>Alpha</step>
<step />
<step>Beta</step>
</location>
<location locationid="20">
<step />
<step />
<step>Gamma</step>
</location>
<location locationid="30">
<step />
<step>prime</step>
</location>
</root>
'


set @xdata.modify('delete //step[fn:empty(.) = xs:boolean("true")]')




Many Thanks.
Arthur Olcot
Arthur Olcot
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3192 Visits: 1779
Hi.. I would use something like the following. This is checking to see if the text node is empty or not in the predicate.:


set @xdata.modify('delete //step[empty(text())]')


ekeopara
ekeopara
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
Points: 14 Visits: 79
Many thanks arthurolcot, it worked! I appreciate your effort.
Nice one and have a lovely weekend! :-D

Best Regards.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search