HTML5 中的 SVG – 何时需要 XML 声明`<?xml version="1.0" encoding="UTF-8"?>`?
SVG in HTML5 – when is XML declaration `<?xml version="1.0" encoding="UTF-8"?>` needed?
在 HTML5 中使用 SVG 时:SVG
是否需要 XML 声明 <?xml version="1.0" encoding="UTF-8"?>
- 作为图像来自
<img>
或
- as CSS
background-image
s?
这与“Are SVG parameters such as 'xmlns' and 'version' needed”. The namespaces issues are clarified as necessary by the two answers and the MDN Namespace crash course.
略有关联
但是 SVG 1.1 不包含关于 XML 声明的必要性或何时可以省略的声明?
没有声明的例子:
<svg xmlns="http://www.w3.org/2000/svg" width="200" height="200" viewBox="0 0 200 200">
<circle id="circle--red" cx="30" cy="30" r="30" fill="#f00"/>
</svg>
2016-07-04 更新:澄清问题是关于 XML 声明。谢谢@Martin Honnen!
2017-10-24更新:更改为“UTF-8“ uppercase和SVGO优化属性顺序。
对于HTML5,正确的DOCTYPE declaration是
<!DOCTYPE html>
需要给浏览器指定全标准模式。
你展示了什么,
<?xml version="1.0" encoding="utf-8"?>
是一个XML declaration. It is optional for XML 1.0 and required for XML 1.1,但是
- XML 1.1 并未广泛使用。
version="1.0"
和 encoding="utf-8"
无论如何都是默认值。
当您希望指定不同的编码时,请在 HTML5 中使用 XML 声明,尤其是当文件可能不仅被浏览器而且还被 XML 处理器使用时。
有关详细信息,请参阅 HTML5: A vocabulary and associated APIs for HTML and XHTML。
关于内部 SVG 的注意事项(感谢,@Quentin): SVG embedded within an HTML5 document should not have an independent XML declaration. Only one XML declaration is allowed in well-formed XML, and it must be at the top, if anywhere. See this answer 了解有关 XML 声明放置要求的更多详细信息。
关于外部 SVG 的注意事项(谢谢,@Kaiido):SVG 通过 HTML5 img
或 CSS background-images
必须有自己的 XML 声明并且应该使用以下 DOCTYPE 声明:
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
关于外部 SVG 的说明(更新)(感谢,@user314159):
根据 1.3. SVG namespace and DTD of the Scalable Vector Graphics (SVG) 2 W3C Working Draft 09 July 2015:
A DTD is not provided in this specification, as the use of DTDs for
validating documents is known to be problematic. In particular, DTDs
do not handle namespaces gracefully and the range of constraints they
can express is limited. It is recommended that authors do not include
a DOCTYPE declaration in SVG documents.
[已强调。]
我在这里 https://oreillymedia.github.io/Using_SVG/extras/ch01-XML.html 找到了不需要 XML 和 DOCTYPE 声明的信息...
The XML declaration is only required if you are using a non-Unicode character encoding (technically, anything other than UTF-8 or UTF-16).
You may also include an SGML DOCTYPE declaration, but it is no longer recommended for SVG.
在 HTML5 中使用 SVG 时:SVG
是否需要 XML 声明<?xml version="1.0" encoding="UTF-8"?>
- 作为图像来自
<img>
或 - as CSS
background-image
s?
这与“Are SVG parameters such as 'xmlns' and 'version' needed”. The namespaces issues are clarified as necessary by the two answers and the MDN Namespace crash course.
略有关联但是 SVG 1.1 不包含关于 XML 声明的必要性或何时可以省略的声明?
没有声明的例子:
<svg xmlns="http://www.w3.org/2000/svg" width="200" height="200" viewBox="0 0 200 200">
<circle id="circle--red" cx="30" cy="30" r="30" fill="#f00"/>
</svg>
2016-07-04 更新:澄清问题是关于 XML 声明。谢谢@Martin Honnen! 2017-10-24更新:更改为“UTF-8“ uppercase和SVGO优化属性顺序。
对于HTML5,正确的DOCTYPE declaration是
<!DOCTYPE html>
需要给浏览器指定全标准模式。
你展示了什么,
<?xml version="1.0" encoding="utf-8"?>
是一个XML declaration. It is optional for XML 1.0 and required for XML 1.1,但是
- XML 1.1 并未广泛使用。
version="1.0"
和encoding="utf-8"
无论如何都是默认值。
当您希望指定不同的编码时,请在 HTML5 中使用 XML 声明,尤其是当文件可能不仅被浏览器而且还被 XML 处理器使用时。
有关详细信息,请参阅 HTML5: A vocabulary and associated APIs for HTML and XHTML。
关于内部 SVG 的注意事项(感谢,@Quentin): SVG embedded within an HTML5 document should not have an independent XML declaration. Only one XML declaration is allowed in well-formed XML, and it must be at the top, if anywhere. See this answer 了解有关 XML 声明放置要求的更多详细信息。
关于外部 SVG 的注意事项(谢谢,@Kaiido):SVG 通过 HTML5 img
或 CSS background-images
必须有自己的 XML 声明并且应该使用以下 DOCTYPE 声明:
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
关于外部 SVG 的说明(更新)(感谢,@user314159):
根据 1.3. SVG namespace and DTD of the Scalable Vector Graphics (SVG) 2 W3C Working Draft 09 July 2015:
A DTD is not provided in this specification, as the use of DTDs for validating documents is known to be problematic. In particular, DTDs do not handle namespaces gracefully and the range of constraints they can express is limited. It is recommended that authors do not include a DOCTYPE declaration in SVG documents.
[已强调。]
我在这里 https://oreillymedia.github.io/Using_SVG/extras/ch01-XML.html 找到了不需要 XML 和 DOCTYPE 声明的信息...
The XML declaration is only required if you are using a non-Unicode character encoding (technically, anything other than UTF-8 or UTF-16).
You may also include an SGML DOCTYPE declaration, but it is no longer recommended for SVG.