Search code examples
phpsvgdomxpath

DOMXPath not working with valid expression


I've got the following SVG:

<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 18.1.1, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
     viewBox="-194.2 -103.1 178.2 480.9" enable-background="new -194.2 -103.1 178.2 480.9" xml:space="preserve">
<filter  height="140%" width="140%" id="Filter_1" y="-20%" x="-20%" color-interpolation-filters="sRGB">
    <feColorMatrix  type="matrix" result="result1" values="1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0" in="SourceGraphic">
        </feColorMatrix>
</filter>
<g id="color1">
    <g transform="matrix( 1, 0, 0, 1, -0.3,27) ">
        <g id="Layer_1_1_">
            <g transform="matrix( 1, 0, 0, 1, 0,0) ">
                <g>
                    <g>
                        <path fill="#999999" d="..."/>
                    </g>
                </g>
            </g>
        </g>
    </g>
</g>
<g id="color2">
    <g transform="matrix( 1, 0, 0, 1, -0.4,18.65) ">
        <g id="Layer_1_2_">
            <g transform="matrix( 1, 0, 0, 1, 0,0) ">
                <g>
                    <g>
                        <path fill="#666666" d="..."/>
                    </g>
                </g>
            </g>
        </g>
    </g>
</g>
<g id="lineart_1_">
    <g transform="matrix( 1, 0, 0, 1, -0.3,27) ">
        <g id="Layer_2">
            <g transform="matrix( 1, 0, 0, 1, 0,0) ">
                <g>
                    <path fill="none" stroke="#000000" stroke-width="0.5" stroke-linecap="round" stroke-linejoin="round" d="..."/>
                </g>
            </g>
        </g>
        <g id="lineart">
            <g transform="matrix( 1, 0, 0, 1, 0,0) ">
                <g>
                    <g>
                        <path d="..."/>
                    </g>
                </g>
            </g>
        </g>
    </g>
</g>
<g id="itemMask">
    <g transform="matrix( 1, 0, 0, 1, -0.3,27) ">
        <g id="Layer_1_3_">
            <g transform="matrix( 1, 0, 0, 1, 0,0) ">
                <g filter="url(#Filter_1)">
                    <g>
                        <path fill="#999999" d="..."/>
                    </g>
                </g>
            </g>
        </g>
    </g>
</g>
<g id="actions">
</g>
</svg>

I've made the following PHP code to extract the first level element that has an ID that doesn't match or partially match lineart.

<?php
$thisEntryDoc = new DOMDocument();
$thisEntryDoc->load('test.svg');
$xpath = new DOMXpath($thisEntryDoc);
$xpath->registerNamespace('svg', "http://www.w3.org/1999/xlink");
$elements = $xpath->query("//svg:svg/svg:g[not(contains(@id, 'lineart'))]");
foreach($elements as $element) {
    echo "Found {$element->nodeName}\n";
}
?>

This produces nothing. I've tested the XPath using an online tool, and it works fine. The only expression that produces any results at all is //*.

I'm completely baffled. Does anyone have any idea why a validated XPath expression simply isn't working at all?


Solution

  • I think you've made a mistake in your namespace registration.

    This is for xlink namespace, not default one:

    $xpath->registerNamespace('svg', "http://www.w3.org/1999/xlink");
    

    Should probably be:

    $xpath->registerNamespace( 'svg', "http://www.w3.org/2000/svg" );