Declaring a Javascript variable in an XSL document

In my target endpoint pre flow, in response, I added step javascript and declared variable by context.setVariable. The next step is xslt transformation, and in that step I want to access my declared variable from javascript. Is it possible?

You first need to define the variables in the XSL Transform Policy. Then, you need to define them in the XSL file itself. Then you can reference the variables in the XSL as you would with any defined variable.

Reading here:

http://apigee.com/docs/api-services/reference/xsl-transform-policy

There are optional elements noted in the Element Reference section. Let's say you are setting variable named "myCustomVariable1" in your Javascript, and you also want to insert a query parameter (for example, userid) into your XSL. Use the following XSL Transform policy as an example:

<XSL name="MyXSL">
    <ResourceURL>xsl://MyXSL.xsl</ResourceURL>
<!-- Begin Optional Elements Section -->
<Parameters ignoreUnresolvedVariables="true">
<Parameter name="myCustomVariable1" ref="myCustomVariable1" />
<Parameter name="userId" ref="request.queryparam.userid" />
</Parameters>
<!-- End Optional Elements Section -->
</XSL>

Then, in the resources/xsl path for the XSL file, declare the variables using xsl:param, per the following. Then you can reference the value-of in the Body:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:myns="http://example.com/messages">
	<xsl:param name="myCustomVariable1"/>
	<xsl:param name="userId/>
	<xsl:output method="xml" indent="no"/>
	<xsl:template match="/Root">
		<soapenv:Envelope>
		<soapenv:Header/>
		<soapenv:Body>
			<myns:MyMessage>
				<myns:myCustomVariable1><xsl:value-of select="$myCustomVariable1"/></myns:myCustomVariable1>
  				<myns:userId><xsl:value-of select="$userId"/></myns:userId>
  			</myns:MyMessage>
  		</soapenv:Body>
		</soapenv:Envelope>
	</xsl:template>
</xsl:stylesheet>

I hope this helps. Many thanks to @Mike Dunker for the sample code.

Version history
Last update:
‎02-11-2015 05:04 PM
Updated by: