Doctype problem displaying SVG with Safari

2019-01-20 16:20发布

问题:

I have several SVG images I'd like to layout on a page. Firefox and Chrome have given me no issues whatsoever, but Safari only seem to display the SVG image if and only if the document has an ".xhtml" extension. If I try to use PHP code (and therefor a ".php" extension) the exact same markup I used in the ".xhtml" document will no longer display the SVG image. The problem is of course, I must use PHP for the project at hand. Any suggestions? Here's the source code:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:svg="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:lang="de" xmlns:xml="http://www.w3.org/XML/1998/namespace">
<head>
<title>SVG Safari Test</title>
</head>
<body>
<!-- Generator: Adobe Illustrator 15.0.2, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="500px" height="500px" viewBox="0 0 500 500" enable-background="new 0 0 500 500" xml:space="preserve"><circle cx="250" cy="250" r="238.048"/></svg>
</body>
</html>

回答1:

The reason .xhtml works in Safari and .html doesn't is that Safari needs to treat the document as XML to allow embedded SVG. The latest versions of Firefox and Chrome use a HTML5 parser which allows SVG to be embedded in plain HTML documents, so they should work with both.

To have it render properly in Safari you need to set the content type to application/xhtml+xml. Use this in your PHP file before you output any content:

<?php
header('Content-type: application/xhtml+xml');
?>


回答2:

you can set your php config to process xhtml files.

In this example I'm assuming you're using apache on debian/ubuntu

you can look in the file /etc/apache2/mods-enabled/php5.conf

<IfModule mod_php5.c>

<FilesMatch "\.xhtml$">
SetHandler application/x-httpd-php
</FilesMatch>

</IfModule>

You may have this SetHandler directive in some other file if you're using a different webserver or distro or OS...

assuming you're on a unix box, just grep the /etc/apache or /etc/httpd directories

grep -Ri sethandler /etc/apache