how to validate a xml file against a xsd? there is domdocument::schemaValidate() but It does not tell where are the errors. is there any class for that? does it have any worth making that parser from scratch? or is it just reinventing he wheel,
如何根据xsd验证xml文件?有domdocument::schemaValidate(),但它不知道错误在哪里。有这方面的课程吗?是否有必要从头开始创建解析器?或者只是重新发明*,
2 个解决方案
#1
19
This code does the business:
这个代码的业务是:
$xml= new DOMDocument();
$xml->loadXML(<A string goes here containing the XML data>, LIBXML_NOBLANKS); // Or load if filename required
if (!$xml->schemaValidate(<file name for the XSD file>)) // Or schemaValidateSource if string used.
{
// You have an error in the XML file
}
See the code in http://php.net/manual/en/domdocument.schemavalidate.php To retrieve the errors.
请参阅http://php.net/manual/en/domdocument.schemavalidate.php中的代码,以检索错误。
I.e.
即。
justin at redwiredesign dot com 08-Nov-2006 03:32 post.
贾斯汀在redwirepdot com 08- 11 -2006 03:32贴。
#2
6
User contrib from http://php.net/manual/en/domdocument.schemavalidate.php
从http://php.net/manual/en/domdocument.schemavalidate.php用户普通发布版
It works like a charm!
它就像一种魅力!
For more detailed feedback from DOMDocument::schemaValidate, disable libxml errors and fetch error information yourself. See http://php.net/manual/en/ref.libxml.php for more info.
有关DOMDocument::schemaValidate的更详细反馈,请禁用libxml错误并自己获取错误信息。更多信息请参见http://php.net/manual/en/ref.libxml.php。
example.xml
example.xml
<?xml version="1.0"?>
<example>
<child_string>This is an example.</child_string>
<child_integer>Error condition.</child_integer>
</example>
example.xsd
example.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="example">
<xs:complexType>
<xs:sequence>
<xs:element name="child_string" type="xs:string"/>
<xs:element name="child_integer" type="xs:integer"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
PHP
PHP
<?php
function libxml_display_error($error)
{
$return = "<br/>\n";
switch ($error->level) {
case LIBXML_ERR_WARNING:
$return .= "<b>Warning $error->code</b>: ";
break;
case LIBXML_ERR_ERROR:
$return .= "<b>Error $error->code</b>: ";
break;
case LIBXML_ERR_FATAL:
$return .= "<b>Fatal Error $error->code</b>: ";
break;
}
$return .= trim($error->message);
if ($error->file) {
$return .= " in <b>$error->file</b>";
}
$return .= " on line <b>$error->line</b>\n";
return $return;
}
function libxml_display_errors() {
$errors = libxml_get_errors();
foreach ($errors as $error) {
print libxml_display_error($error);
}
libxml_clear_errors();
}
// Enable user error handling
libxml_use_internal_errors(true);
$xml = new DOMDocument();
$xml->load('example.xml');
if (!$xml->schemaValidate('example.xsd')) {
print '<b>DOMDocument::schemaValidate() Generated Errors!</b>';
libxml_display_errors();
}
?>
#1
19
This code does the business:
这个代码的业务是:
$xml= new DOMDocument();
$xml->loadXML(<A string goes here containing the XML data>, LIBXML_NOBLANKS); // Or load if filename required
if (!$xml->schemaValidate(<file name for the XSD file>)) // Or schemaValidateSource if string used.
{
// You have an error in the XML file
}
See the code in http://php.net/manual/en/domdocument.schemavalidate.php To retrieve the errors.
请参阅http://php.net/manual/en/domdocument.schemavalidate.php中的代码,以检索错误。
I.e.
即。
justin at redwiredesign dot com 08-Nov-2006 03:32 post.
贾斯汀在redwirepdot com 08- 11 -2006 03:32贴。
#2
6
User contrib from http://php.net/manual/en/domdocument.schemavalidate.php
从http://php.net/manual/en/domdocument.schemavalidate.php用户普通发布版
It works like a charm!
它就像一种魅力!
For more detailed feedback from DOMDocument::schemaValidate, disable libxml errors and fetch error information yourself. See http://php.net/manual/en/ref.libxml.php for more info.
有关DOMDocument::schemaValidate的更详细反馈,请禁用libxml错误并自己获取错误信息。更多信息请参见http://php.net/manual/en/ref.libxml.php。
example.xml
example.xml
<?xml version="1.0"?>
<example>
<child_string>This is an example.</child_string>
<child_integer>Error condition.</child_integer>
</example>
example.xsd
example.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="example">
<xs:complexType>
<xs:sequence>
<xs:element name="child_string" type="xs:string"/>
<xs:element name="child_integer" type="xs:integer"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
PHP
PHP
<?php
function libxml_display_error($error)
{
$return = "<br/>\n";
switch ($error->level) {
case LIBXML_ERR_WARNING:
$return .= "<b>Warning $error->code</b>: ";
break;
case LIBXML_ERR_ERROR:
$return .= "<b>Error $error->code</b>: ";
break;
case LIBXML_ERR_FATAL:
$return .= "<b>Fatal Error $error->code</b>: ";
break;
}
$return .= trim($error->message);
if ($error->file) {
$return .= " in <b>$error->file</b>";
}
$return .= " on line <b>$error->line</b>\n";
return $return;
}
function libxml_display_errors() {
$errors = libxml_get_errors();
foreach ($errors as $error) {
print libxml_display_error($error);
}
libxml_clear_errors();
}
// Enable user error handling
libxml_use_internal_errors(true);
$xml = new DOMDocument();
$xml->load('example.xml');
if (!$xml->schemaValidate('example.xsd')) {
print '<b>DOMDocument::schemaValidate() Generated Errors!</b>';
libxml_display_errors();
}
?>