Is there an easy way, for example, to drop an XML name space, but keep the tag as is with jQuery or JavaScript? For example:
<html:a href="#an-example" title="Go to the example">Just an Example</html:a>
And change it to:
<a href="#an-example" title="Go to the example">Just an Example</a>
On the fly with jQuery or JavaScript and not knowing the elements and or attributes inside?
Is there an easy way, for example, to drop an XML name space, but keep the tag as is with jQuery or JavaScript? For example:
<html:a href="#an-example" title="Go to the example">Just an Example</html:a>
And change it to:
<a href="#an-example" title="Go to the example">Just an Example</a>
On the fly with jQuery or JavaScript and not knowing the elements and or attributes inside?
Share Improve this question edited Oct 11, 2010 at 17:06 Dimitre Novatchev 244k27 gold badges303 silver badges437 bronze badges asked Sep 13, 2010 at 21:11 Oscar GodsonOscar Godson 32.8k42 gold badges125 silver badges206 bronze badges 2- To be clear, am I correct in thinking that this is after being served as text/html and therefore parsed as HTML and not XHTML. In which case, this isn't a question about XML or namespaces, but about converting elements from one node name to another by dropping the characters in the name preceding and including a colon? – Alohci Commented Sep 13, 2010 at 21:36
- Yes. The file is served up as HTML, not XHTML (it has a DOCTYPE of <!DOCTYPE html>), and, technically, yes its not XML namespaces, but i figured that there might be a way to parse the HTML as XML to strip out the XML namespaces is all because $('html:a') or getElementByTagName with html:a returns unrecognized expression. – Oscar Godson Commented Sep 13, 2010 at 21:47
3 Answers
Reset to default 4If there is no <script>
tag in the code to be replaced you can try (demo):
container.innerHTML = container.innerHTML
.replace(/<(\/?)([^:>\s]*:)?([^>]+)>/g, "<$1$3>")
This isn't really an answer, but you'd be better off handling this server-side. JavaScript es into the picture a bit too late for this kind of task... events may have already been attached to the existing nodes and you'd be processing each element twice.
What is the purpose of the namespace?
If these are static html files and the namespace serves no purpose I would strip all the namespaces with a regex.
If they're not static and the namespace has a purpose when served as xml, you could do some server-side detection to serve with the right doctype and the namespace (when appropriate).
It's easy when you know how ... just use \\ to escape the colon so it's not parsed as an action delimiter by the jquery parsing engine.
$('html\\:a').each( function(){
var temp = $(this).html();
$(this).replaceWith("<a>"+temp+"</a>");
});
This should iterate between each of those elements and replace them with normal tags. Since these are being served up to an ajax callback function, otherwise I don't see why you'd want to do it on the fly ... then the top line would change to:
$.post('...',{}, function(dat){
$(dat).find('html\\:a').each( blah blah ....
.
.
});
NB, i'm one of those terrible people who only really tests things in FF ...