-1

Well the htmo code is something like this:

  <a href="javascript:my_win('.......'">
<img src="...." border=0>
<font color="red">title
</a>
</font>

and I want to identify the color only for those a's which href contains th word:

javascript:my_win

This is my query:

$xpath->query('//a[contains(@href,"javascript:my_win")]/font');

but I get nothing.

If my query changes to this, I normally get all the hrefs, so there is no chance of mispelling.

$elements = $xpath->query('//a');

If my query changes to this Every colot is being printed out.

$elements = $xpath->query('//a/font');

Whole code is here:

$elements = $xpath->query('//a[contains(@href,"javascript:my_win")]/font');

foreach ( $elements as $element ) {

$str1=$element->getAttribute('color');

}
ghostrider
  • 4,917
  • 12
  • 63
  • 113
  • 3
    Your code contains syntax errors, see where the highlighting is broken above. – salathe Aug 26 '12 at 16:06
  • @ghostrider salathe means that you have a quote mismatch. It should be `$xpath->query('//a[contains(href,"javascript:my_win('http://www2.mysite.gr/test/form?')]/font");` not `$xpath->query('//a[contains(href,"javascript:my_win('http://www2.mysite.gr/test/form?")]/font');` – toniedzwiedz Aug 26 '12 at 16:13
  • There is a quoting mismatch in your code. You start with `"` but end with `'`. – O. R. Mapper Aug 26 '12 at 16:19
  • please see the edit because whatever I put in contains it does not play – ghostrider Aug 26 '12 at 16:22

1 Answers1

0

Use the @ character to refer to attributes in XPath:

//a[contains(@href, 'some required text')]

When just writing href, the XPath processor will look for a child element named <href> whose text contents includes the specified string.

O. R. Mapper
  • 18,591
  • 9
  • 58
  • 102
  • @ghostrider: Could you update your question with a well-formed relevant fragment of your XHTML code, please? – O. R. Mapper Aug 26 '12 at 16:28
  • @ghostrider: That's not well-formed XHTML. I'm surprised you're getting any results for `//a` and `//a/font` at all. – O. R. Mapper Aug 26 '12 at 16:37
  • it's not code written by me. Its from the site I want to grab the data. Is there sollution with this html? – ghostrider Aug 26 '12 at 16:40
  • @ghostrider: Usually XPath works only on well-formed Xml (such as XHTML). I can just guess PHP's XPath processor is making the best it can out of that HTML code and therefore behaves not quite as expected. Please have a look at question such as [this](http://stackoverflow.com/questions/354322/finding-a-node-or-close-to-it-using-xpath-in-non-well-formed-html) or [this](http://stackoverflow.com/questions/1148928/disable-warnings-when-loading-non-well-formed-html-by-domdocument-php) for possible pointers towards solutions. – O. R. Mapper Aug 26 '12 at 16:45