SVG在HTML5中 - 什么时候是XML声明`<?xml version =“1.0” encoding =" UTF-8"?>`需要? [英] SVG in HTML5 – when is XML declaration `<?xml version="1.0" encoding="UTF-8"?>` needed?

查看:122
本文介绍了SVG在HTML5中 - 什么时候是XML声明`<?xml version =“1.0” encoding =" UTF-8"?>`需要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在HTML5中使用SVG时:SVG需要XML声明<?xml version =1.0encoding =UTF-8?> p>


  • 作为图像通过< img>

  • as CSS background-image s?



是否需要SVG参数,例如'xmlns'和'version' 。命名空间问题根据需要由 澄清 由两个答案和 MDN命名空间速成课程



但SVG 1.1不包括XML声明的必要性声明或何时可以省略?



没有声明的示例:

 < svg xmlns =http: //www.w3.org/2000/svgwidth =200height =200viewBox =0 0 200 200> 
< / svg>

更新2016-07-04 :澄清问题是关于 XML声明。感谢@Martin Honnen!
更新2017-10-24 :更改为UTF-8大写和SVGO优化属性顺序。 对于HTML5,正确的 /#doctype-declarationrel =nofollow noreferrer> DOCTYPE声明

 <!DOCTYPE html> 

需要为浏览器指定完整标准模式。


$

 <?xml version =1.0encoding =utf- 8\" >?; 

XML声明 。它是 XML 1.0的可选 XML 1.1所需的,但




  • XML 1.1并未广泛使用。
  • 无论如何,code>和 encoding =utf-8都是默认值。


当您希望指定不同的编码时,请在HTML5中使用XML声明,尤其是当文件不仅可以被浏览器使用,而且可以被XML处理器使用时。



欲了解更多信息,请参阅 HTML5:词汇表和相关API对于HTML和XHTML

https://stackoverflow.com/users/19068/quentin\">@Quentin ):嵌入在HTML中的SVG 5文档不应该有独立的XML声明。在格式良好的XML中只允许一个 XML声明,而它必须位于顶部,(如果在任何地方)。有关XML声明放置要求的详细信息,请参阅此答案



有关外部SVG的注意(感谢, @ Kaiido ):通过HTML5 img 或CSS background-images 引用的SVG必须拥有自己的XML声明并且应使用以下DOCTYPE声明:

 <!DOCTYPE svg PUBLIC -  // W3C // DTD SVG 1.1 // EN 
http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">

关于外部SVG(更新)的注意事项(谢谢,@user314159 ):

Per 1.3。 SVG名称空间和DTD 可缩放矢量图形( SVG)2 W3C工作草案2015年7月9日


此处未提供DTD规范,因为使用
验证文档的DTD是已知的问题。特别是,DTD
不能优雅地处理名称空间,并且
可以表达的约束范围是有限的。 建议作者在SVG文档中不要包含
a DOCTYPE声明。


[重点添加。]


When using SVG within HTML5: Is the XML declaration <?xml version="1.0" encoding="UTF-8"?> needed with SVG

  • as images via <img> or
  • as CSS background-images?

This is slightly related to "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.

But SVG 1.1 doesn't include statement on necessity of XML declaration or when it could be left out?

Example without declaration:

<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>

Update 2016-07-04: Clarified that question is about XML declaration. Thanks @Martin Honnen! Update 2017-10-24: Changed to "UTF-8" uppercase and SVGO optimized attribute order.

解决方案

For HTML5, the correct DOCTYPE declaration is

<!DOCTYPE html> 

It is needed to specify full standards mode to the browser.

What you've shown,

<?xml version="1.0" encoding="utf-8"?>

is an XML declaration. It is optional for XML 1.0 and required for XML 1.1, but

  • XML 1.1 isn't in widespread use.
  • version="1.0" and encoding="utf-8" are the defaults anyway.

Use an XML declaration in HTML5 when you wish to specify a different encoding, especially when the file might be consumed not just by browsers but also by XML processors.

For more information see HTML5: A vocabulary and associated APIs for HTML and XHTML.

Note regarding internal SVG (thanks, @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 for further details regarding XML declaration placement requirements.

Note regarding external SVG (thanks, @Kaiido): SVG referenced via HTML5 img or CSS background-images must have its own XML declaration and should use the following DOCTYPE declaration:

<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
          "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">

Note regarding external SVG (update) (thanks, @user314159):

Per 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.

[Emphasis added.]

这篇关于SVG在HTML5中 - 什么时候是XML声明`&lt;?xml version =“1.0” encoding =&quot; UTF-8&quot;?&gt;`需要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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