Thursday, November 14, 2019

12c SOA XSLT - Use of Template Rules or apply-templates

Template Rules or apply-templates: 
Template rules are xsl:template statements with match attributes. Template rules are supported by the XSLT Map Editor. <xsl:apply-template> tag signals the XSLT processor to find the appropriate template to apply, based on the type and context of each selected node.

<xsl:apply-templates> is a little different with named template and it is the real power of XSLT: It takes any number of XML nodes (whatever you define in the select attribute), iterates them (this is important: apply-templates works like a loop!) and finds matching templates for them:

A concept to understand with XSLT is that of the "current node". With <xsl:apply-templates> the current node moves on with every iteration, whereas <xsl:call-template> does not change the current node. I.e. the . within a called template refers to the same node as the . in the calling template. This is not the case with apply-templates.
There are some other aspects of templates that affect their behavior: Their mode and priority, the fact that templates can have both a name and a match. It also has an impact whether the template has been imported (<xsl:import>) or not. These are advanced uses and you can deal with them when you get there.

Use case:
Here i will concatenate the employee names with a space from a set of employess input xml using apply template.
Input XML:
<Employees>
 <Employee>
  <Id>1ws</Id>
  <Name>test1</Name>
 </Employee>
 <Employee>
  <Id>2ws</Id>
  <Name>test2</Name>
 </Employee>
</Employees>

Output expected: test1 test2

Implementation steps:

Create a SOA project



 Choose synchronous BPEL process
 Change the XSD as required.


 Choose the source and target payloads

 Add the apply templates
<xsl:template match="/">
    <ns0:processResponse>
      <ns0:result>
        <xsl:apply-templates select="/ns0:Employees/ns0:Employee/ns0:Name"/>
      </ns0:result>
    </ns0:processResponse>
  </xsl:template>
  <xsl:template match="/ns0:Employees/ns0:Employee/ns0:Name">
    <xsl:value-of select="concat(' ',.)"/>

  </xsl:template>

 Deploy and Test


No comments:

Post a Comment

Featured Post

11g to 12c OSB projects migration points

1. Export 11g OSB code and import in 12c Jdeveloper. Steps to import OSB project in Jdeveloper:   File⇾Import⇾Service Bus Resources⇾ Se...