Delete node/element but not its child

Questions about XML that are not covered by the other forums should go here.
StefanG
Posts: 7
Joined: Wed Aug 31, 2011 4:03 pm

Delete node/element but not its child

Post 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.)
StefanG
Posts: 7
Joined: Wed Aug 31, 2011 4:03 pm

Re: Delete node/element but not its child

Post by StefanG »

*push*
adrian
Posts: 2855
Joined: Tue May 17, 2005 4:01 pm

Re: Delete node/element but not its child

Post 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
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
Post Reply