How does the XSL transformation policy work?

I am transforming a XML file using XSL policy. I am unable to understand how the policy is working to transform the input XML file. 
Here is input XML file:

 

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Body>
    <anb:RefundStatusInqRs
        xmlns:anb="http://foo.bar">
      <anb:MsgRsHdr>
        <anb:StatusCode>122</anb:StatusCode>
        <anb:StatusDesc>Success</anb:StatusDesc>
        <anb:HostStatus>
          <anb:HostName>123</anb:HostName>
          <anb:HostStatusCode>00000</anb:HostStatusCode>
        </anb:HostStatus>
        <anb:RqUID>123</anb:RqUID>
        <anb:AgentId>ApigeeAPI</anb:AgentId>
        <anb:ClientDt>2009-04-14T00:00:00</anb:ClientDt>
      </anb:MsgRsHdr>
      <anb:Body>
        <anb:RefundId>123</anb:RefundId>
        <anb:TimeStamp>2021-12-21T12:12:32</anb:TimeStamp>
        <anb:RefundStatus>4</anb:RefundStatus>
      </anb:Body>
    </anb:RefundStatusInqRs>
  </soapenv:Body>
</soapenv:Envelope>

 


XSL File:

 

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="*">
    <xsl:element name="{local-name(.)}">
      <xsl:apply-templates select="@* | node()"/>
    </xsl:element>  
  </xsl:template>
  <xsl:template match="@*">
    <xsl:attribute name="{local-name(.)}">
      <xsl:value-of select="."/>
    </xsl:attribute>  
  </xsl:template>
</xsl:stylesheet>

 

Output XML:

 

<Envelope>
  <Body>
    <RefundStatusInqRs>
      <MsgRsHdr>
        <StatusCode>111</StatusCode>
        <StatusDesc>Success</StatusDesc>
        <HostStatus>
          <HostName>111</HostName>
          <HostStatusCode>00000</HostStatusCode>
        </HostStatus>
        <RqUID>111</RqUID>
        <AgentId>111</AgentId>
        <ClientDt>2009-04-14T00:00:00</ClientDt>
      </MsgRsHdr>
      <Body>
        <RefundId>111</RefundId>
        <TimeStamp>2021-12-21T12:12:32</TimeStamp>
        <RefundStatus>4</RefundStatus>
      </Body>
    </RefundStatusInqRs>
  </Body>
</Envelope>

 



I need to know the how to remove or delete fields from the XML

 

Solved Solved
0 1 233
1 ACCEPTED SOLUTION

The XSL Policy just applies an XSL transformation. XSL of course is a standard language and not an Apigee-specific technology. So if you have questions about how XSL works, then maybe you want to look for an XSL Tutorial, elsewhere.

To answer the question about THAT specific XSL that you cited, it transforms the inbound document by removing namespaces. You'll see that the sheet uses local-name()... that's just a way to refer to the element name without the namespace. So that's what that sheet does. The output document will have the same structure, but with no namespaces. Understanding that requires that you understand XSL in some detail.

If you plan to use XSL, my suggestion is to get an XSL tool that allows you to test out XSL transforms, outside of the Apigee environment. You need to be able to modify your XSL and XML documents, then test and try the transform, quickly and interactively. Visual Studio has a good tool. You can get command line tools too, to work from a terminal. Online XSL tools help too, but In my experience having a local development environment is really important. Develop and build your XSL sheet in that local environment, until you're satisfied with how it works. Then bring that XSL sheet into the Apigee , and specify it in the XSL policy, and it will work just the same way it works on your development workstation.

Removing elements from the XML is possible via XSL, but of course you need to learn the XSL language, which can be an obstacle for some people.

If you want to do something pretty simple, like removing a single element from a document, then you could use the custom Edit XML Node policy. Here's an example in the README that shows you how to remove a single element.

There is one other way that I know of to "modify XML" within an Apigee API proxy: using the E4X extensions in a JavaScript policy. This q&a provides some examples for that.

So the summary is, in an Apigee API Proxy, you have 3 primary options for modifying an XML document. There are tradeoffs for each.

Option Reference resource Notes
XSL link to policy documentation This brings the full capability of XSL 1.0 or XSL 2.0.
Pros: powerful, can transform very flexibly.
Cons: must learn XSL. This can be an obstacle for some people or some teams.
Edit-XML-Node github repo This is a Java callout.
Pros: simple to use.
Cons: Limited in function. Can modify just one node at a time.
E4X

community posts:

E4X is a mechanism for manipulating XML from within JavaScript (Rhino engine).
Pros: accessible, can do the job from within JavaScript.
Cons: a somewhat obscure API for Javascript. Supported in Apigee, but not widely supported elsewhere.

View solution in original post

1 REPLY 1

The XSL Policy just applies an XSL transformation. XSL of course is a standard language and not an Apigee-specific technology. So if you have questions about how XSL works, then maybe you want to look for an XSL Tutorial, elsewhere.

To answer the question about THAT specific XSL that you cited, it transforms the inbound document by removing namespaces. You'll see that the sheet uses local-name()... that's just a way to refer to the element name without the namespace. So that's what that sheet does. The output document will have the same structure, but with no namespaces. Understanding that requires that you understand XSL in some detail.

If you plan to use XSL, my suggestion is to get an XSL tool that allows you to test out XSL transforms, outside of the Apigee environment. You need to be able to modify your XSL and XML documents, then test and try the transform, quickly and interactively. Visual Studio has a good tool. You can get command line tools too, to work from a terminal. Online XSL tools help too, but In my experience having a local development environment is really important. Develop and build your XSL sheet in that local environment, until you're satisfied with how it works. Then bring that XSL sheet into the Apigee , and specify it in the XSL policy, and it will work just the same way it works on your development workstation.

Removing elements from the XML is possible via XSL, but of course you need to learn the XSL language, which can be an obstacle for some people.

If you want to do something pretty simple, like removing a single element from a document, then you could use the custom Edit XML Node policy. Here's an example in the README that shows you how to remove a single element.

There is one other way that I know of to "modify XML" within an Apigee API proxy: using the E4X extensions in a JavaScript policy. This q&a provides some examples for that.

So the summary is, in an Apigee API Proxy, you have 3 primary options for modifying an XML document. There are tradeoffs for each.

Option Reference resource Notes
XSL link to policy documentation This brings the full capability of XSL 1.0 or XSL 2.0.
Pros: powerful, can transform very flexibly.
Cons: must learn XSL. This can be an obstacle for some people or some teams.
Edit-XML-Node github repo This is a Java callout.
Pros: simple to use.
Cons: Limited in function. Can modify just one node at a time.
E4X

community posts:

E4X is a mechanism for manipulating XML from within JavaScript (Rhino engine).
Pros: accessible, can do the job from within JavaScript.
Cons: a somewhat obscure API for Javascript. Supported in Apigee, but not widely supported elsewhere.