3

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:

  • Instead of depending on XSLT after the $.ajax call, I could just parse the XML returned and manually emit HTML table rows (yuck).
  • Do a POST without Ajax and fetch the transformed XML with the account data included

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

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.