1

What is the DOM Core Version is Supported by PHP DOM? I can see there are many different ones listed like (list):

Which one is supported?

hakre
  • 193,403
  • 52
  • 435
  • 836

2 Answers2

1

PHP DOM Extension has the Document Object Model (Core) Level 1 feature. You can test for features that are implemented with a helper method and then testing for features and versions, here a summary for four features:

  • One Core versions found: '1.0'.
  • Four XML versions found: '2.0'; '1.0'; ''; NULL.
  • Zero HTML versions found.
  • Zero XHTML versions found.
  • Zero XPath versions found.

This result combine with the specs is puzzeling if not esoteric. The Core Feature in Level 1.0 requires to return TRUE as well for a non-specified version (here: for '' and NULL), but as the results show, it does not. So even DOM Core Level 1 is announced as feature, it's also broken.

Also the XML Feature can not be level 2.0 if the Core feature of level 2.0 is not supported - and this is the case here, Core Level 2.0 is not a supported feature.

Features in DOM (source):

Features in DOM

Exemplary Output of my example script:

Core Feature is in PHP DOMDocument implementation:

    1.) Core '3.0': FALSE
    2.) Core '2.0': FALSE
    3.) Core '1.0': TRUE
    4.) Core ''   : FALSE
    5.) Core NULL : FALSE

One Core versions found: '1.0'.

XML Feature is in PHP DOMDocument implementation:

    1.) XML '3.0': FALSE
    2.) XML '2.0': TRUE
    3.) XML '1.0': TRUE
    4.) XML ''   : TRUE
    5.) XML NULL : TRUE

Four XML versions found: '2.0'; '1.0'; ''; NULL.

HTML Feature is in PHP DOMDocument implementation:

    1.) HTML '3.0': FALSE
    2.) HTML '2.0': FALSE
    3.) HTML '1.0': FALSE
    4.) HTML ''   : FALSE
    5.) HTML NULL : FALSE

Zero HTML versions found.

XHTML Feature is in PHP DOMDocument implementation:

    1.) XHTML '3.0': FALSE
    2.) XHTML '2.0': FALSE
    3.) XHTML '1.0': FALSE
    4.) XHTML ''   : FALSE
    5.) XHTML NULL : FALSE

Zero XHTML versions found.

XPath Feature is in PHP DOMDocument implementation:

    1.) XPath '3.0': FALSE
    2.) XPath '2.0': FALSE
    3.) XPath '1.0': FALSE
    4.) XPath ''   : FALSE
    5.) XPath NULL : FALSE

Zero XPath versions found.

Example script:

<?php
/**
 * What is the DOM Core Version is Supported by PHP DOM?
 * @link http://stackoverflow.com/a/17340953/367456
 */

$dom = new DOMDocument();
$dom->loadXML('<root/>');

$versionsArray = ['3.0', '2.0', '1.0', '', NULL];
$features      = [
    # Document Object Model (DOM) <http://www.w3.org/DOM/DOMTR>
    'Core'  => $versionsArray,

    # Document Object Model (DOM) <http://www.w3.org/DOM/DOMTR>
    'XML'   => $versionsArray,

    # Document Object Model (DOM) Level 2 HTML Specification <http://www.w3.org/TR/DOM-Level-2-HTML/>
    'HTML'  => $versionsArray,
    'XHTML' => $versionsArray,

    # Document Object Model XPath <http://www.w3.org/TR/DOM-Level-3-XPath/xpath.html>
    "XPath" => $versionsArray,
];

const DISPLAY_TITLE   = 1;
const DISPLAY_DETAILS = 2;
const DISPLAY_SUMMARY = 4;
const DISPLAY_ALL     = 7;

dom_list_features($dom, $features);

function dom_list_features(DOMDocument $dom, array $features, $display = DISPLAY_ALL) {

    foreach ($features as $feature => $versions) {
        dom_list_feature($dom, $feature, $versions, $display);
    }
}

function dom_list_feature(DOMDocument $dom, $feature, array $versions, $display) {

    if ($display & DISPLAY_TITLE) {
        echo "$feature Feature is in PHP DOMDocument implementation:\n\n";
    }

    $found = [];

    foreach ($versions as $i => $version) {
        $result = $dom->implementation->hasFeature($feature, $version);
        if ($result) {
            $found[] = $version;
        }

        if ($display & DISPLAY_DETAILS) {
            printf("    %d.) $feature %' -5s: %s\n", $i + 1, var_export($version, true), $result ? 'TRUE' : 'FALSE');
        }
    }

    if ($display & DISPLAY_DETAILS) {
        echo "\n";
    }

    $formatter = new NumberFormatter('en_UK', NumberFormatter::SPELLOUT);
    $count     = ucfirst($formatter->format(count($found)));
    $found     = array_map(function ($v) {
        return var_export($v, TRUE);
    }, $found);

    if ($display & DISPLAY_SUMMARY) {
        printf("%s %s versions found%s.\n\n", $count, $feature, $found ? ': ' . implode('; ', $found) : '');
    }
}
hakre
  • 193,403
  • 52
  • 435
  • 836
  • Came back here because my post was up-voted. I see yours is really a nice one. one of the hidden treasures. :) unfortunately I already up-voted this.. (although I must admit that I personally don't think one should answer his own questions. I would make a blog instead) – hek2mgl Aug 10 '13 at 10:59
  • Well I also blog and indeed, that would be an appropriate alternative. However I also think there is some value to have this here on Stackoverflow and I like the Q&A style so placed it here :) Maybe one day I make a blog post about the *Seven Secrets of PHP's DOMDocument* or what not :) – hakre Aug 10 '13 at 12:36
  • Yes this is a nice once, this is out of question. Will read some articles on your blog page now ... (and drinking beer during this :P ) – hek2mgl Aug 10 '13 at 12:40
1

The dom core version depends on the libxml2 version PHP is linked against. You can even replace the binary version of the library under the hood without need to recompile php. (That's hakish and limited I know, but once I did that because the debian lenny version of libxml2 had a bug)

For runtime detection of this features in PHP @hakre's answers is a nice snippet

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • speaking of libxnml, which features does it support? ;) - (I think it's still as outlined above, that didn't change across libxml versions) – hakre Jul 04 '13 at 23:44
  • don't understand the question – hek2mgl Jul 04 '13 at 23:45
  • I think that replacing libxml does not change much here. It's DOM Level-1 and that's it. Also this is dependent on the object wrappers that the PHP extension offers. It's based on libxml but still it's own object interface in PHP's DOḾDocument so I'm not sure if your answer hits the nail here. In any case writing which features those different libxml versions support would add some value. – hakre Jul 04 '13 at 23:45
  • Yeah of course, I'm with you and I didn't wanted to say the opposite. Upgrading a whole DOM core version by replacing the lib isn't possible as this would require to adjust the PHP extension as well. Minor bugfixes are possible. Just wanted to point out that PHP has not it's own DOM implementation. It's linked against libxml2 and has the same feature set as the library version PHP is linked against. So when looking to the features I usually had a look to libxml2 before. But the runtime detection script above is nice.. – hek2mgl Jul 04 '13 at 23:48
  • Well PHP has it's own DOMDOcument implementation - like with SimpleXML. It's still linked on libxml2 but it's also PHP specific. Especially as the interfaces are concerned for those features. – hakre Jul 04 '13 at 23:50
  • 1
    I have to admit, that the code amount of the dom extension is significant. Would have to dig more into it to be able to surely say: *this* work is done by the extension, while *this* work is done by `libxml2` – hek2mgl Jul 05 '13 at 00:03