createElement vs. createElementNS

down vote accepted

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 standardizes, 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.


27 down vote accepted

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 standardizes, 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.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值