Handle XML Catalogs by php
I have an XSD (XML Schema) which contains multiple files. There's a catalog.xml in the root of the system, which is an XML Catalog. It contains SYSTEM and URI catalog definitions which are needed to use to properly process the xsd files.
Now my question: How can I validate xml against these schemas in php? If I simply use DOMDocument->Schemavalidate() and give it the correct xsd, the catalog definitions will not be resolved, and the php throws an error like this:
Warning: DOMDocument::schema开发者_如何转开发Validate(): I/O warning : failed to load external entity "urn:oasis:names:tc:dita:xsd:highlightDomain.xsd:1.2"
It's in the catalog.xml how to resolve this expression, but I couldn't find a way to handle that with php.
DOM uses libxml underneath. LibXML should be able to use Catalog files when they are placed in /etc/xml/catalog
. Same path on windows IIRC.
Feel free to upvote my feature request to make the path configurable from PHP:
- Request #53950 - Add a way to configure where libxml searches for Catalog Files
The problem you're facing is that PHP is not able to resolve the following path:
urn:oasis:names:tc:dita:xsd:highlightDomain.xsd:1.2
within your system.
You could replace instances of such URIs with something PHP is able to resolve.
One thing that come to my mind to do this is by registering a stream wrapper that is resolving these URIs for you automatically.
As an alternative fix you can read the XSD files in on your own and convert them into a DATA URI and then replace the old urn:
URI with the new data URI. Some example code:
$name = 'file.xsd';
$path = '/path-to-file/'.$name;
$data = file_get_contents($path);
$file = 'data://text/plain;base64,'.base64_encode($data);
Hope this helps.
精彩评论