Delete node/element but not its child

Questions about XML that are not covered by the other forums should go here.
StefanG
Posts: 7

Delete node/element but not its child

Wed Dec 05, 2012 5:03 pm

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

Re: Delete node/element but not its child

Wed Dec 12, 2012 1:47 am

*push*
adrian
Posts: 2388

Re: Delete node/element but not its child

Fri Dec 14, 2012 5:36 pm

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

Return to “General XML Questions”

Who is online

Users browsing this forum: No registered users and 0 guests