从PHP 5.2.8升级到5.3.3后,新的libxml_disable_entity_loader(false)
函数似乎无法通过命令行在Windows上运行到网络共享上的PHP文件。更具体地说,DOMDocument::schemaValidate()
失败的XSD文件包含多层嵌套的include()到网络共享上的其他XSD文件。
下面的例子让我相信,PHP中没有使用包含多级嵌套XSD include()的XSD文件来调用libxml_disable_entity_loader(false)
的函数。
我在这里遗漏了什么,或者你认为这是PHP或者libxml的错误吗?
重复:
测试1(成功):
"C:\PHP\5.3.3\php.exe" -c "C:\PHP\5.3.3\php.ini" "C:\Temp\validate.php"
测试2(失败):
"C:\PHP\5.3.3\php.exe" -c "C:\PHP\5.3.3\php.ini" "\\192.168.82.99\Deployment\Temp\validate.php"
在PHP 5.3.3中运行上面的命令行示例会产生:
Warning: DOMDocument::schemaValidate(): I/O warning : failed to load external entity "/192.168.82.99/DEPLOYMENT/Temp/grandparent.xsd" in \\192.168.82.99\DEPLOYMENT\Temp\validate.php on line 8
Warning: DOMDocument::schemaValidate(): Element '{http://www.w3.org/2001/XMLSchema}include': Failed to load the document '/192.168.82.99/DEPLOYMENT/Temp/grandparent.xsd' for inclusion. in \\192.168.82.99\DEPLOYMENT\Temp\validate.php on line 8
Warning: DOMDocument::schemaValidate(): Invalid Schema in \\192.168.82.99\DEPLOYMENT\Temp\validate.php on line 8
failed
需要复制的文件:
将所有这些文件放在“C:\ Temp \”(或在命令行示例中更改路径)
validate.php
<?php
chdir(dirname(__FILE__));
libxml_disable_entity_loader(false);
$xmlDoc = new DomDocument();
$xmlDoc->load('sample.xml');
echo $xmlDoc->schemaValidate('child.xsd') ? 'passed' : 'failed';
?>
sample.xml中
<?xml version="1.0"?>
<team mascot="cowboys" />
child.xsd
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="qualified" >
<!-- The include below works -->
<xsd:include schemaLocation="parent.xsd" />
<xsd:element name="team" type="baseTeam" />
</xsd:schema>
parent.xsd
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="qualified" >
<!-- The include below fails in PHP 5.3.3 even though libxml_disable_entity_loader(false) is called! -->
<xsd:include schemaLocation="grandparent.xsd" />
<xsd:complexType name="baseTeam">
<xsd:attribute name="mascot" type="mascotNames" use="required" />
</xsd:complexType>
</xsd:schema>
grandparent.xsd
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="qualified" >
<xsd:simpleType name="mascotNames">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="bengals" />
<xsd:enumeration value="cowboys" />
<xsd:enumeration value="patriots" />
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
答案 0 :(得分:2)
$dir = dirname(__FILE__) . '/sources/';
$file = $dir . 'data.xml';
$xml = file_get_contents($file);
$doc = new DOMDocument;
$doc->documentURI = $file;
$doc->loadXML($xml);
$doc->xinclude();
echo $doc->schemaValidate($dir . 'child.xsd') ? 'passed' : 'failed';