createElement vs. createElementNS [英] createElement vs. createElementNS
问题描述
未来持有的常规
createElement
? Svg是xml,而不是html。我明白了。所以我们使用 createElementNS(ns_string,'svg')
然后 setAttributeNS(null ,,)
。为什么?
为什么不 setAttributeNS('my_ns',,)
?
为什么必须 ns_string
是 http://www.w3.org/2000/svg
而不是一些随机字符串?如果只有一个命名空间,命名空间的目的是什么?
在常规html中 ns
的目的是什么?我应该将现有代码中的 createElement
的所有实例更改为 createElementNS
?
我正在阅读 DOM-Level-2 规范。但是我仍然感到困惑。
要了解命名空间尝试解决的问题,请考虑文件扩展名。 3个字母的文件扩展名描述文件的内容做的很糟糕。它们含糊不清,不携带版本信息。 XML命名空间使用更大的字符串空间,URI来解决相同的问题,并使用短的前缀,以便您可以在同一文档中简洁地混合多种XML。
如果只有一个名称空间,命名空间的目的是什么?
有很多命名空间用于标识不同类型的XML和不同版本的这些类型。
SVG和MathML是两种XML,每个都有自己的可以嵌入HTML5的命名空间,而且它们经常使用XLink,另一个XML命名空间。许多其他XML模式(使用相应的命名空间)用于在客户端和服务器之间传递消息以及数据存储。
XHTML是将HTML表示为有效的XML。它有自己的命名空间。
所以我们使用createElementNS(ns_string,'svg')然后setAttributeNS(null ,,)。为什么?为什么不setAttributeNS('my_ns',,)?
你应该尝试一直使用 setAttributeNS使用命名空间URI使用
。 createElementNS
时,使用命名空间URI
XML已定义在多个步骤。规范的第一个版本没有关于命名空间,但留下足够的语法,以便可以通过使用前缀和特殊的 xmlns
属性,在XML之上指定具有命名空间的XML,而无需使用命名空间。 XML规范说:
XML建议书中的命名空间[XML名称]为包含冒号字符的名称分配含义,因此作者不得以除名称空间之外的XML名称中使用冒号, XML处理器必须接受冒号作为名称字符。
XML命名空间让XML处理应用程序知道他们在处理什么,允许多种XML混合在同一个文档中。
为什么ns_string必须是 http://www.w3.org/2000/svg
这包括SVG的标准化年份,2000年,所以它带有有用的信息。
与 xmlns:svg
它也让浏览器知道 svg:
前缀表示SVG,而不是XML的其他方言。
What's the real difference between those two? I mean real, essential difference.
What's the future holding for regular createElement
?
Svg is xml, not html. I get that. So we use createElementNS(ns_string, 'svg')
And then setAttributeNS(null,,)
. Why?
Why not setAttributeNS('my_ns',,)
?
Why must ns_string
be http://www.w3.org/2000/svg
and not some random string? What's the purpose of a namespace if there is only one namespace?
What's the purpose of ns
in regular html? Should I change all instances of createElement
to createElementNS
in my existing code?
I am reading the DOM-Level-2 spec. but I'm still puzzled.
To understand the problem namespaces are trying to solve, consider file extensions. 3-letter file extensions have done a really bad job of describing the content of files. They're ambiguous and don't carry version info. XML namespaces use a larger space of strings, URIs, to solve the same problem, and use short prefixes so you can succinctly mix multiple kinds of XML in the same document.
What's the purpose of namespace if there is only one name space?
There are many namespaces used to identify different kinds of XML, and different versions of those kind.
SVG and MathML are two kinds of XML each with their own namespaces that can be embedded in HTML5, and they often use XLink, another XML namespace. Many other XML schemas, with corresponding namespaces, are used for passing messages between clients and servers and for data storage.
XHTML is an attempt to express HTML as valid XML. It has its own namespace.
So we use createElementNS(ns_string, 'svg') And then setAttributeNS(null,,). Why? Why not setAttributeNS('my_ns',,)???
You should probably try to consistently use setAttributeNS
with a namespace URI when using createElementNS
with a namespace URI.
XML was defined in multiple steps. The first version of the spec said nothing about namespaces but left enough syntax so that XML with namespaces could be specified on top of XML without namespaces by using prefixes and special xmlns
attributes. The XML specification says:
"The Namespaces in XML Recommendation [XML Names] assigns a meaning to names containing colon characters. Therefore, authors should not use the colon in XML names except for namespace purposes, but XML processors must accept the colon as a name character."
XML namespaces let XML processing applications know what they're dealing with, and allow multiple kinds of XML to be mixed together in the same document.
Why ns_string must be "http://www.w3.org/2000/svg"
This includes the year that version of SVG was standardized, 2000, so it carries useful information.
When used with xmlns:svg
it also lets the browser know that the svg:
prefix means SVG and not some other dialect of XML.
这篇关于createElement vs. createElementNS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!