Layer7 API Management

Expand all | Collapse all

Use "Apply XSL Transformation" and the value of the complete XSL obtained from a context variable

  • 1.  Use "Apply XSL Transformation" and the value of the complete XSL obtained from a context variable

    Posted 09-13-2016 06:53 AM

    Hi.

     

    I'm using CA API Gateway 9.0.

     

    We need to use "Apply XSL Transformation" to apply a transformation with XSLT and XML, and get the resulting string.
    The problem we have is that our XSLT is dynamic, so we wanted to use a context variable that will store the full value of XSLT, the context variable would like to use in "Apply XSL Transformation" (in "Configured in advance" -> "XSLT Stylesheet").
    However, this policy does not allow the use of context variable full mode indicating the error: "Unable to save: The XSL is missing or invalid. Content is not allowed in prolog.". If only the first label set of XSLT and then the rest of XSLT with a context variable, does not return error, but it's like not obtain the value of the context variable.
    How could perform the transformation using the XSLT full value that is stored in a variable context?

     

    I've got a possible solution to my problem (although it may not be very optimal).

     

    To understand the solution, I present my first goal:
    I will create an API Rest in CA Gateway API. This API will make a request (SOAP) to a web service and return an XML where one of his label has the value of a XSLT and another label is the string of XML data to perform the transformation with the XSLT . The idea is to parse and get both XSLT and XML in context variables and use it with "Apply XSL Transformation". But the problem is I can not use the context variable with the string of XSLT within "Apply XSL Transformation".

     

    I think the solution is:
    Store the XSLT cache with "Store to Cache". Create a new API whose objective is to obtain the value of cache XSLT and return it in the response. Then "Apply XSL Transformation" put the url of the new API, where Stylesheet Location is set to "Monitor URL for lastest value". (In the url I pass a parameter to indicate the value of "Cache entry key" and the new API would get it).

     

    Although not the most optimal, this is the only solution I could implement and, in principle, for my case work.
    Do you know a more optimal solution or solution to use a context variable with the value of the XSLT in the transformation policy?

     

    Greetings.



  • 2.  Re: Use "Apply XSL Transformation" and the value of the complete XSL obtained from a context variable

    Posted 09-15-2016 04:52 AM

    Have you see that ? Can a variable set in "Set Context Variable" assertion be used in the "Apply XSL Transformation" assertion? 

    I had a similar problem. I wanted to use context variables in XSL Assertion, especially in the < xsl:template match = "$myVar"> .  The statement <xsl:param name="myVar" select=""/> allows the use of context variables but not in this attribute "match" ! Below is the response I got from the support :

    The sample which I have which uses a context variable is

    <?xml version="1.0" encoding="UTF-8"?>

    <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

                                 xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

       <xsl:param name="authenticatedUser.foo" select="'foo attribute not available'"/>

       <!-- add the element at the end of the headers -->

       <xsl:template match="//soap:Envelope/soap:Header/*[last()]">

           <xsl:copy>

                    <xsl:apply-templates select="node()|@*" />

                    </xsl:copy>

           <xsl:call-template name="tokenize-1">

               <xsl:with-param name="pat" select="$authenticatedUser.foo"/>

           </xsl:call-template>

       </xsl:template>

       <xsl:template match="node()|@*">

                    <xsl:copy>

                    <xsl:apply-templates select="node()|@*" />

                    </xsl:copy>

       </xsl:template>

       <xsl:template name="tokenize-1">

           <xsl:param name="pat"/>

           <xsl:choose>

               <xsl:when test="contains($pat,',')">

                 <xsl:call-template name="process-token">

                   <xsl:with-param name="token" select="substring-before($pat,',')"/>

                 </xsl:call-template>

                 <xsl:call-template name="tokenize-1">

                   <xsl:with-param name="pat" select="substring-after($pat,',')"/>

                 </xsl:call-template>

               </xsl:when>

                <xsl:otherwise>

                 <xsl:call-template name="process-token">

                   <xsl:with-param name="token" select="$pat"/>

                 </xsl:call-template>

               </xsl:otherwise>

           </xsl:choose>

       </xsl:template>

       <xsl:template name="process-token">

         <xsl:param name="token"/>

         <foo><xsl:value-of select="$token"/></foo>

       </xsl:template>

    </xsl:transform>

    I did get your xsl reviewed by my team. It seems you cannot use a context variable in the match parameter it will need to be a static entry.


    Therefore, to apply the transformation that I wanted in my XML Response (add to my children Elements XML response), I used a XPath and XML Add Element .  I hope it will help you.
    David


  • 3.  Re: Use "Apply XSL Transformation" and the value of the complete XSL obtained from a context variable

    Posted 09-16-2016 04:37 AM

    Good Morning.

     

    I saw it, but it isn't the same problem.

    My main problem is to use the context variable with full text of the XSLT and it can be used inside the policy (only a context variable, without other text or xslt. Only the context variable with de full text with the XSLT). This seems not to be possible.

     

    Thanks for answer

    Regards.



  • 4.  Re: Use "Apply XSL Transformation" and the value of the complete XSL obtained from a context variable

    Posted 09-16-2016 11:45 AM

    Good morning,

     

    The two issues raised above are expected behavior with the current makeup of the product. Context variables can not be used to represent the entire XSLT in the assertion or in the match component. The context variable can be used in element such as:

    <xsl:value-of select="$reqmsgid.result"/>
    <xsl:with-param name="pat" select="$authenticatedUser.foo"/>
    <xsl:if test="./store != $store.id">

    Sincerely,

     

    Stephen Hughes

    Director, CA Support



  • 5.  Re: Use "Apply XSL Transformation" and the value of the complete XSL obtained from a context variable

    Posted 09-19-2016 05:08 AM

    Thanks Stephen

    It seems to me that this information must be propagate widely for community and developper of policy in API Gateway .

     

    Please, can you add this to CA Doc API Gateway 9.0/9.1  ?

    Apply XSL Transformation Assertion - CA API Gateway - 9.0 - CA Technologies Documentation 

    Apply XSL Transformation Assertion - CA API Gateway - 9.1 - CA Technologies Documentation 

     

    Best regards

    David



  • 6.  Re: Use "Apply XSL Transformation" and the value of the complete XSL obtained from a context variable

    Posted 09-19-2016 12:51 PM

    David,

     

    I've reached out to our documentation team to review this community post and make the relevant changes to our online documentation.

     

    Sincerely,

     

    Stephen Hughes

    Director, CA Support



  • 7.  Re: Use "Apply XSL Transformation" and the value of the complete XSL obtained from a context variable

    Posted 09-20-2016 07:17 AM

    Thanks.

    Now I am clear that the context variable can not use the full value of the XSLT.

    Additionally, I would like to give me your opinion of alternative solution I propose in my first post.

     

    Regards.



  • 8.  Re: Use "Apply XSL Transformation" and the value of the complete XSL obtained from a context variable

    Posted 09-21-2016 12:28 AM

    Javier,

     

    The way that we have seen embedded XSL references looks like <?xml-stylesheet type="text/xsl" href="http://localhost:8080/test.xsl"?> in the top element and then in the XSLT assertion you would use the option "fetch from URL in processing instruction". When the XML is being processed through the assertion it would fetch and apply the XSL to the message. The idea you have is interesting in that you can cache the XSL for a period of time and call from it from the main service. In version 8.4 and higher we introduced in the gateway the ability to schedule tasks (Manage Scheduled Tasks - CA API Gateway - 8.4 - CA Technologies Documentation ) that could have a policy that is trigger against all nodes in the cluster at a set interval to update the XSL file in cache. From there you would create the service that would have the endpoint for the XSL retrieving the value from cache and in the main service inject the stylesheet element into the message prior to the XSLT assertion for processing.

     

    Sincerely,

     

    Stephen Hughes

    Director, CA Support



  • 9.  Re: Use "Apply XSL Transformation" and the value of the complete XSL obtained from a context variable

    Posted 09-21-2016 04:20 AM

    Thanks for your answer.

    For my case, it isn't possible to use scheduled tasks because the value of XSL is obtained at the request to other web services from the input parameters of my service.

    Anyway, it was a good idea.

     

    I think that I will use my propose solution since you've seen interesting.

     

    Thanks.

    Regards.