赞
踩
我想加快针对同一个
XML架构(XSD)验证一批
XML文件的过程.只有我在
PHP环境中的限制.
我目前的问题是我想要验证的模式包括相当复杂的2755行的xhtml模式(http://www.w3.org/2002/08/xhtml/xhtml1-transitional.xsd).
即使对于非常简单的数据,这也需要很长时间(大约30秒pr.验证).
由于我的批处理中有数千个XML文件,因此实际上并不能很好地扩展.
为了验证XML文件,我使用了标准php-xml库中的这两种方法.
> DOMDocument :: schemaValidate
> DOMDocument :: schemaValidateSource
我认为PHP实现通过HTTP获取XHTML模式并构建一些内部表示(可能是DOMDocument),并且在验证完成时抛弃它.我在想,XML-libs的一些选项可能会改变这种行为,以便在此过程中缓存某些内容以供重用.
我已经构建了一个简单的测试设置来说明我的问题:
elementFormDefault="qualified"
targetNamespace="http://myschema.example.com/"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:myschema="http://myschema.example.com/"
xmlns:xhtml="http://www.w3.org/1999/xhtml">
schemaLocation="http://www.w3.org/2002/08/xhtml/xhtml1-transitional.xsd"
namespace="http://www.w3.org/1999/xhtml">
This is an XHTML paragraph!
$data_dom = new DOMDocument();
$data_dom->load('test-data.xml');
// Multiple validations using the schemaValidate method.
for ($attempt = 1; $attempt <= 3; $attempt++) {
$start = time();
echo "schemaValidate: Attempt #$attempt returns ";
if (!$data_dom->schemaValidate('test-schema.xsd')) {
echo "Invalid!";
} else {
echo "Valid!";
}
$end = time();
echo " in " . ($end-$start) . " seconds.\n";
}
// Loading schema into a string.
$schema_source = file_get_contents('test-schema.xsd');
// Multiple validations using the schemaValidate method.
for ($attempt = 1; $attempt <= 3; $attempt++) {
$start = time();
echo "schemaValidateSource: Attempt #$attempt returns ";
if (!$data_dom->schemaValidateSource($schema_source)) {
echo "Invalid!";
} else {
echo "Valid!";
}
$end = time();
echo " in " . ($end-$start) . " seconds.\n";
}
运行此schematest.php文件将生成以下输出:
schemaValidate: Attempt #1 returns Valid! in 30 seconds.
schemaValidate: Attempt #2 returns Valid! in 30 seconds.
schemaValidate: Attempt #3 returns Valid! in 30 seconds.
schemaValidateSource: Attempt #1 returns Valid! in 32 seconds.
schemaValidateSource: Attempt #2 returns Valid! in 30 seconds.
schemaValidateSource: Attempt #3 returns Valid! in 30 seconds.
如何解决这个问题的任何帮助和建议都非常欢迎!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。