I am trying to create an XPath query that will get 549 characters of text every time. The text should be about the related subject, in the example below it is oranges
or apples
or pears
. If there doesn't exist elements on the page that contain these words, then I would like the XPath query to find easier to target / less specific text on the page.
So to clarify, I am trying to create an XPath query that finds elements that contain a particular kind of text, if 549 or more characters are found using the query below, then we are done, if none is found or if the text returned is less than 549 characters, I would like the XPath query to get ANY text on the page that is in paragraph form (anything will work except text from buttons, links, menu's, etc.) and return 549 characters of this text, if the resulting string is less than 549 characters I would like to concatenate these two queries with the the following: ...
in the middle.
substring(normalize-space(//*[self::p or self::div][contains(text(),'apples') or contains(text(),'oranges') or contains(text(),'pears')]), 0, 549)
I have been trying to work this out for quite a while and I would appreciate any suggestions!
Many thanks in advance!