PHP 5.3.3 fails to validate XML against nested XSD's
After upgrading from PHP 5.2.8 to 5.3.3, the new libxml_disable_entity_loader(false)
function does not seem to work on Windows via command line to a PHP file on a network share. More specifically, DOMDocument::schemaValidate()
fails with XSD files that contain multiple levels of nested include()'s to other XSD files on a network share.
The example below leads me to believe that the function call to libxml_disable_entity_loader(false)
is not being honored in PHP with XSD files that contain multiple levels of nested XSD include()'s.
Am I missing something here or do you think this is a bug with PHP or possibly libxml?
TO REPLICATE:
Test 1 (success):
"C:\PHP\5.3.3\php.exe" -c "C:\PHP\5.3.3\php.ini" "C:\Temp\validate.php"
- The Windows command line example above uses a hard coded path and not a Windows shared folder.
- It will successfully echo "passed" in both PHP 5.2.8 and PHP 5.3.3.
Test 2 (fail):
"C:\PHP\5.3.3\php.exe" -c "C:\PHP\5.3.3\php.ini" "\\192.168.82.99\Deployment\Temp\validate.php"
- Right-click on your C: drive > Properties > Sharing > New Share > Share name: "Deployment"
- Edit the IP address in the command line example above to match your local machine.
- The Windows command line example above uses the same files but through a Windows shared folder.
- In PHP 5.2.8, it will echo "passed".
- In PHP 5.3.3, it will echo "failed" and produce the following PHP Warnings about "failed to load external entity" and "Failed to load the document":
Running the above command line example in PHP 5.3.3 produces:
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: DOMDocu开发者_如何学Pythonment::schemaValidate(): Invalid Schema in \\192.168.82.99\DEPLOYMENT\Temp\validate.php on line 8
failed
FILES NEEDED TO REPLICATE:
Place all these files in "C:\Temp\" (or change your paths in the command line examples)
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>
$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';
精彩评论