Parsing XML input from web form into namespaced xml file

J

Jason

I have a web form that will accept XML as input whose contents need to be
namespaced, and inserted into an XML document with a different namespace.



I need to take this:

Lorem ipsum dolor sit amet, consectetuer <a href="http://www.example.com"
title="etc" rel="whatever">Link Here</a> adipiscing elit. Vivamus molestie
dolor ultrices leo.

And stick it into this:

<root xmlns="http://some/default/ns"
xmlns:xhtml="http://www.w3.org/1999/xhtml">
<a>a</a>
<b>b</b>
<content>Lorem ipsum dolor sit amet, consectetuer <xhtml:a
href="http://www.example.com" title="etc" rel="whatever">Link Here</xhtml:a>
adipiscing elit. Vivamus molestie dolor ultrices leo.</content>
</root>


The form is being processed client side and the parent XML document is being
generated completely by Javascript using W3C XML DOM (need only work in
Firefox). The XML document is then POSTed to a PHP script that is doing
some additional processing to the xml document with SimpleXML and DOM.
Currently, the form contents are being added to the xml document and the xml
fragments are entity escaped. Any and all suggestions are welcome. Any
method of solution is acceptable (JavaScript, PHP, or XSLT, whatever works).
Just keep in mind the form contents must be namespaced differently from the
parent document namespace.

Thank you.
J.K
 
J

Joe Kesselman

Lorem ipsum dolor sit amet, consectetuer <a href="http://www.example.com"
title="etc" rel="whatever">Link Here</a> adipiscing elit. Vivamus molestie
dolor ultrices leo.

Well, the first problem is that this isn't a complete well-formed XML
document. It is well formed as an XML fragment, but that means that to
parse it you will probably to wrap a top-level element around it (which
can assert whatever default namespace you prefer, while you're at it),
parse that combined text, then take the output of that parse (minus the
wrapper element) and insert that into the appropriate point in the DOM
(using importNode to obtain nodes which are compatable with the tree you
want to insert them into). As long as the parse was namespace-aware, so
you get namespace-aware DOM nodes, all the right things should happen.

Can't help you with the string manipulation and parser invocation; I
don't do browser scripting, and I suspect Firefox doesn't yet support
the DOM3 load APIs (though I'd be glad to be wrong).
 
J

Jason

Thanks. I don't know why I didn't think of that. My brain turns to mush
after hours of working on the same little thing. Here's out it finally
worked:

var xmlString = '<description
xmlns="'+twix.html_ns+'">'+textarea.val()+'</description>';
// code for IE
if (window.ActiveXObject){
var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.loadXML(xmlString);
}
// code for Mozilla, Firefox, Opera, etc.
else{
var parser=new DOMParser();
var xmlDoc=parser.parseFromString(xmlString,"text/xml");
}

var root=xmlDoc.documentElement;
root = el.ownerDocument.importNode(root,true);//el is the element where we'd
like to put this junk
while(root.hasChildNodes()){
el.appendChild(root.removeChild(root.firstChild));
}



If you have any comments on that, I'd love to hear them. It *looks* like
all that should work in IE but I'm not sure that importNode() is supported
yet. But it works in Fx so that's all I care!

Thanks again,
~J
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top