11

有没有办法(不安装任何库)在 PHP 中使用自定义 DTD 验证 XML?

4

4 回答 4

5

看看PHP 的 DOM,尤其是DOMDocument::schemaValidateDOMDocument::validate

DOMDocument::validate 的示例相当简单:

<?php
$dom = new DOMDocument;
$dom->Load('book.xml');
if ($dom->validate()) {
    echo "This document is valid!\n";
}
?>
于 2008-09-19T13:50:23.767 回答
3

如果字符串中有 dtd,则可以使用 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";
}
于 2011-06-30T09:48:18.943 回答
3

我对原始问题的解释是,我们有一个“板载”XML 文件,我们想根据“板载”DTD 文件对其进行验证。Soren 和 PayamRWD 在评论中表达了我将如何实现“在 DOCTYPE 元素中插入本地 DTD”的想法:

公共功能验证($xml_realpath,$dtd_realpath=null){
    $xml_lines = 文件($xml_realpath);
    $doc = 新的 DOMDocument;
    如果($dtd_realpath){
        // 在 DOCTYPE 行中注入 DTD:
        $dtd_lines = 文件($dtd_realpath);
        $new_lines = 数组();
        foreach ($xml_lines as $x) {
            // 假设 DOCTYPE SYSTEM "blah blah" 格式:
            if (preg_match('/DOCTYPE/', $x)) {
                $y = preg_replace('/SYSTEM "(.*)"/', " [\n" . implode("\n", $dtd_lines) . "\n]", $x);
                $new_lines[] = $y;
            } 别的 {
                $new_lines[] = $x;
            }
        }
        $doc->loadXML(implode("\n", $new_lines));
    } 别的 {
        $doc->loadXML(implode("\n", $xml_lines));
    }
    // 启用用户错误处理
    libxml_use_internal_errors(true);
    if (@$doc->validate()) {
        echo "有效!\n";
    } 别的 {
        echo "无效:\n";
        $errors = libxml_get_errors();
        foreach ($errors as $error) {
            print_r($error, true);
        }
    }
}

请注意,为简洁起见,已禁止错误处理,并且可能有更好/更通用的方法来处理插值。但我实际上已经将此代码与真实数据一起使用,并且它适用于 PHP 版本 5.2.17。

于 2011-09-12T14:15:15.847 回答
1

尝试完成“owenmarshall”答案:

在 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/>
}

?>

模板格式.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>&lt;/background-color>  <br/>
<color>&lt;/color>  <br/>
<font-size>&lt;/font-size>  <br/>
<header-image>&lt;/header-image>  <br/>

</template> 

</template-format>
于 2011-03-03T16:11:08.457 回答