February 28, 2014 at 5:18 am
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>
'
[p]set @xdata.modify('delete //step[fn:empty(.) = xs:boolean("true")]')
[/p]
Many Thanks.
February 28, 2014 at 10:02 am
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())]')
February 28, 2014 at 10:21 am
Many thanks arthurolcot, it worked! I appreciate your effort.
Nice one and have a lovely weekend!
Best Regards.
Viewing 3 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy