是否可以针对 PHP 中的多个模式验证 XML? [英] Is it possible to validate XML against multiple schemas in PHP?
本文介绍了是否可以针对 PHP 中的多个模式验证 XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道是否可以针对 PHP 中的多个模式验证 xml,或者我必须以某种方式合并我的模式.
I'm wondering if it is possible to validate xml against multiple schemas in PHP or I have to merge my schemas somehow.
感谢您的回答!
推荐答案
我已经通过简单的 PHP 脚本解决了我的问题:
I've solved my problem via simple PHP script:
$mainSchemaFile = dirname(__FILE__) . "/main-schema.xml";
$additionalSchemaFile = 'second-schema.xml';
$additionalSchema = simplexml_load_file($additionalSchemaFile);
$additionalSchema->registerXPathNamespace("xs", "http://www.w3.org/2001/XMLSchema");
$nodes = $additionalSchema->xpath('/xs:schema/*');
$xml = '';
foreach ($nodes as $child) {
$xml .= $child->asXML() . "\n";
}
$result = str_replace("</xs:schema>", $xml . "</xs:schema>", file_get_contents($mainSchemaFile));
var_dump($result); // merged schema in form XML (string)
但只有由于模式相同这一事实才有可能 - 即
But it is possible only thanks to the fact that the schemas are the same - i.e.
<xs:schema xmlns="NAMESPACE"
targetNamespace="NAMESPACE"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
在两个文件中.
这篇关于是否可以针对 PHP 中的多个模式验证 XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文