I've tried the solution here: Getting attribute using XPath but it gives me an error.
I have some XHTML like this:
<a href="link.php">Click me!</a>
I'm recursively parsing the XML and trying to get both the href
attribute (link.php
) and the link text (Click me!
) at the same time.
<?php
$node = $xpath->query('string(self::a/@href) | self::a/text()', $nodes->item(0));
This code throws the following error:
Warning: DOMXPath::query(): Invalid type
If I do either of these two separately they work, but not together:
<?php
$node = $xpath->evaluate('string(self::a/@href)', $nodes->item(0));
$node = $xpath->query('self::a/text()', $nodes->item(0));
If I use the following I get the whole attribute (href="link.php"
), not just its value:
<?php
$node = $xpath->query('self::a/@href | self::a/text()', $nodes->item(0));
Is there any way of getting both text values at the same time using XPath 1.0 in PHP?