使用 xml.setNamespace() 时不添加名称空间
每当我调用 xml.setNamespace(ns)
时,元素的命名空间都会设置为 ns
,但 ns
也会添加为另一个命名空间它是元素自己的前缀。我想知道如何阻止后者发生(我可以修改 XML.prototype.function::setNamespace
)而不定义 @xmlns
,因为我可以' t 使用 E4X 语法。或者,不使用 E4X @attribute 语法的 XML.prototype.function::setAttribute
(当然,除了使用 function::
来定义它)会更好。
例子:
var xhtml = new Namespace("http://www.w3.org/1999/xhtml"),
xml = <foo/>;
xml.setNamespace(xhtml);
// what I get:
xml.toXMLString() ===
<foo
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns="http://www.w3.org/1999/xhtml"
/>.toXMLString();
// what I want:
xml.toXMLString() ===
<foo
xmlns="http://www.w3.org/1999/xhtml"
/>.toXMLString();
Whenever I call xml.setNamespace(ns)
, the namespace of the element is set to ns
, but ns
is also added as another namespace with it's own prefix to the element. I would like to know how to stop the latter from happening (I'm okay with modifying XML.prototype.function::setNamespace
) without defining @xmlns
as I can't use E4X syntax. Alternatively, an XML.prototype.function::setAttribute
that doesn't use E4X @attribute syntax (except of course for the one use of function::
for defining it) would be even better.
Example:
var xhtml = new Namespace("http://www.w3.org/1999/xhtml"),
xml = <foo/>;
xml.setNamespace(xhtml);
// what I get:
xml.toXMLString() ===
<foo
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns="http://www.w3.org/1999/xhtml"
/>.toXMLString();
// what I want:
xml.toXMLString() ===
<foo
xmlns="http://www.w3.org/1999/xhtml"
/>.toXMLString();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道答案,但我通过 Rhino 1.7r2 运行了你的代码,它返回了不同的结果。这要么与实现相关,要么我们的 E4X 实现之一存在错误。不知道是哪个。
在 Rhino 1.7r2 上:
看起来您遇到了古老的“命名空间前缀应该微不足道,但在现实世界中它们实际上很重要”的问题。 :(
I don't know the answer, but I ran your code through Rhino 1.7r2, and it returned different results. Either this is implementation-dependent or one of our E4X implementations is buggy. dunno which.
on Rhino 1.7r2:
Looks like you've run into the age-old "namespace prefixes are supposed to be insignificant, but in the real world they are actually significant" problem. :(
我满足于仅仅获得
...
所以我就去坚持使用命名空间(new Namespace(name, nsURI)
)。I'm content with just getting
<xhtml:my-root xmlns:xhtml="...">...</xhtml:my-root>
so I'm just going to stick with using a named namespace (new Namespace(name, nsURI)
).