tags:

views:

30

answers:

1

Given a string of text "my search phrase", inside an html content block, how would you determine if the string appears within the first 100 or last 100 non tag-based characters of the document? (Don't count html tags, only text nodes)

Example, this would return true...

<p>This html text block contains <b>My Search Phrase</b>, because it appears in the first 100 characters of the content.</p>

+3  A: 

how would you determine if the string appears within the first 100 or last 100 non tag-based characters of the document?

In the absence of any definition of "non-tag-based characters, I assume these are any characters of the string value of the document: string(/).

Therefore:

contains(substring(/,1,100),'my search phrase')

returns true() exactly when the first 100 characters of the string value of the document contain the string 'my search phrase'.

Similarly:

contains(substring(/, string-length(/)-99),'my search phrase')

returns true() exactly when the last 100 characters of the string value of the document contain the string 'my search phrase'.

Therefore:

  contains(substring(/,1,100),'my search phrase')
or
  contains(substring(/, string-length(/)-99),'my search phrase')

returns true() exactly when the first or last 100 characters of the string value of the document contain the string 'my search phrase'.

Dimitre Novatchev
Dimitre: +1 Really good answer!
Alejandro