删除自动添加的xmlns属性 [英] Removing xmlns attributes added automatically

查看:119
本文介绍了删除自动添加的xmlns属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Javascript 中执行一个搜索一段文本的简单搜索和突出显示功能。发出该文本的 XHTML 标记也作为参数提供,以便在查找该文本时提供其他帮助。

I am trying to execute a simple search and highlight function in Javascript that searches for a piece of text. The XHTML tag with which that piece of text occurs is also given as an argument for additional help in locating that text.

我正在测试此功能的XHTML:

The XHTML that I am testing this function out on:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
 "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta content="application/xml+xhtml;charset=UTF-8" />
<script src="searcher.js" type="text/javascript"></script>
<script src="jquery-2.0.2.min.js"> </script>
<title>Building your resume</title>
</head>
<body id="highlightbegin">
<h1>Building your resume</h1>

<div> <input name="input" type="button" value="Highlight3" onclick="javascript:searcher('&lt;h1&gt;','Building your resume', '&lt;h1&gt;Building your resume', 'resume');" /> </div>

</body>
</html>  

searcher.js中的函数搜索器:

The function searcher in searcher.js:

function searcher(tag, text, tagText, word) {

    //simple search.
    console.info(word + " to be searched for in " + text + " with tag text = " + tagText);

    //get old html.
    var oldHTML = document.getElementById("highlightbegin").innerHTML;

    //get regexp.
    var regexp = new RegExp(tagText, 'g');

    var match = oldHTML.match(regexp);
    console.info(text + " found " + match.length + " times.");
}  

然而,执行 RegExp ,匹配返回 null 。进一步的调查显示标签< h1>建立您的简历< / h1> 变为< h1 xmlns =http://www.w3 .org / 1999 / xhtml>构建简历< / h1> ,这会导致匹配函数返回 null 。我的问题:

However, executing the RegExp, match returns null. Further investigation reveals that the tag <h1>Building your resume</h1> becomes <h1 xmlns="http://www.w3.org/1999/xhtml">Building your resume</h1> which causes the match function to return null. My questions:


  1. 为什么自动添加 xmlns 属性?

  2. 有没有办法阻止添加属性?

  3. 该属性会添加哪些标签?假设它将被添加到每个标签是否安全?

  4. 这是特定于浏览器的问题还是可以在所有浏览器中出现这种情况?

  1. Why is the xmlns attribute added automatically?
  2. Is there a way to prevent the attribute from being added?
  3. What tags will that attribute be added to? Is it safe to assume that it will be added to every tag?
  4. Is this a browser-specific issue or can this behavior be expected in all browsers?

编辑:

观察:

1 。如果我将 xmlns 属性添加到正文标记,并使用 outerHTML <访问所有内容/ code>( var oldHTML = document.getElementById(highlightbegin)。outerHTML; ),其子元素没有 xmlns 属性。

我的问题:

1.可以编辑outerHTML元素(使用 Javascript )和替换?

2.上面的观察结果是否一致(每次调用 outerHTML 时看到)还是依赖于实现?

3.是否 Javascript 会自动添加 xmlns 属性还是浏览器?


An observation:
1. If I add the xmlns attribute to the body tag and access all content with outerHTML (var oldHTML = document.getElementById("highlightbegin").outerHTML;), its child elements do not have the xmlns attribute.
My questions:
1. Can the outerHTML element be edited (with Javascript) and replaced?
2. Is the observation above consistent (seen each time outerHTML is invoked) or is it implementation dependent?
3. Is it Javascript that adds the xmlns attribute automatically or the browser?

推荐答案

Why is the xmlns attribute added automatically?

因为如果没有,则标记不能代表元素的名称空间DOM,在这种情况下,如果你将字符串写回DOM,浏览器将不再将元素解释为HTML元素,并且您的页面将会中断。

Because if it didn't, the markup wouldn't be representative of the namespaces of the elements in the DOM, in which case if you wrote the string back to the DOM the elements would no longer be interpreted by the browser as HTML elements, and your page would break.

Is there a way to prevent the attribute from being added?

如果您使用的是XMLDocument,那么就不是使用innerHTML。如果需要,您可以通过遍历DOM来创建自己的序列化程序。

Not with innerHTML, if you're using an XMLDocument, which it seems you are. You could create your own serializer by walking the DOM if you wanted.

What tags will that attribute be added to? Is it safe to assume that it will be 
added to every tag?

至少每个开始标记都是你调用innerHTML的元素的顶级子标记,假设子节点不在null命名空间中。加上与其父级不同的命名空间中的任何后代元素的开始标记。如果浏览器选择这样做,将它添加到所有开始标记是没有错的。

At least every start tag that's a top level child of the element on which you're calling innerHTML, assuming that child is not in the null namespace. Plus the start tag of any descendent element in a different namespace to its parent. It wouldn't be wrong to add it to all the start tags if the browsers chose to do so.

Is this a browser-specific issue or can this behavior be expected in all browsers?

使用innerHTML序列化DOM在浏览器之间传统上有所不同。虽然浏览器一致地执行,但我不会依赖它。

Serializing the DOM with innerHTML has traditionally varied between browsers. Although browers should do it consistently, I wouldn't rely on it.

1. Can the outerHTML element be edited (with Javascript) and replaced?

理论上是的,但它无济于事。你必须用null命名空间中的一个替换元素来停止出现在外部元素上的属性,这只会导致outerHTML将属性添加到子元素(因为它们将具有与其父元素不同的命名空间) )。

In theory yes, but it won't help. You'd have to replace the element with one in the null namespace to stop the attribute appearing on the outer element, and that would just cause outerHTML to add the attribute to the child elements (because they would then have a different namespace to their parent).

2. Is the observation above consistent (seen each time outerHTML is invoked) or is 
   it implementation dependent?

出于与innerHTML相同的原因,序列化必须有的地方添加属性,以便可以成功读回字符串,并将浏览器想要添加 的位置添加回来。不保证它会保持一致。

For the same reason as with innerHTML, there are places where the serialization has to add the attribute so the string can be read back in successfully, and places where it may be added it the browser wants to. There's no guarantee that it'll be consistent.

3. Is it Javascript that adds the xmlns attribute automatically or the browser?

这是浏览器将DOM序列化为字符串的内置过程。该属性通常不在DOM中的元素上(< html> 元素是正常的异常),它被添加到浏览器认为必要的地方它被转换为字符串。

It's the browser's in-built process of serializing the DOM to a string. The attribute usually isn't on the element in the DOM (the <html> element is the normal exception), it gets added where-ever the browser thinks is necessary as it gets converted to a string.

更一般地说,这是专家试图阻止尝试使用正则表达式处理HTML标记的一个原因。即使使用HTML而不是XHTML,也不需要担心这个命名空间业务,在字符串和DOM表单之间进行转换时,可以添加和删除属性,并且可以以任意方式更改它们的顺序,而不保证它们之间的一致性浏览器制作,甚至是同一浏览器的连续版本。

On a more general note, this is one reason why the experts try to discourage attempting to process HTML mark-up with regular expressions. Even with HTML as opposed to XHTML, where there's none of this namespace business to worry about, during conversion between the string and DOM forms, attributes can get added and removed, and their order can get changed in arbitrary ways with no guarantees about consistency between browser makes, or even successive versions of the same browser make.

这篇关于删除自动添加的xmlns属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆