-
Notifications
You must be signed in to change notification settings - Fork 7
xpath ancestor registration #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
tvdijen
merged 7 commits into
simplesamlphp:master
from
ioigoume:xpath-ancestor-xmlns-registration
Nov 14, 2025
Merged
xpath ancestor registration #74
tvdijen
merged 7 commits into
simplesamlphp:master
from
ioigoume:xpath-ancestor-xmlns-registration
Nov 14, 2025
Conversation
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
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #74 +/- ##
============================================
- Coverage 93.76% 93.41% -0.35%
- Complexity 881 913 +32
============================================
Files 212 212
Lines 3271 3328 +57
============================================
+ Hits 3067 3109 +42
- Misses 204 219 +15 🚀 New features to boost your workflow:
|
tvdijen
reviewed
Nov 13, 2025
tvdijen
reviewed
Nov 13, 2025
Member
|
I think it looks really good! |
Contributor
Author
|
For reference: private static function registerAncestorNamespaces(DOMXPath $xp, DOMNode $node): void
{
// Avoid re-binding while walking upwards.
$registered = [
'xml' => true,
'xs' => true,
];
// Start from the nearest element (or documentElement if a DOMDocument is passed).
$current = $node instanceof DOMDocument
? $node->documentElement
: ($node instanceof DOMElement ? $node : $node->parentNode);
// Normalize to the nearest element ancestor (in case $node is a text/attr/etc.)
while ($current !== null && !$current instanceof DOMElement) {
$current = $current->parentNode;
}
while ($current instanceof DOMElement) {
// Try to pick up xmlns declarations when they are exposed as attributes.
if ($current->hasAttributes()) {
foreach ($current->attributes as $attr) {
// Robustly detect namespace declarations:
// - prefixed declaration: xmlns:foo="uri" -> $attr->prefix === 'xmlns'
// - default declaration: xmlns="uri" -> $attr->nodeName === 'xmlns'
if ($attr->prefix !== 'xmlns' && $attr->nodeName !== 'xmlns') {
continue;
}
// 'foo' for xmlns:foo, 'xmlns' for default
$prefix = ($attr->nodeName === 'xmlns') ? 'xmlns' : $attr->localName;
$uri = (string) $attr->nodeValue;
if (
$prefix === null || $prefix === '' ||
$prefix === 'xmlns' || $uri === '' ||
isset($registered[$prefix])
) {
continue;
}
$xp->registerNamespace($prefix, $uri);
$registered[$prefix] = true;
}
}
// Fallback: bind prefixes in use by elements using their namespaceURI directly.
// This works even if the subtree is detached (no ancestor xmlns).
// - current element
if ($current->prefix !== '' && !isset($registered[$current->prefix])) {
$uri = $current->namespaceURI; // use the element's own ns URI
if (is_string($uri) && $uri !== '') {
$xp->registerNamespace($current->prefix, $uri);
$registered[$current->prefix] = true;
}
}
// attributes (non-xmlns) with prefixes
if ($current->hasAttributes()) {
foreach ($current->attributes as $attr) {
if (
$attr->prefix !== ''
&& $attr->prefix !== 'xmlns'
&& !isset($registered[$attr->prefix])
) {
$uri = $attr->namespaceURI;
if (is_string($uri) && $uri !== '') {
$xp->registerNamespace($attr->prefix, $uri);
$registered[$attr->prefix] = true;
}
}
}
}
// all descendant elements: pick prefixes seen anywhere below and bind via their namespaceURI
$desc = $current->getElementsByTagName('*');
/** @var \DOMElement $el */
foreach ($desc as $el) {
if ($el->prefix === '' || isset($registered[$el->prefix])) {
continue;
}
$uri = $el->namespaceURI;
if (is_string($uri) && $uri !== '') {
$xp->registerNamespace($el->prefix, $uri);
$registered[$el->prefix] = true;
}
}
// Go up one step; root's parent is DOMDocument, which stops the loop
$current = $current->parentNode;
}
} |
tvdijen
approved these changes
Nov 14, 2025
tvdijen
added a commit
that referenced
this pull request
Nov 14, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
No description provided.