[英]Validate XML using a custom DTD in PHP
是否有一種方法(沒有安裝任何庫)使用PHP中的自定義DTD驗證XML?
看一下PHP的DOM ,特別是DOMDocument :: schemaValidate和DOMDocument :: validate 。
DOMDocument :: validate的示例非常簡單:
<?php
$dom = new DOMDocument;
$dom->Load('book.xml');
if ($dom->validate()) {
echo "This document is valid!\n";
}
?>
如果你有一個字符串中的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";
}
我對原始問題的解釋是,我們有一個“板載”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) { // Inject DTD inside DOCTYPE line: $dtd_lines = file($dtd_realpath); $new_lines = array(); foreach ($xml_lines as $x) { // Assume DOCTYPE SYSTEM "blah blah" format: 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)); } // Enable user error handling libxml_use_internal_errors(true); if (@$doc->validate()) { echo "Valid!\n"; } else { echo "Not valid:\n"; $errors = libxml_get_errors(); foreach ($errors as $error) { print_r($error, true); } } }
請注意,為簡潔起見,已經抑制了錯誤處理,並且可能有更好/更通用的方法來處理插值。 但我實際上已將此代碼與真實數據一起使用,並且它適用於PHP 5.2.7版。
在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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.