PHP DOM支持的DOM核心级别/版本是什么?

时间:2013-06-27 10:40:26

标签: php xml dom domdocument

PHP DOM支持哪些DOM核心版本?我可以看到有许多不同的列表如(list):

支持哪一个?

2 个答案:

答案 0 :(得分:1)

PHP DOM扩展具有Document Object Model (Core) Level 1功能。您可以测试使用辅助方法实现的功能,然后测试功能和版本,这里是四个功能的摘要: / p>

  • 找到了一个 Core 版本:'1.0'
  • 找到了四个 XML 版本:'2.0'; '1.0'; ''; NULL
  • 找到零 HTML 版本。
  • 找到零 XHTML 版本。
  • 找到零 XPath 版本。

如果不是深奥,此结果与规格相结合困惑Core Feature in Level 1.0还需要返回TRUE以及非指定版本(此处:''NULL),但结果显示,它不会。因此,即使DOM核心级别1被宣布为功能,它也会被破坏。

同样the XML Feature can not be level 2.0 if不支持2.0级的 Core 功能 - 这就是这种情况,Core Level 2.0 不是支持的功能。< / p>

DOM中的功能(source):

Features in DOM

我的示例脚本的示例输出:

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.

示例脚本:

<?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) : '');
    }
}

答案 1 :(得分:1)

dom核心版本取决于PHP链接的libxml2版本。您甚至可以在引擎盖下替换库的二进制版本,而无需重新编译php。 (这是我知道的苛刻和有限,但是一旦我这样做,因为libxml2的debian lenny版本有一个错误)

对于运行时检测PHP中的这些功能,@ hakre的答案是一个不错的代码片段