Parsing XML input from web form into namespaced xml file

Discussion in 'XML' started by Jason, Apr 27, 2007.

  1. Jason

    Jason Guest

    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
     
    Jason, Apr 27, 2007
    #1
    1. Advertising

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



    --
    () ASCII Ribbon Campaign | Joe Kesselman
    /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
     
    Joe Kesselman, Apr 28, 2007
    #2
    1. Advertising

  3. Jason

    Jason Guest

    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
     
    Jason, Apr 28, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. impulse()
    Replies:
    0
    Views:
    2,554
    impulse()
    Oct 13, 2006
  2. Stephen Bannasch
    Replies:
    1
    Views:
    166
  3. Erik Wasser
    Replies:
    5
    Views:
    487
    Peter J. Holzer
    Mar 5, 2006
  4. andyashton
    Replies:
    3
    Views:
    136
    Martin Honnen
    Jul 6, 2007
  5. Replies:
    0
    Views:
    171
Loading...

Share This Page