Note that there are some explanatory texts on larger screens.

plurals
  1. POValidating SVG file in PHP with XMLReader
    text
    copied!<p>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.</p> <p>OK, here's some code:</p> <pre><code> // Set some values for the purpose of this example $this-&gt;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-&gt;open($this-&gt;path); $valid = $reader-&gt;setSchema($xsdPath); $reader-&gt;close(); </code></pre> <p>OK, so the XSD files I've got in my <code>xsd</code> folder are:</p> <ul> <li><a href="http://www.w3.org/TR/2002/WD-SVG11-20020108/SVG.xsd" rel="nofollow">SVG.xsd</a></li> <li><a href="http://www.w3.org/XML/2008/06/xlink.xsd" rel="nofollow">xlink.xsd</a></li> <li><a href="http://www.w3.org/2000/10/xml.xsd" rel="nofollow">xml.xsd</a></li> </ul> <p>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.</p> <p>OK, here's the output:</p> <pre><code> 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 </code></pre> <p>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?</p> <p>Edit: the last error suggests the schema should be set before reading the document. OK, so I've changed the code to this:</p> <pre><code>$reader = new XMLReader(); $valid = $reader-&gt;setSchema($xsdPath); $reader-&gt;open($this-&gt;path); $reader-&gt;close(); </code></pre> <p>-- some of the initial warnings go, but I still get the <code>Unable to set schema</code> one.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload