php - 使用 XMLReader 在 PHP 中验证 SVG 文件

标签 php xml svg xmlreader xml-validation

我正在根据 SVG 规范验证 SVG 文档(我认为该文档有效)。我在 PHP 中使用 XMLReader,并且宁愿坚持使用 XMLReader,因为我将在其他地方使用 XMLReader;也就是说,如果有其他基于流的阅读器可以更轻松/更好地完成此操作,请告诉我。

好的,这是一些代码:

    // Set some values for the purpose of this example
    $this->path = '/Users/jon/Development/Personal/Visualised/master/test-assets/import-png.svg';
    $xsdPath = '/Users/jon/Development/Personal/Visualised/master/test-assets/xsd/SVG.xsd';

    $reader = new XMLReader();
    $reader->open($this->path);
    $valid = $reader->setSchema($xsdPath);
    $reader->close();

好的,我的 xsd 文件夹中的 XSD 文件是:

解析器似乎从第一个 XSD 导入第二个和第三个 XSD - 我希望所有依赖项都存储在磁盘上,而不是从互联网检索。

好的,这是输出:

    XMLReader::setSchema(): Element '{http://www.w3.org/2001/XMLSchema}import': Skipping import of schema located at '/Users/jon/Development/Personal/Visualised/master/test-assets/xsd/xml.xsd' for the namespace 'http://www.w3.org/XML/1998/namespace', since this namespace was already imported with the schema located at 'http://www.w3.org/2001/xml.xsd'. in /Users/jon/Development/Personal/Visualised/master/lib/Visualised/Document.php on line 45

    Warning: XMLReader::setSchema(): Element '{http://www.w3.org/2001/XMLSchema}attribute': The attribute 'type' is not allowed. in /Users/jon/Development/Personal/Visualised/master/lib/Visualised/Document.php on line 45

    Warning: XMLReader::setSchema(): Element '{http://www.w3.org/2001/XMLSchema}attribute': The attribute 'type' is not allowed. in /Users/jon/Development/Personal/Visualised/master/lib/Visualised/Document.php on line 45

    Warning: XMLReader::setSchema(): Element '{http://www.w3.org/2001/XMLSchema}attribute': The attribute 'type' is not allowed. in /Users/jon/Development/Personal/Visualised/master/lib/Visualised/Document.php on line 45

    Warning: XMLReader::setSchema(): Unable to set schema. This must be set prior to reading or schema contains errors. in /Users/jon/Development/Personal/Visualised/master/lib/Visualised/Document.php on line 45

似乎我在某处导入了错误版本的架构 - 我只是通过网络搜索找到了所有 XSD 文档。有什么想法吗?

编辑:最后一个错误表明应该在阅读文档之前设置架构。好的,所以我将代码更改为:

$reader = new XMLReader();
$valid = $reader->setSchema($xsdPath);
$reader->open($this->path);
$reader->close();

-- 一些初始警告消失了,但我仍然收到无法设置架构

最佳答案

您链接到的 SVG XSD 文件来自 SVG 1.1 的旧工作草案版本。目前 SVG 1.1 没有正式支持的 XML 模式。请参阅this answer了解更多详情。

关于php - 使用 XMLReader 在 PHP 中验证 SVG 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9651493/

相关文章:

javascript - DataMaps.js - 网络天气 map (入站和出站弧线)

php - 通过php在服务器之间同步数据库

php - 在 MySQL 中处理多个数组数据的最佳方式?

java - 以编程方式 ScrollView

c# - 将 WPF 中的对象序列化为 XML

java - 指定 Android 网格布局中的项目以调整大小以填充屏幕高度

html - 具有固定边框大小的可扩展 SVG

css - Flex div 中的 SVG 图像在 Safari 和 Chrome 中的显示方式不同

PHP 函数在 MySQL 中的替代

PHP simpleXML如何检查嵌套子项是否存在