我正在使用 PHP xmlreader 来验证和解析 xml 数据。此 xml 通过 XMLReader::setSchema 函数使用来自本地文件的一些 xsd 模式和通过 xsd:import/include 来自 http://的远程 xsd 模式进行验证。一切正常,但每次调用时它都会从网络中获取 xsd 模式并从磁盘读取。
所以我的问题是:
是否有在本地 RAM 中缓存远程 xsd 模式的方法? 对于本地模式文件,我认为 Linux 中的 tmpfs 可以正常工作,但是否有另一种方法来缓存本地 xsd 模式文件?
解决方案
感谢 VolkerK 指出 xmlcatalog 系统。它适用于 libxml/php xmlreader。在 Linux 中,只需编辑文件/etc/xml/catalog(当您在 Fedora 中时,它来自 xml-common)添加一些条目(例如):
<rewriteURI uriStartString="http://schemas.xmlsoap.org/soap/envelope/" rewritePrefix="/etc/xml/SOAP-Envolope.xsd"/>
<rewriteURI uriStartString="http://schemas.xmlsoap.org/soap/encoding/" rewritePrefix="/etc/xml/SOAP-Encoding.xsd"/>
和手动下载架构(例如 http://schemas.xmlsoap.org/soap/encoding/ ->/etc/xml/SOAP-Encoding.xsd)然后 php xmlreader 在解析 SOAP 消息时按预期工作。
最佳答案
php 的 xmlreader 使用 libxml并且 libxml 支持 xml catalouges :
What is a catalog? Basically it's a lookup mechanism [...]
It is basically used for 3 things:
[...]
- providing a local cache mechanism allowing to load the entities associated to public identifiers or remote resources, this is a really important feature for any significant deployment of XML or SGML since it allows to avoid the aleas and delays associated to fetching remote resources.
还没有尝试过,但我想值得一试。
关于PHP xml阅读器: How to cache xsd schema?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6826384/