I want to validate an XML document against a local XSD file using XMLReader on PHP/5.3.0. The schema is called test.xsd
and it contains a reference to a second schema:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:include schemaLocation="common.xsd"/>
I have a copy of all files in the same directory. The relevant part of the code looks like this:
<?php
define('DIR_XML',开发者_运维技巧 dirname(__FILE__) . DIRECTORY_SEPARATOR . 'samples' . DIRECTORY_SEPARATOR);
$oXMLReader = new XMLReader;
$oXMLReader->open('test.xml');
$oXMLReader->setSchema(DIR_XML . 'test.xsd');
while( $oXMLReader->read() ){
}
$oXMLReader->close();
?>
My code works fine if the samples
directory is local to the web server and reachable by a drive letter, e.g.:
define('DIR_XML', 'C:\samples' . DIRECTORY_SEPARATOR);
However, real data is in a network drive reachable by UNC path:
\\SERVER\WEB PROJECTS\foo\samples
And here's the problem: in such case (and only in such case) PHP loads test.xsd
just fine but it cannot find common.xsd
. I get a warning:
Warning: XMLReader::setSchema() [xmlreader.setschema]: Unable to set schema. This must be set prior to reading or schema contains errors.
... and the following XML errors:
Warning 1549 (Start element): failed to load external entity "/SERVER/WEB%20PROJECTS/foo/samples/common.xsd"
Error 3050 (Start element): Element '{http://www.w3.org/2001/XMLSchema}include': Failed to load the document '/SERVER/WEB%20PROJECTS/foo/samples/common.xsd' for inclusion.
It seems that PHP is misinterpreting the UNC path as a URL :-?
Is there any option or trick to fix this?
i ran into a similar issue and the only way i could fix it was using a symlink from local HDD to UNC path (windows): mklink /D "C:\SomeDir" "//Some//UNC//Path"
would have thought that this would also work on linux
It's hard to answer without seeing what the string storing \\SERVER\WEB PROJECTS\foo\sample
looks like. This might be a cause of escaping needed, as \\
produces \
, and \\\\
is needed to produce \\
.
精彩评论