How should the the optional parameters on the XSL Transform policy be used?

The XSL Transform policy allows optional parameters to be set on the policy, per this topic:

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

and per the schema:

https://github.com/apigee/api-platform-samples/blob/master/schemas/policy/xsl.xsd

How are those parameters designed to be used? Can you provide an example or two?

Solved Solved
2 4 4,007
1 ACCEPTED SOLUTION

Yes, here's an example. Suppose you have an XSL policy like this:

<XSL name="XSL-SubscriberNumbers-XformRequest">
  <DisplayName>XSL - SubscriberNumbers - Request</DisplayName>
  <FaultRules/>
  <Properties/>
  <OutputVariable>request.content</OutputVariable>
  <Parameters ignoreUnresolvedVariables="false">
    <Parameter name="uid" ref="authn.uid"/>
    <Parameter name="pwd" ref="authn.pwd"/>
  </Parameters>
  <ResourceURL>xsl://SubscriberNumbers-Request.xsl</ResourceURL>
  <Source>request</Source>
</XSL>

We can see from reading it, it applies to the request message, and transforms the content in place. (returning the output to request.content).

This policy specifies two parameters, uid and pwd, which will be passed to the XSL. The values for those parameters are obtained from context variables, in this case, they are named authn.uid and authn.pwd , respectively. Values for those context variables need to have been set prior to the evaluation of this XSL policy.

Now let's look at the actual XSL sheet. You would refer to the passed parameters by name at the top of the XSL like this:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:cdm="http://example.com/cheesodatamodel.xsd">


  <xsl:param name="uid" select="''"/>
  <xsl:param name="pwd" select="''"/>


  <xsl:output method="xml" omit-xml-declaration="yes" indent="no" media-type="string"/>


  <xsl:template ...>
  ...
</xsl:stylesheet>

Then, you can retrieve those values in the XSL with a dollar-sign notation, like this:

  <xsl:template match="/inquiry">
    <soap:Envelope
        xmlns:mh="http://example.com/messageheader.xsd"
        xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
        xmlns:q0="http://example.com/soaprequest.xsd">

      <soap:Header>
        <mh:MessageHeader>
          <cdm:userName><xsl:value-of select="$uid"/></cdm:userName>
          <cdm:userPassword><xsl:value-of select="$pwd"/></cdm:userPassword>
        </mh:MessageHeader>
      </soap:Header>

      <soap:Body>
        <q0:InquireRequest>...</q0:InquireRequest>
      </soap:Body>
    </soap:Envelope>
  </xsl:template>

View solution in original post

4 REPLIES 4

Yes, here's an example. Suppose you have an XSL policy like this:

<XSL name="XSL-SubscriberNumbers-XformRequest">
  <DisplayName>XSL - SubscriberNumbers - Request</DisplayName>
  <FaultRules/>
  <Properties/>
  <OutputVariable>request.content</OutputVariable>
  <Parameters ignoreUnresolvedVariables="false">
    <Parameter name="uid" ref="authn.uid"/>
    <Parameter name="pwd" ref="authn.pwd"/>
  </Parameters>
  <ResourceURL>xsl://SubscriberNumbers-Request.xsl</ResourceURL>
  <Source>request</Source>
</XSL>

We can see from reading it, it applies to the request message, and transforms the content in place. (returning the output to request.content).

This policy specifies two parameters, uid and pwd, which will be passed to the XSL. The values for those parameters are obtained from context variables, in this case, they are named authn.uid and authn.pwd , respectively. Values for those context variables need to have been set prior to the evaluation of this XSL policy.

Now let's look at the actual XSL sheet. You would refer to the passed parameters by name at the top of the XSL like this:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:cdm="http://example.com/cheesodatamodel.xsd">


  <xsl:param name="uid" select="''"/>
  <xsl:param name="pwd" select="''"/>


  <xsl:output method="xml" omit-xml-declaration="yes" indent="no" media-type="string"/>


  <xsl:template ...>
  ...
</xsl:stylesheet>

Then, you can retrieve those values in the XSL with a dollar-sign notation, like this:

  <xsl:template match="/inquiry">
    <soap:Envelope
        xmlns:mh="http://example.com/messageheader.xsd"
        xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
        xmlns:q0="http://example.com/soaprequest.xsd">

      <soap:Header>
        <mh:MessageHeader>
          <cdm:userName><xsl:value-of select="$uid"/></cdm:userName>
          <cdm:userPassword><xsl:value-of select="$pwd"/></cdm:userPassword>
        </mh:MessageHeader>
      </soap:Header>

      <soap:Body>
        <q0:InquireRequest>...</q0:InquireRequest>
      </soap:Body>
    </soap:Envelope>
  </xsl:template>

Hi @Dino

I tried this for a variable which is containing XML itself.

I want to insert this XML into another XML.

I tried as below,

1)Passed the variable as parameter into XSLT

OutTestXML contains <Name>Test1</Name>

 <Source>request</Source>
    <Parameters ignoreUnresolvedVariables="false">
        <Parameter name="TestXML" ref="OutTestXML"/>
    </Parameters>
    <ResourceURL>xsl://InsertXML.xsl</ResourceURL>
    <OutputVariable>XSLTOutput</OutputVariable>

2)In XSLT gave as below,

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml"/>
    <xsl:param name="TestXML" select="''"/>
    <xsl:template match="/">
        <TestDetails>
            <Id>1</Id>
            <xsl:copy-of  select="$TestXML"/>
        </TestDetails>
    </xsl:template>
</xsl:stylesheet>

3)I tried the same XSLT with even xsl:value-of

4)But in both 2nd and 3rd methods,its showing result with "ampersand lt" and "ampersand gt" replacing xml tags.

Result

(I have replaced the Ampersand symbol with word in the Result just to show how the result looks like ,because I was not able to paste the & as it was converting to < and > here.)

<?xml version="1.0" encoding="UTF-8"?><TestDetails><Id>1</Id>"ampersand lt"?xml version="1.0" encoding="UTF-8"?ampersand gt"ampersand lt"Name"ampersand gt"1"\ampersand lt"Name"ampersand gt"</TestDetails>

Please let me know how to avoid getting this "ampersand lt" and "ampersand gt" and how to get the correct XML tags <,> .

Also note that xml version is getting added in that XML variable in its previous step when I tried to do JSON to XML conversion.

So,I have to pick the <Name> node alone from the variable.Please let me know how to do this.

I have some good information for you @RK4! but I won't provide it for you here. Please ask a NEW question and I will answer there. Do not ask questions in comments attached to old answers!

2208-ask-a-question.png

Sure @Dino

I thought this is just an extension of the question.As you suggested,I have created a new question.

https://community.apigee.com/questions/20392/using-variables-containing-xml-in-xslt-policy.html