5

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?

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? 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. Commented Sep 13, 2010 at 21:47

3 Answers 3

4

If there is no <script> tag in the code to be replaced you can try (demo):

container.innerHTML = container.innerHTML
                               .replace(/<(\/?)([^:>\s]*:)?([^>]+)>/g, "<$1$3>")
Sign up to request clarification or add additional context in comments.

2 Comments

Awesome you rock, this is exactly what I needed!
This fails for me with this XML <s:Envelope xmlns:s="http://test.org"><s:Body><ExecuteResponse xmlns="http://test.org"></ExecuteResponse></s:Body></s:Envelope> It looks like the http: confuses it and it returns this: <Envelope xmlns:s="http://test.org"><Body><//test.org"></ExecuteResponse></Body></Envelope>
0

This isn't really an answer, but you'd be better off handling this server-side. JavaScript comes 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).

1 Comment

For this specific project I can't do it server side unfortunately.
0

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

1 Comment

Thanks, but the main thing is that i want it to do that, but also add the params also...?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.