0

In my continuing saga of xpath and and extracting data I continue to struggle. I need just two values contained in a table cell. I can get to each individually, but I cannot access the other while there. I have cell's like so

<TR>
<TD width="120" align="center" valign="top">
<A href="http://www..yadayada.com"> <!--the href I need to extract-->
<IMG src="http://images.com/items/yada.gif" width="80" height="80" border="1"></A>
<BR>
<B>Random number PT</B><!--the text I need to extract-->
</TD>

I traverse like so:

@$dom = new DOMDocument();
@$dom->loadHTML( $rawPage );
@$xpath = new DOMXPath( $dom );
@$queryResult = $xpath->query( "..../tr/td[contains( b, 'PT' ) ]/b" );

to get to the href link and similary,

@$queryResult = $xpath->query( "..../tr/td[contains( b, 'PT' ) ]/a" );

to get the text I need. And then I extract like so

//for the text in b
foreach ( $queryResult as $result )
{
echo $result->textContent . " text content<br>";
}

and for the link

//for the text in href
foreach ( $queryResult as $result )
{
echo $result->getAttribute( 'href' ) . " href<br>";
}

I do not pull each TD in the table and that's why I match /td[contains( b, 'PT' ) ] ones that have PT in the . I've read about unions and using /td[contains( b, 'PT' ) ]/*[self::a or self::b but my for each errors with Invalid argument supplied for foreach()

I've tried using nextSibling and all that too and it just is blank when I echo it. So, how can I get the two values out of my tables?

2
  • In ..../tr/td[contains( b, 'PT' ) ]/b what is PT ? your html does not have any word PT. Commented Jul 25, 2012 at 16:36
  • @VamanKulkarni, I have corrected that in my post. Commented Jul 25, 2012 at 16:46

1 Answer 1

1

You can try

//td[contains( b, 'PT' ) ]

And

//td[contains( b, 'PT' ) ]/a

Two queries should work,
Using your existing code

queryResult = $xpath->query( "//td[contains( b, 'PT' ) ]" );
foreach ( $queryResult as $result )
{
  echo $result->textContent . " text content<br>";
}

$queryResult = $xpath->query( "//td[contains( b, 'PT' ) ]/a" );
foreach ( $queryResult as $result )
{
  echo $result->getAttribute( 'href' ) . " href<br>";
}
Sign up to request clarification or add additional context in comments.

2 Comments

I have did this, as part of the single xpath query, however, I don't know how to implement this. do I write two separate xpath queries?
The way you describe it, it posts the b and a separate. I simply moved the href loop inside the first in order to post the matching href with the corresponding b. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.