Created
August 24, 2015 11:51
-
-
Save ThomasWeinert/71ba036c6afa863a9346 to your computer and use it in GitHub Desktop.
Using a callback to fetch the current context in an xpath expression, this might allow to implement the CSS selector `:scope`
This file contains 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 | |
$xml = file_get_contents('php://stdin'); | |
class MyXpath extends DOMXpath { | |
private static $_context = null; | |
public function __construct($document) { | |
parent::__construct($document); | |
$this->registerNamespace("php", "http://php.net/xpath"); | |
$this->registerPHPFunctions([self::CLASS.'::getContext']); | |
} | |
public function evaluate($expression, $context = NULL, $registerNamespaces = FALSE) { | |
self::$_context = $context; | |
return parent::evaluate($expression, $context, $registerNamespaces); | |
} | |
public function getContext() { | |
return self::$_context; | |
} | |
} | |
$document = new DOMDocument(); | |
$document->loadXml($xml); | |
$xpath = new MyXpath($document); | |
$xpath->registerPhpFunctions(['getContext']); | |
foreach ($xpath->evaluate('//item') as $item) { | |
var_dump($xpath->evaluate('name(//*/.)', $item)); | |
var_dump($xpath->evaluate('name(//*[. = php:function("MyXpath::getContext")])', $item)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment