AJ.
AJ.

Reputation: 28174

Using jQuery to Update XML Doc and Perform XSLT

I'm writing a small web app to display mail server settings to my users. They input their email address, submit the form, and it should return the correct settings.

The tools I'm using are XML + XSL for the UI, and jQuery to handle the data retrieval. Initially, my application has no context so the XML data is not available. I just load a basic XML document linked to my XSL stylesheet to show the user the form.

<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="mail-settings.xsl" ?>
<Autodiscover xmlns="http://schemas.microsoft.com/exchange/autodiscover/responseschema/2006">
    <Response xmlns="http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a">
        <Account/>
    </Response>
</Autodiscover>

When the form is submitted, the jQuery ajax call fetches the account settings as XML. What I want to do after I get the response is to update the current document to include the Account info and then have the XSL stylesheet update the page.

$.ajax({
    type: "POST",
    url: "https://myfooserver.com/maildata.xml", //actually points to a wsgi app that returns XML
    data: xml_request,
    dataType: "xml",
    async: false,
    success: function(account_data){
        $( "Account" ).replaceWith(account_data);
    },  
    error: function (request, status, error){
        alert("Error handler request.responseText: " + request.responseText);
    }   
}); 

But as I'm discovering, nothing is "refreshing" the XSL transformation after I modify the DOM.

Other options I've thought of include:

I was hoping there was some way I could update the XML dynamically on the client side to have the settings appear, as HTML using XML + XSLT. Any thoughts on getting my preferred solution to work?

Upvotes: 3

Views: 1559

Answers (1)

Luigi Bai
Luigi Bai

Reputation: 21

Have you taken a look at MagicXML? This library was released under "MIT License", and in relevant part, uses javascript XSLT processing:

    xslt = new XSLTProcessor();
    xslt.importStylesheet(xsl); //NOTE: this is a string with XML

    // If we have a parameters array, set the values in the XSLT.
    if (parameters !== undefined) {
        for (i; i &lt; parameters.length; i++) {
            parameter = parameters[i];
            xslt.setParameter(
                (parameter.xmlns !== undefined) ? parameter.xmlns : null, 
                parameter.name,
                parameter.value
            );
        }
    }
    return xslt.transformToFragment(xml, document); // NOTE: xml is a string

and

    template = new ActiveXObject("MSXML2.XSLTemplate.6.0");

    template.stylesheet = xsl; // NOTE: a string with XSLT
    processor = template.createProcessor();
    processor.input = xml; // NOTE: a string with XML

    // If we have a parameters array, set the values in the XSLT.
    if (parameters !== undefined) {
        for (i; i < parameters.length; i++) {
            parameter = parameters[i];
            processor.addParameter(
                parameter.name, 
                parameter.value,
                parameter.xmlns
            );
        }
    }

    processor.transform();
    return processor.output;

Disclaimer: I have not used this library myself.

Upvotes: 1

Related Questions