使用XMLReader在PHP中验证SVG文件

使用XMLReader在PHP中验证SVG文件,php,xml,svg,xmlreader,xml-validation,Php,Xml,Svg,Xmlreader,Xml Validation,我正在对照SVG规范验证SVG文档(我认为它是有效的)。我在PHP中使用XMLReader,我宁愿坚持使用它,因为我将在其他地方使用XMLReader;也就是说,如果有其他基于流的读者可以更容易/更好地完成这项工作,请务必告诉我 好的,这里有一些代码: // Set some values for the purpose of this example $this->path = '/Users/jon/Development/Personal/Visualised/mas

我正在对照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—我希望所有依赖项都存储在磁盘上,而不是从internet检索

好的,下面是输出:

    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
似乎我在某处导入了错误的模式版本——我只是通过web搜索找到了所有XSD文档。有什么想法吗

编辑:最后一个错误建议在读取文档之前设置架构。好的,我将代码更改为:

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

--一些初始警告消失了,但我仍然得到
无法设置模式的警告。

您链接到的SVG的XSD文件来自SVG1.1的旧工作草案版本。目前没有官方支持的SVG1.1 XML模式。请参阅以了解更多详细信息。

Hmm,我已将svg XSD转换为RelaxNG,并将
setSchema
切换为
setRelaxNGSchema
,我得到了相同的结果。有趣的是,
setRelaxNGSchema
似乎需要在设置文档后调用(源代码:),然而,我认为对于
setSchema
,情况正好相反。请参阅。非常感谢@ErikDahlström-我会在有时间的时候跟进这一点(如果我的PHP错误是由于我使用了错误的XSD文件,或者如果我可以使用DTD而不是XSD/NRG,那么你就可以得到我为这个问题计划的奖励)@ErikDahlström-谢谢你!我已经和这个打了很久了。通过一些代码修改,我现在已经成功地针对DTD进行了验证。如果你想要+100,请在下面添加你的链接;我会设立一个赏金奖励给你。我会在明天奖励赏金-它不会立即允许。