使用 xml.setNamespace() 时不添加名称空间

发布于 2024-08-08 22:19:33 字数 764 浏览 10 评论 0原文

每当我调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白日梦 2024-08-15 22:19:33

我不知道答案,但我通过 Rhino 1.7r2 运行了你的代码,它返回了不同的结果。这要么与实现相关,要么我们的 E4X 实现之一存在错误。不知道是哪个。

在 Rhino 1.7r2 上:

var xhtml = new Namespace("http://www.w3.org/1999/xhtml"),
xml = <foo/>;
xml.setNamespace(xhtml);

js> xml.toXMLString()
<e4x_0:foo xmlns:e4x_0="http://www.w3.org/1999/xhtml"/>

看起来您遇到了古老的“命名空间前缀应该微不足道,但在现实世界中它们实际上很重要”的问题。 :(

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:

var xhtml = new Namespace("http://www.w3.org/1999/xhtml"),
xml = <foo/>;
xml.setNamespace(xhtml);

js> xml.toXMLString()
<e4x_0:foo xmlns:e4x_0="http://www.w3.org/1999/xhtml"/>

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. :(

陌路终见情 2024-08-15 22:19:33

我满足于仅仅获得 ... 所以我就去坚持使用命名空间(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)).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文