在客户端 XSL 中访问 JavaScript 变量

发布于 2024-11-16 18:27:15 字数 1891 浏览 1 评论 0原文

我正在通过客户端提供的 Web 服务从 SharePoint 2007 系统创建报告(这都是由于开发人员的限制 - 我知道,如果我有权限,整个练习在 SharePoint 设计器中将非常简单)。

目前我有一份工作报告。在 JavaScript 中,我传入三个参数(“从”日期、“到”日期和“业务类别”),用于生成对 Web 服务的 CAML 查询。返回 XML 响应,(在进行一些格式清理之后)导入 XSLT(外部文件)并直接应用到它,并将结果泵送到页面上的 DIV 中。一切运作良好。

然而,此时我想在报告上实际显示输入参数(日期和类别)。我至少有两个笨拙的选项来执行此操作:

1)输出显示 DIV 之外的值。这会起作用,但不是很通用。

2) 输出 XSL 中值的占位符,然后在显示之前进行一系列替换。这感觉……很奇怪。

3) 在转换之前手动将具有所需值的节点添加到 XML 数据包中,然后在 XSLT 中正常访问它们。这对我来说似乎是最干净的......但也有一些我不确定我喜欢的包袱。

有没有“正确”的方法来做到这一点?有可能是其中之一吗?

下面是一些(缩写的)代码来说明:

    // 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", "LookupList");
    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);

};

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", "LookupList");
    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);

};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没有伤那来痛 2024-11-23 18:27:15

您可以在 javascript 中将参数传递给 XSLT,只需将

放在样式表的根目录中即可。查看 JavaScript 中 XSLTProcessor 对象的 setParameter 方法。这是我使用的 javascript 方法:

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;
}

前两个之外的任何参数都被视为名称/值对,作为参数传递给 xslt。 inputXmlxslt 都是 XML 文档。

编辑:刚刚注意到,我忘了提到这个函数使用了一个辅助方法“makeDocFromString”,它只将 XML 文档的源作为字符串输入,并返回一个实际的 XML 文档。该函数在 .js 的其他地方定义,它不是标准库的一部分。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文