Php 使用XSD进行XML验证
我有这个XML的产品,我正在尝试验证 (我将其保存在xml.xml文件中) 我需要重写模式,以便它注意到主元素中的*多个元素(请注意相同的名称用法)。我已使用该模式生成以下模式(该模式验证您提供的XML):Php 使用XSD进行XML验证,php,xml,validation,xsd,Php,Xml,Validation,Xsd,我有这个XML的产品,我正在尝试验证 (我将其保存在xml.xml文件中) 我需要重写模式,以便它注意到主元素中的*多个元素(请注意相同的名称用法)。我已使用该模式生成以下模式(该模式验证您提供的XML): 你可以从上面开始,并在前进的过程中进行调整 验证结果: 如果希望手动修复工具创建的XSD,请确保仅将catalogType的前两次出现的重命名为其他内容(例如catalogType1)。我用于生成以下模式(验证您提供的XML): 你可以从上面开始,并在前进的过程中进行调整 验证结果
你可以从上面开始,并在前进的过程中进行调整
验证结果:
如果希望手动修复工具创建的XSD,请确保仅将catalogType
的前两次出现的重命名为其他内容(例如catalogType1
)。我用于生成以下模式(验证您提供的XML):
你可以从上面开始,并在前进的过程中进行调整
验证结果:
如果您希望手动修复您的工具创建的XSD,请确保仅将目录类型的前两次出现的
重命名为其他内容(例如目录类型1
)。抱歉,在问题中已更新。仍然不知道问题是什么,在问题中更新。仍然不知道问题是什么,但是使用上面的php代码运行nint仍然会生成错误:php php.php坏XML
错误1866:元素“catalog”,属性“link”:属性“link”是不允许的。在第3行
错误1871:元素“subject”:不应使用此元素。预期为(目录)。在第4Hi@valk行,我附上了您发布的XML的屏幕截图和我生成的XSD;验证在输出面板中显示为OK。您一定有其他问题(可能是您正在使用的XML?)。。。您确定刚才尝试的XML与发布的XML相同吗?我想不出其他任何东西:(…我非常确定。现在我重新检查了它。可能是$dom->schemaValidate()有一些bug,但我对此表示怀疑。顺便说一句,这是PHP5.3,但应该没有什么区别。@valk,至少现在加载了架构;但是,消息没有意义;您以某种方式验证了嵌套的目录
节点,而不是根节点-这是肯定的。请查看代码,因为您没有验证根目录,但e下一个。神奇之处在于$xml_object->catalog;尝试验证您的$xml_对象。感谢xsd pal,但是使用上面的php代码运行nint仍然会生成错误:php php.php坏xml
错误1866:Element'catalog',attribute'link':属性'link'是不允许的。在第3行
错误1871:Element'subject':此元素是不允许的expected.expected is(catalog)。在第4Hi@valk行,我附上了您发布的XML和我生成的XSD的屏幕截图;验证在输出面板中显示为OK。您一定有其他问题(可能是您正在使用的XML?)…你确定刚才尝试的XML与发布的XML相同吗?我想不出其他任何东西:(…我非常确定。现在我重新检查了它。可能是$dom->schemaValidate()有一些bug,但我对此表示怀疑。顺便说一句,这是PHP5.3,但应该没有什么区别。@valk,至少现在加载了架构;但是,消息没有意义;您以某种方式验证了嵌套的目录
节点,而不是根节点-这是肯定的。请查看代码,因为您没有验证根目录,但e下一个。神奇之处在于$xml\u object->catalog;尝试验证您的$xml\u对象。
<?xml version="1.0" encoding="UTF-8"?>
<catalog label="global">
<catalog label="animals" link="/animals.php">
<subject>Subject1</subject>
</catalog>
<catalog label="animals" link="/animals.php">
<subject>Subject2</subject>
</catalog>
</catalog>
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="catalog" type="catalogType" />
<xsd:complexType name="catalogType">
<xsd:sequence>
<xsd:element maxOccurs="unbounded" name="catalog" type="catalogType" />
</xsd:sequence>
<xsd:attribute name="label" type="xsd:string" />
</xsd:complexType>
<xsd:complexType name="catalogType">
<xsd:sequence>
<xsd:element name="subject" type="xsd:string" />
</xsd:sequence>
<xsd:attribute name="label" type="xsd:string" />
<xsd:attribute name="link" type="xsd:string" />
</xsd:complexType>
</xsd:schema>
<?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 checkXMLStructure($xml_object)
{
$xmlElement = $xml_object->catalog;
$dom_sxe = dom_import_simplexml($xmlElement);
$dom = new DOMDocument('1.0');
$dom_sxe = $dom->importNode($dom_sxe, true);
$dom_sxe = $dom->appendChild($dom_sxe);
if ( !$dom->schemaValidate( dirname(__FILE__) . '/xml.xsd') ) {
echo "BAD XML\n";
return;
}
echo "*GOOD* XML!\n";
}
// Enable user error handling
libxml_use_internal_errors(true);
$xml_object = simplexml_load_file('xml.xml');
checkXMLStructure($xml_object);
// Display Errors
$errors = libxml_get_errors();
foreach ($errors as $error) {
print libxml_display_error($error);
}
libxml_clear_errors();
PHP Warning: DOMDocument::schemaValidate(): Element '{http://www.w3.org/2001/XMLSchema}complexType': A global complex type definition 'catalogType' does already exist. in /path/php.php on line 13
PHP Stack trace:
PHP 1. {main}() /path/php.php:0
PHP 2. checkXMLStructure() /path/php.php:23
PHP 3. DOMDocument->schemaValidate() /path/php.php:13
PHP Warning: DOMDocument::schemaValidate(): Invalid Schema in /path/php.php on line 13
PHP Stack trace:
PHP 1. {main}() /path/php.php:0
PHP 2. checkXMLStructure() /path/php.php:23
PHP 3. DOMDocument->schemaValidate() /path/php.php:13
BAD XML
<?xml version="1.0" encoding="utf-8"?>
<!--W3C Schema generated by QTAssistant/W3C Schema Refactoring Module (http://www.paschidev.com)-->
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="catalog">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="unbounded" name="catalog">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="subject" type="xsd:string" />
</xsd:sequence>
<xsd:attribute name="label" type="xsd:string" use="required" />
<xsd:attribute name="link" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="label" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:schema>