Page 1 of 1
Find text OUTSIDE specific element
Posted: Mon May 04, 2015 6:10 pm
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
Re: Find text OUTSIDE specific element
Posted: Mon May 04, 2015 7:44 pm
by adrian
Hi,
You can use an XPath that looks for text whose parent is NOT 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
Re: Find text OUTSIDE specific element
Posted: Wed May 06, 2015 1:21 pm
by kirstendehaan
Thanks a lot!
Re: Find text OUTSIDE specific element
Posted: Wed May 06, 2015 1:55 pm
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
Re: Find text OUTSIDE specific element
Posted: Wed May 06, 2015 2:34 pm
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