开发者

Not adding a namespace when using xml.setNamespace()

开发者 https://www.devze.com 2022-12-09 06:09 出处:网络
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 la

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 = ne开发者_如何学Gow 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();


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号