Ok, here's a function that extends XPath syntax with the following special characters:
~ inserts default namespace prefix if defined
# shorthand for text()
% shorthand for comment()
$ shorthand for node()
?* shorthand for processing-instruction()
?foo shorthand for processing-instruction("foo")
? shorthand for processing-instruction("")
^ escapes following character (with literal or SGML entity as needed)
All of the above except ^ are ignored within quoted strings
<?php
function extendXPath($str, $defns = NULL) {
$quote = false;
$map = array(
'~' => isset($defns) ? "$defns:" : '',
'#' => 'text()',
'%' => 'comment()',
'$' => 'node()'
);
$out = '';
for ($i = 0, $len = strlen($str); $i < $len; $i++) {
$c = $str[$i];
if (!$quote && array_key_exists($c, $map)) {
$out .= $map[$c];
} else switch ($c) {
case '^':
$out .= htmlspecialchars($str[++$i], ENT_QUOTES);
break;
case '?':
if ($quote) {
$out .= $c;
} elseif ($str[$i + 1] == '*') {
$out .= 'processing-instruction()';
$i++;
} else {
preg_match('/^\w+/', substr($str, $i + 1), $matches);
$out .= 'processing-instruction("'.$matches[0].'")';
$i += strlen($matches[0]);
};
break;
case '"':
$quote = !$quote;
default:
$out .= $c;
};
};
return $out;
}
?>