【问题标题】:Validating SVG file in PHP with XMLReader使用 XMLReader 在 PHP 中验证 SVG 文件
【发布时间】:2012-03-27 22:23:42
【问题描述】:

我正在根据 SVG 规范验证 SVG 文档(我认为它是有效的)。我在 PHP 中使用 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();

-- 一些初始警告消失了,但我仍然收到 Unable to set schema 警告。

【问题讨论】:

  • 嗯,我已将 svg XSD 转换为 RelaxNG here 并将 setSchema 切换为 setRelaxNGSchema - 我得到了相同的结果。
  • 有趣的是,setRelaxNGSchema 似乎需要在设置文档后调用(来源:bugs.php.net/bug.php?id=46978),而我认为 setSchema 的情况正好相反。
  • 非常感谢@ErikDahlström - 我会在有时间的时候跟进(如果我的 PHP 错误是由于我使用了错误的 XSD 文件,或者我可以优先使用 DTD到 XSD/NRG 那么你可以得到我为这个问题计划的赏金)。
  • @ErikDahlström - 谢谢!我已经和这个人斗争了很多年。通过一些代码摆弄,我现在已经成功地验证了 DTD。如果您想要 +100,请在下面添加您的链接;我会设置一个赏金并奖励给你。

标签: php xml svg xmlreader xml-validation


【解决方案1】:

您链接到的 SVG 的 XSD 文件来自 SVG 1.1 的旧工作草案版本。目前没有官方支持的 SVG 1.1 的 XML 模式。详情请见this answer

【讨论】:

  • 明天将奖励赏金 - 它不会立即允许。
猜你喜欢
  • 2018-07-28
  • 1970-01-01
  • 2014-06-23
  • 2012-07-27
  • 1970-01-01
  • 1970-01-01
  • 2010-12-22
  • 1970-01-01
相关资源
最近更新 更多