使用XMLReader在PHP中验证SVG文件 [英] Validating SVG file in PHP with XMLReader

查看:118
本文介绍了使用XMLReader在PHP中验证SVG文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在根据SVG规范验证SVG文档(我认为是有效的).我在PHP中使用XMLReader,并且会坚持使用它,因为我将在其他地方使用XMLReader.那就是说,如果还有其他基于流的读者可以更轻松/更好地做到这一点,请告诉我.

I am validating a SVG document (which I believe to be valid) against the SVG spec. I'm using XMLReader in PHP, and would rather stick with that as I will be using XMLReader elsewhere; that said if there are other stream-based readers that will do this easier/better, do let me me know.

好的,这是一些代码:

    // 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文件是:

OK, so the XSD files I've got in my xsd folder are:

  • SVG.xsd
  • xlink.xsd
  • xml.xsd

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

It seems that the parser imports the second and third XSD from the first - I want any dependencies to be stored on disk, not retrieved from the 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

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

It seems like maybe I have imported the wrong version of a schema somewhere - I found all the XSD docs just through a web search. Any ideas?

最后一个错误建议应在阅读文档之前设置架构.确定,所以我将代码更改为此:

the last error suggests the schema should be set before reading the document. OK, so I've changed the code to this:

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

-出现了一些初始警告,但我仍然得到了Unable to set schema的警告.

-- some of the initial warnings go, but I still get the Unable to set schema one.

推荐答案

您链接到的SVG的XSD文件来自SVG 1.1的旧工作草案版本. SVG 1.1当前没有官方支持的XML模式.有关更多详细信息,请参见此答案.

The XSD file for SVG you link to is from an old working draft version of SVG 1.1. There's currently no officially supported XML schema for SVG 1.1. Please see this answer for more details.

这篇关于使用XMLReader在PHP中验证SVG文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆