使用XMLReader在PHP中验证SVG文件

时间:2012-03-11 00:48:46

标签: 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 - 我希望任何依赖项存储在磁盘上,而不是从互联网上检索。

好的,这是输出:

    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();

- 一些初始警告,但我仍然得到Unable to set schema一个。

1 个答案:

答案 0 :(得分:1)

您链接到的SVG的XSD文件来自SVG 1.1的旧工作草案版本。目前没有正式支持的SVG 1.1 XML架构。有关详细信息,请参阅this answer