开发者

Accessing JavaScript Variables in XSL, Client-Side

开发者 https://www.devze.com 2023-03-14 06:44 出处:网络
I\'m creating reporting from a SharePoint 2007 system via the provided WebServices on the client-side (this is all due to developer restrictions - I know, if I had permission, this entire exercise wou

I'm creating reporting from a SharePoint 2007 system via the provided WebServices on the client-side (this is all due to developer restrictions - I know, if I had permission, this entire exercise would be perfectly simple in SharePoint designer).

Currently I have a working report. In JavaScript I pass in three parameters (a "From" date, "To" date and "Business Category") which are used to generate a CAML query to web service. The XML response is returned and (after some formatting clean-up) an XSLT is imported (external file) and applied directly to it and the result is pumped into a DIV on the page. All works well.

At this point however I'd like to actually display the input parameters (dates and category) on the report. I have at least a few two kludgy options to do this:

1) Output the values outside of the display DIV. This will work but isn't very versitile.

2) Ouput place-holders for the values in the XSL and then run through a series of replacements before display. This just feels... odd.

3) Manually add nodes with the desired values to the XML packet before transformation then access them normally in the XSLT. This seems the cleanest to me... but also has some baggage I'm not sure I like.

Is there a "right" way to do this? Any chance one of those is it?

Here's some (abbreviated) code to illustrate:

    // Set the URL of the XSL to apply
reportXSLURL = "BusinessCategoryReport.xsl";


    // Set the input variables.
var CurCategory = DP_QueryString.get("ForCategory", "first");
var CurFrom = DP_QueryString.get("ForFrom", "first");
var CurTo = DP_QueryString.get("ForTo", "first");


* * Soap Call Censored (Too Hot for the Internet) * * 

    // Load the data
function ProcessResponse(ResponseText) {

        // Create and load the serviceXML
    var serviceXML = new DP_XML();
    serviceXML.parse(ResponseText);

        // Create and Load the XSL
    var reportXSL = new DP_XML();
    reportXSL.load(reportXSLURL);

        // Clean Up
    CleanSharePointColumn(serviceXML.Doc, "ows_Duration", "CalculatedField");
    CleanSharePointColumn(serviceXML.Doc, "ows_Incident_x0020_Manager_x0028_s_x", "UserList");
    CleanSharePointColumn(serviceXML.Doc, "ows_Application_x0020__x0028_EAI_x00", "LookupList");
    CleanSharePointColumn(serviceXML.Doc, "ows_Business_x0020_Category", "Loo开发者_如何学PythonkupList");
    CleanSharePointColumn(serviceXML.Doc, "ows_Incident_x0020_Start", "DateTime_Min");
    CleanSharePointColumn(serviceXML.Doc, "ows_Incident_x0020_End", "DateTime_Min");

        // Present the Results
    document.getElementById("DataDisplay").innerHTML = serviceXML.Doc.transformNode(reportXSL.Doc);

};


You can pass parameters to an XSLT in javascript, just put

<xsl:param name="myparam" />

in the root of your stylesheet. Look into the setParameter method on the XSLTProcessor object in javascript. Here's a javascript method I use:

function transform(inputXml, xslt) {
    var xsltProcessor;
    var toReturn;

    if (window.XSLTProcessor) {
        xsltProcessor = new XSLTProcessor();
        xsltProcessor.importStylesheet(xslt);
        if (arguments.length > 2 && arguments.length % 2 == 0) {
            for (var i = 0; i < Math.floor((arguments.length)/2)-1; i++) {
                xsltProcessor.setParameter(null, arguments[2*i+2],arguments[2*i+3]);
            }
        }
        toReturn = xsltProcessor.transformToDocument(inputXml);
    }
    else if (window.ActiveXObject) {
        toReturn = makeDocFromString(inputXml.transformNode(xslt));
    }
    else {
        toReturn = "Unable to transform";
    }
    return toReturn;
}

Any parameters beyond the first two are treated as name/value pairs to be passed to the xslt as a parameter. inputXml and xslt are both XML Documents.

EDIT: Just noticed, I forgot to mention this function uses a helper method, 'makeDocFromString', that just takes the source of an XML document as a string input, and returns an actual XML document. That function's define elsewhere in that .js, it's not part of the standard libraries.

0

精彩评论

暂无评论...
验证码 换一张
取 消