Created
November 12, 2012 16:38
-
-
Save Svel/4060375 to your computer and use it in GitHub Desktop.
Funny tests with xPath axes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * http://habrahabr.ru/post/52680/ | |
| * http://stackoverflow.com/questions/453191/select-first-instance-only-with-xpath/ | |
| */ | |
| $doc = new DomDocument('UTF-8', '1.0'); | |
| $doc->load(__DIR__ . '/xml.xml'); | |
| $path = new DomXPath($doc); | |
| // Tests | |
| echo "1: '/html/td/td[1]/a'", PHP_EOL; | |
| $result = $path->query('/html/td/td[1]/a'); | |
| echo ' ', (int) (3 == $result->length), PHP_EOL; | |
| echo ' ', (int) ('td1link1' == $result->item(0)->textContent), PHP_EOL; | |
| echo ' ', (int) ('td1link2' == $result->item(1)->textContent), PHP_EOL; | |
| echo ' ', (int) ('link2' == $result->item(2)->textContent), PHP_EOL; | |
| echo "2: '/html/td/td[3]/a'", PHP_EOL; | |
| $result = $path->query('/html/td/td[3]/a'); | |
| echo ' ', (int) (2 == $result->length), PHP_EOL; | |
| echo ' ', (int) ('td3link1' == $result->item(0)->textContent), PHP_EOL; | |
| echo ' ', (int) ('link4' == $result->item(1)->textContent), PHP_EOL; | |
| echo "3: '/html/td/td[3]/descendant::a'", PHP_EOL; | |
| $result = $path->query('/html/td/td[3]/descendant::a'); | |
| echo ' ', (int) (8 == $result->length), PHP_EOL; | |
| echo ' ', (int) ('td3link1' == $result->item(0)->textContent), PHP_EOL; | |
| echo ' ', (int) ('td31link1' == $result->item(1)->textContent), PHP_EOL; | |
| echo ' ', (int) ('td31link2' == $result->item(2)->textContent), PHP_EOL; | |
| echo ' ', (int) ('td32link1' == $result->item(3)->textContent), PHP_EOL; | |
| echo ' ', (int) ('td32link2' == $result->item(4)->textContent), PHP_EOL; | |
| echo ' ', (int) ('td33link1' == $result->item(5)->textContent), PHP_EOL; | |
| echo ' ', (int) ('td33link2' == $result->item(6)->textContent), PHP_EOL; | |
| echo ' ', (int) ('link4' == $result->item(7)->textContent), PHP_EOL; | |
| echo "4: '/html/td/descendant-or-self::td[1]/a'", PHP_EOL; | |
| $result = $path->query('/html/td/descendant-or-self::td[1]/a'); | |
| echo ' ', (int) (2 == $result->length), PHP_EOL; | |
| echo ' ', (int) ('link0' == $result->item(0)->textContent), PHP_EOL; | |
| echo ' ', (int) ('link1' == $result->item(1)->textContent), PHP_EOL; | |
| echo "5: '/html/td/descendant-or-self::td[3]/a'", PHP_EOL; | |
| $result = $path->query('/html/td/descendant-or-self::td[3]/a'); | |
| echo ' ', (int) (3 == $result->length), PHP_EOL; | |
| echo ' ', (int) ('td2link1' == $result->item(0)->textContent), PHP_EOL; | |
| echo ' ', (int) ('td2link2' == $result->item(1)->textContent), PHP_EOL; | |
| echo ' ', (int) ('link3' == $result->item(2)->textContent), PHP_EOL; | |
| echo "6: '/html/td/descendant-or-self::td[3]/descendant::a'", PHP_EOL; | |
| $result = $path->query('/html/td/descendant-or-self::td[3]/descendant::a'); | |
| echo ' ', (int) (3 == $result->length), PHP_EOL; | |
| echo ' ', (int) ('td2link1' == $result->item(0)->textContent), PHP_EOL; | |
| echo ' ', (int) ('td2link2' == $result->item(1)->textContent), PHP_EOL; | |
| echo ' ', (int) ('link3' == $result->item(2)->textContent), PHP_EOL; | |
| echo "7: '/html/td/descendant::td[3]/a'", PHP_EOL; | |
| $result = $path->query('/html/td/descendant::td[3]/a'); | |
| echo ' ', (int) (2 == $result->length), PHP_EOL; | |
| echo ' ', (int) ('td3link1' == $result->item(0)->textContent), PHP_EOL; | |
| echo ' ', (int) ('link4' == $result->item(1)->textContent), PHP_EOL; | |
| echo "8: '/html/td/descendant::td[3]/descendant::a'", PHP_EOL; | |
| $result = $path->query('/html/td/descendant::td[3]/descendant::a'); | |
| echo ' ', (int) (8 == $result->length), PHP_EOL; | |
| echo ' ', (int) ('td3link1' == $result->item(0)->textContent), PHP_EOL; | |
| echo ' ', (int) ('td31link1' == $result->item(1)->textContent), PHP_EOL; | |
| echo ' ', (int) ('td31link2' == $result->item(2)->textContent), PHP_EOL; | |
| echo ' ', (int) ('td32link1' == $result->item(3)->textContent), PHP_EOL; | |
| echo ' ', (int) ('td32link2' == $result->item(4)->textContent), PHP_EOL; | |
| echo ' ', (int) ('td33link1' == $result->item(5)->textContent), PHP_EOL; | |
| echo ' ', (int) ('td33link2' == $result->item(6)->textContent), PHP_EOL; | |
| echo ' ', (int) ('link4' == $result->item(7)->textContent), PHP_EOL; | |
| echo "9: '/html/td//descendant-or-self::td[3]/a'", PHP_EOL; | |
| $result = $path->query('/html/td//descendant-or-self::td[3]/a'); | |
| echo ' ', (int) (5 == $result->length), PHP_EOL; | |
| echo ' ', (int) ('td2link1' == $result->item(0)->textContent), PHP_EOL; | |
| echo ' ', (int) ('td2link2' == $result->item(1)->textContent), PHP_EOL; | |
| echo ' ', (int) ('td32link1' == $result->item(2)->textContent), PHP_EOL; | |
| echo ' ', (int) ('td32link2' == $result->item(3)->textContent), PHP_EOL; | |
| echo ' ', (int) ('link3' == $result->item(4)->textContent), PHP_EOL; | |
| echo "10: '//td[3]/a'", PHP_EOL; | |
| $result = $path->query('//td[3]/a'); | |
| echo ' ', (int) (5 == $result->length), PHP_EOL; | |
| echo ' ', (int) ('td3link1' == $result->item(0)->textContent), PHP_EOL; | |
| echo ' ', (int) ('td33link1' == $result->item(1)->textContent), PHP_EOL; | |
| echo ' ', (int) ('td33link2' == $result->item(2)->textContent), PHP_EOL; | |
| echo ' ', (int) ('link1' == $result->item(3)->textContent), PHP_EOL; | |
| echo ' ', (int) ('link4' == $result->item(4)->textContent), PHP_EOL; | |
| echo "11: '//descendant::td[3]/a'", PHP_EOL; | |
| $result = $path->query('//descendant::td[3]/a'); | |
| echo ' ', (int) (6 == $result->length), PHP_EOL; | |
| echo ' ', (int) ('td2link1' == $result->item(0)->textContent), PHP_EOL; | |
| echo ' ', (int) ('td2link2' == $result->item(1)->textContent), PHP_EOL; | |
| echo ' ', (int) ('td3link1' == $result->item(2)->textContent), PHP_EOL; | |
| echo ' ', (int) ('td33link1' == $result->item(3)->textContent), PHP_EOL; | |
| echo ' ', (int) ('td33link2' == $result->item(4)->textContent), PHP_EOL; | |
| echo ' ', (int) ('link4' == $result->item(5)->textContent), PHP_EOL; | |
| echo "12: '//descendant-or-self::td[3]/descendant::a'", PHP_EOL; | |
| $result = $path->query('//descendant-or-self::td[3]/descendant::a'); | |
| echo ' ', (int) (5 == $result->length), PHP_EOL; | |
| echo ' ', (int) ('td2link1' == $result->item(0)->textContent), PHP_EOL; | |
| echo ' ', (int) ('td2link2' == $result->item(1)->textContent), PHP_EOL; | |
| echo ' ', (int) ('td32link1' == $result->item(2)->textContent), PHP_EOL; | |
| echo ' ', (int) ('td32link2' == $result->item(3)->textContent), PHP_EOL; | |
| echo ' ', (int) ('link3' == $result->item(4)->textContent), PHP_EOL; | |
| echo "13: '/html/td[3]/td[1]/a[1]'", PHP_EOL; | |
| $result = $path->query('/html/td[3]/td[1]/a[1]'); | |
| echo ' ', (int) (1 == $result->length), PHP_EOL; | |
| echo ' ', (int) ('link2' == $result->item(0)->textContent), PHP_EOL; | |
| echo "14: '/html/descendant-or-self::td[3]/a'", PHP_EOL; | |
| $result = $path->query('/html/descendant-or-self::td[3]/a'); | |
| echo ' ', (int) (2 == $result->length), PHP_EOL; | |
| echo ' ', (int) ('td2link1' == $result->item(0)->textContent), PHP_EOL; | |
| echo ' ', (int) ('td2link2' == $result->item(1)->textContent), PHP_EOL; | |
| echo "15: '/html/descendant-or-self::td[3]/descendant::a'", PHP_EOL; | |
| $result = $path->query('/html/descendant-or-self::td[3]/descendant::a'); | |
| echo ' ', (int) (2 == $result->length), PHP_EOL; | |
| echo ' ', (int) ('td2link1' == $result->item(0)->textContent), PHP_EOL; | |
| echo ' ', (int) ('td2link2' == $result->item(1)->textContent), PHP_EOL; | |
| echo "16: '/html/descendant::td[3]/descendant::a'", PHP_EOL; | |
| $result = $path->query('/html/descendant::td[3]/descendant::a'); | |
| echo ' ', (int) (2 == $result->length), PHP_EOL; | |
| echo ' ', (int) ('td2link1' == $result->item(0)->textContent), PHP_EOL; | |
| echo ' ', (int) ('td2link2' == $result->item(1)->textContent), PHP_EOL; | |
| echo "17: '/html/descendant-or-self::td[3]/descendant::td/a'", PHP_EOL; | |
| $result = $path->query('/html/descendant-or-self::td[3]/descendant::td/a'); | |
| echo ' ', (int) (0 == $result->length), PHP_EOL; | |
| echo "18: '/html/descendant-or-self::td/td/a'", PHP_EOL; | |
| $result = $path->query('/html/descendant-or-self::*/td[3]/a'); | |
| echo ' ', (int) (5 == $result->length), PHP_EOL; | |
| echo ' ', (int) ('td3link1' == $result->item(0)->textContent), PHP_EOL; | |
| echo ' ', (int) ('td33link1' == $result->item(1)->textContent), PHP_EOL; | |
| echo ' ', (int) ('td33link2' == $result->item(2)->textContent), PHP_EOL; | |
| echo ' ', (int) ('link1' == $result->item(3)->textContent), PHP_EOL; | |
| echo ' ', (int) ('link4' == $result->item(4)->textContent), PHP_EOL; | |
| echo "19: '/html/td[3]/a'", PHP_EOL; | |
| $result = $path->query('/html/td[3]/a'); | |
| echo ' ', (int) (1 == $result->length), PHP_EOL; | |
| echo ' ', (int) ('link1' == $result->item(0)->textContent), PHP_EOL; | |
| echo "20: '/html/descendant-or-self::td[3]/a[1]'", PHP_EOL; | |
| $result = $path->query('/html/descendant-or-self::td[3]/a[1]'); | |
| echo ' ', (int) (1 == $result->length), PHP_EOL; | |
| echo ' ', (int) ('td2link1' == $result->item(0)->textContent), PHP_EOL; | |
| echo "DONE", PHP_EOL; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" ?> | |
| <html> | |
| <td> | |
| <a>link0</a> | |
| <td>td1 <a>td1link1</a><a>td1link2</a></td> | |
| <td>td2 <a>td2link1</a><a>td2link2</a></td> | |
| <td>td3 | |
| <a>td3link1</a> | |
| <td>td31 | |
| <a>td31link1</a> | |
| <a>td31link2</a> | |
| </td> | |
| <td>td32 | |
| <a>td32link1</a> | |
| <a>td32link2</a> | |
| </td> | |
| <td>td33 | |
| <a>td33link1</a> | |
| <a>td33link2</a> | |
| </td> | |
| </td> | |
| </td> | |
| <td> <x/></td> | |
| <td> | |
| <a>link1</a> | |
| <td><a>link2</a></td> | |
| <td><a>link3</a></td> | |
| <td><a>link4</a></td> | |
| </td> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment