Find text OUTSIDE specific element

Oxygen general issues.
kirstendehaan
Posts: 4
Joined: Mon May 04, 2015 5:57 pm

Find text OUTSIDE specific element

Post by kirstendehaan »

Hi there,

is it possible, using XPath in the Find/Replace dialog, to find and replace text that is OUTSIDE a specific element?

For example, in a certain file I would like to find all instances of φ that are not marked as <span class="dummy-formula">, and then replace them with <span class="dummy-formula">φ</span>.

So far, I have only been able to get XPath to find text INSIDE certain elements (for example: //span[@class='dummy-formula']) but now I need to do exactly the opposite. Is this possible?

Many thanks,
Kirsten
adrian
Posts: 2879
Joined: Tue May 17, 2005 4:01 pm

Re: Find text OUTSIDE specific element

Post by adrian »

Hi,

You can use an XPath that looks for text whose parent is NOT span:

Code: Select all

//text()[not(parent::span)]
Or you can go further and ensure that the text is not nested inside other elements that are inside a 'span', so check that its ancestors do not include span:

Code: Select all

//text()[not(ancestor-or-self::span)]
You can also add the @class atribute check for the span element.
Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
kirstendehaan
Posts: 4
Joined: Mon May 04, 2015 5:57 pm

Re: Find text OUTSIDE specific element

Post by kirstendehaan »

Thanks a lot!
kirstendehaan
Posts: 4
Joined: Mon May 04, 2015 5:57 pm

Re: Find text OUTSIDE specific element

Post by kirstendehaan »

Hi Adrian,

thanks again for the reply.
However, I have a follow-up question:

I need to find the instances of φ in my files that are not in any element with class="dummy-formula". In our files, certain parts have been marked as mathematical formules using <span class="dummy-formula"> or <p class="dummy-formula">.

I need to find the instances that have not been marked as formulas so I can correct any mistakes. Is it possible to do this by adjusting the solution you gave me? I have tried but failed.

Thanks!
Kirsten
adrian
Posts: 2879
Joined: Tue May 17, 2005 4:01 pm

Re: Find text OUTSIDE specific element

Post by adrian »

Hi,
kirstendehaan wrote:I need to find the instances of φ in my files that are not in any element with class="dummy-formula". In our files, certain parts have been marked as mathematical formules using <span class="dummy-formula"> or <p class="dummy-formula">.
Here's what you need. This also looks in ancestor elements (not just in parent elements):

Code: Select all

//text()[not(ancestor-or-self::*[@class="dummy-formula"])]
This looks for text nodes that do not have ancestor elements with the @class="dummy-formula" attribute. If you only expect the attribute to be directly on the parent element, use parent:: instead of ancestor-or-self::.

Regards,
Adrian
Adrian Buza
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
Post Reply