Page 1 of 1

Delete node/element but not its child

Posted: Wed Dec 05, 2012 5:03 pm
by StefanG
I need to delete certain nodes in an xml document without deleting their childs.

Here a simplified sample. In this example I want to delete all span elements that have a img child (somewhere).

Before:

Code: Select all

<body>
<p><span><img src="test.png"/></span></p>
<p><span class="foo"><img src="test.png"/></span></p>
<p><span class="test"><b><img src="test.png"/></b></span></p>
<p><span class="test"><b><i><img src="test.png"/></i></b></span></p>
<p>Please open menu <span class="uistring">File</span> and select ...</p>
</body>
After:

Code: Select all

<body>
<p><img src="test.png"/></p>
<p><img src="test.png"/></p>
<p><b><img src="test.png"/></b></p>
<p><b><i><img src="test.png"/></i></b></p>
<p>Please open menu <span class="uistring">File</span> and select ...</p>
</body>
I can regex search for .* with xpath //span[descendant::*/img] but i will always also delete all childs of the node found. And that's exactly what I now want to do.

That is, basically it should work like placing the cursor in a span tag and select Refactoring - Delete Element Tags (Alt-Shift-X).

How can I do this (without XSLT)?

(Samples are for demonstration purposes only not the actual code.)

Re: Delete node/element but not its child

Posted: Wed Dec 12, 2012 1:47 am
by StefanG
*push*

Re: Delete node/element but not its child

Posted: Fri Dec 14, 2012 5:36 pm
by adrian
Hi,

Apologies for the late reply.

You can accomplish this in the Find/Replace dialog from Oxygen with the following:
Text to find: <span.*?>(.*?)</span>
Replace with: $1
XPath: //span[descendant-or-self::img]
Regular expression
Dot matches all (if the span element spreads to multiple lines)

Regards,
Adrian