Validating SVG file in PHP with XMLReader
Asked Answered
R

1

2

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.

OK, here's some code:

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

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

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.

OK, here's the output:

    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

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?

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

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

Rendering answered 11/3, 2012 at 0:48 Comment(5)
Hmm, I've converted the svg XSD to RelaxNG here and switched setSchema to setRelaxNGSchema - and I get the same result.Rendering
Interestingly, setRelaxNGSchema seems to need to be called after the document is set (source: bugs.php.net/bug.php?id=46978), whereas I think it's the other way around for setSchema.Rendering
See #3511486.Lyell
Much appreciated @ErikDahlström - I will follow that up when I get a moment (if my PHP errors turn out to be due to my using the wrong XSD files, or if I can use a DTD in preference to an XSD/NRG then you can have the bounty I planned for this question).Rendering
@ErikDahlström - thank you! I'd been fighting with this one for ages. With a bit of code fiddling I've now successfully validated against the DTD successfully. If you'd like +100, please add your link below; I'll set up a bounty and award it to you.Rendering
R
3

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.

Reproduce answered 14/3, 2012 at 9:33 Comment(1)
Will award the bounty tomorrow - it won't permit it immediately.Rendering

© 2022 - 2024 — McMap. All rights reserved.