calling javascript from XSLT

Posted on 2008-11-18
Medium Priority
Last Modified: 2013-11-18

I need to call webservice from xslt in HTTP POST Method. The MSXML should be used. I am using javascript.

I have an XML File which is having Integer Node.....Shown Below
<?xml version="1.0" encoding="utf-8" ?>

XSLT will call javascript and pass "Integer" node to javascript. The javascript will create the SOAP message and call webservice.
The soap message is....

  <?xml version="1.0" encoding="utf-8" ?>
    - <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <AddTwoNumbers xmlns="http://localhost/wwwroot/addnumbers/Service1">

Please tell me how to write the code in xslt.



The Web method is......

using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml;

/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://www.tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService : System.Web.Services.WebService {

    public WebService () {
        //Uncomment the following line if using designed components

    //public string HelloWorld()
    //    return "Hello World";
    public int WebAdd(int x, int y)
        return x + y;
    public int WebMultiply(int x, int y)
        return x * y;
    public void testmethod(XmlDocument xd)

Question by:smaranika_sikdar
LVL 75

Expert Comment

by:Michel Plungjan
ID: 22992163
LVL 60

Accepted Solution

Geert Bormans earned 750 total points
ID: 22992359
well, let me start by saying that I STRONGLY recommend that you don't try it this way.
SOAP messages from within XSLT is the other away round

- you cannot rely on the fact that the JavaScript (or JScript in this case) engine in the XSLT will allow exactly what the scripting engine outside the XSLT allows, not all libraries will be loaded. So it might be possible in one architecture and not possible in another. This is all highly undocumented
- XSLT has no predefined flow order of the programm... if you will be trying very procedural stuff inside an XSLT, you might get unpredictable behaviour (variables not being set when you expect them to be set, etc...)
- main point: you would not be able to report back to the XSLT. Grabbing Error messages from the Soap connection and putting the message in the output tree will fail because of the previous point.
This will lead to programms that will be very hard to debug and maintain

I would do this the other way round.
Call the XSLT from the JavaScript and use the XSLT results in the SOAP call!

I can imagine a three step to do what you need to do.
- transform the XML using XSLT, resulting in a small and easy XML snippet containing the a and b value (might not be necessary in this case)
- pull the a and b value out the XML using simple DOM methods, call the SOAP with the extracted values
- push the result as a parameter in a new XSLT transformation (the one where you wanted to use the result from the soap call)

This would work if you only intended to do one soap call in the XSLT

If you need the soap calls on multiple nodes:
- transform the XML collecting the multiple nodes in a simplyfied XML,
- make a nodeset from the simplified XML, loop over the nodeset and call the soap server for each node
- Make the real transformation XSLT, put identifiable placeholders in the XSLT result, where you expected the soap results
- merge the soap call results with the XSLT results by streaming trought the serialised XSLT result and replacing the placeholders

just a thought



Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
The viewer will learn how to count occurrences of each item in an array.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question