views:

35

answers:

2

How can I alter this xpath to find if the content contains an img tag with an alt containing my keyword phrase?

$xPath->evaluate('count(/html/body//'.$tag.'[contains(.,"'.$keyword.'")])');

A: 

I don't understand exactly what elements are you loooking for but there is the example, which returns all elements h1 which contains at least one image with keyword in alt:

//h1[.//img[constains(@alt, 'your_keyword')]]

You should also handle if it is case sensitive or not. You can use this xpath but be careful, some xpath evaluators doesn't support lower-case function.

//h1[.//img[constains(lower-case(@alt), lower-case('your_keyword'))]]

Here is example:

//h1[.//img[contains(@alt, 'key ')]]

<html>
    <h1> <!-- found -->
        <img alt='here is my key' />
    </h1>
    <h1><!-- not found -->
        <img alt='here is not' />
    </h1>
    <h1> <!-- found -->
        <h2>
            <img alt='the key is also here' />
        </h2>
    </h1>
    <h1></h1> <!-- not found -->

</html>
Gaim
Thanks Gaim, I'm looking to determine if at least one image within the content has an alt containing the keyword. I think I can take your example and go from there. Thanks a ton!
Scott B
@Scott-B: Please, edit your question and put your comment at the start of the text. Otherwise it makes little sense and doesn't contain a question at all.
Dimitre Novatchev
+1  A: 

Use:

boolean(//img[contains(@alt, 'yourKeywordHere')])

to find (true(), false()) whether there is an img element in the XML document whose alt attribute contains 'yourKeywordHere'.

Use:

boolean(//yourTag//img[contains(@alt, 'yourKeywordHere')])

to find if there is an element in the document named yourTag that has a descendent img whose alt attribute contains 'yourKeywordHere'.

Dimitre Novatchev
Thanks Dimitre. That makes perfect sense. Could I use xpath to determine if the keyword appears in the first and/or last sentence of the document? I'm thinking xpath would not recognize a "sentence" as an entity like a node.
Scott B
@Scott-B: You need to define "sentence" precisely.
Dimitre Novatchev
I don't see difference between my and your answer except 4 hours but never mind
Gaim
@Gaim: Your answer selects all `h1` elements that have a descendent `img` element whose `alt` attribute contains some specific string. Instead, what @Scott-B wants is to know just one bit of information -- `true()` or `false()` -- "*Is* there such an element". This is what I give him with my answer.
Dimitre Novatchev
@Dimitre: +1 Good answer.
Alejandro
@Scott-B: If by "sentence" you mean HTML "block element", then you could test for not preceding "block" elements (first test), or not following "block" element (last test).
Alejandro
@Scott-B: If by "sentence" you mean string tokenized by new line character, you could test for not preceding text nodes having a new line character or not following text nodes having a new line character.
Alejandro