是否有一种方法(没有安装任何库)使用PHP中的自定义DTD验证XML?
是否有一种方法(没有安装任何库)使用PHP中的自定义DTD验证XML?
看一眼 PHP的DOM特别是 DOM文档:: schemaValidate 和 DOM文档::验证。
DOMDocument :: validate的示例非常简单:
<?php
$dom = new DOMDocument;
$dom->Load('book.xml');
if ($dom->validate()) {
    echo "This document is valid!\n";
}
?>
如果你有一个字符串中的dtd,你可以使用a来验证它 数据包装器 对于dtd:
$xml = '<?xml version="1.0"?>
        <!DOCTYPE note SYSTEM "note.dtd">
        <note>
            <to>Tove</to>
            <from>Jani</from>
            <heading>Reminder</heading>
            <body>Don\'t forget me this weekend!</body>
        </note>';
$dtd = '<!ELEMENT note (to,from,heading,body)>
        <!ELEMENT to (#PCDATA)>
        <!ELEMENT from (#PCDATA)>
        <!ELEMENT heading (#PCDATA)>
        <!ELEMENT body (#PCDATA)>';
$root = 'note';
$systemId = 'data://text/plain;base64,'.base64_encode($dtd);
$old = new DOMDocument;
$old->loadXML($xml);
$creator = new DOMImplementation;
$doctype = $creator->createDocumentType($root, null, $systemId);
$new = $creator->createDocument(null, null, $doctype);
$new->encoding = "utf-8";
$oldNode = $old->getElementsByTagName($root)->item(0);
$newNode = $new->importNode($oldNode, true);
$new->appendChild($newNode);
if (@$new->validate()) {
    echo "Valid";
} else {
    echo "Not valid";
}
我对原始问题的解释是,我们有一个“板载”XML文件,我们希望根据“板载”DTD文件进行验证。因此,我将如何实现Soren和PayamRWD在评论中表达的“插入DOCTYPE元素内的本地DTD”的想法:
public function validate($ xml_realpath,$ dtd_realpath = null){
    $ xml_lines = file($ xml_realpath);
    $ doc = new DOMDocument;
    if($ dtd_realpath){
        //在DOCTYPE行中注入DTD:
        $ dtd_lines = file($ dtd_realpath);
        $ new_lines = array();
        foreach($ xml_lines为$ x){
            //假设DOCTYPE SYSTEM“blah blah”格式:
            if(preg_match('/ DOCTYPE /',$ x)){
                $ y = preg_replace('/ SYSTEM“(。*)”/',“[\ n”。implode(“\ n”,$ dtd_lines)。“\ n]”,$ x);
                $ new_lines [] = $ y;
            } else {
                $ new_lines [] = $ x;
            }
        }
        $ doc-> loadXML(implode(“\ n”,$ new_lines));
    } else {
        $ doc-> loadXML(implode(“\ n”,$ xml_lines));
    }
    //启用用户错误处理
    libxml_use_internal_errors(真);
    if(@ $ doc-> validate()){
        echo“有效!\ n”;
    } else {
        echo“无效:\ n”;
        $ errors = libxml_get_errors();
        foreach($ errors as $ error){
            print_r($ error,true);
        }
    }
}
请注意,为简洁起见,已经抑制了错误处理,并且可能有更好/更通用的方法来处理插值。但是我 有 实际上将此代码与真实数据一起使用,它适用于PHP 5.2.17版。
在xml-validator.php中:
添加HTML,标题,正文,...
<?php
$dom = new DOMDocument; <br/>
$dom->Load('template-format.xml');<br/>
if ($dom->validate()) { <br/>
    echo "This document is valid!\n"; <br/>
}
?>
模板format.xml:
<?xml version="1.0" encoding="utf-8"?>
<!-- DTD to Validate against (format example) -->
<!DOCTYPE template-format [  <br/>
  <!ELEMENT template-format (template)>  <br/>
  <!ELEMENT template (background-color, color, font-size, header-image)>  <br/>
  <!ELEMENT background-color   (#PCDATA)>  <br/>
  <!ELEMENT color (#PCDATA)>  <br/>
  <!ELEMENT font-size (#PCDATA)>  <br/>
  <!ELEMENT header-image (#PCDATA)>  <br/>
]>
<!-- XML example -->
<template-format>
<template>
<background-color></background-color>  <br/>
<color></color>  <br/>
<font-size></font-size>  <br/>
<header-image></header-image>  <br/>
</template> 
</template-format>