Saturday 7 May 2011

Using key for apply templates in xsl

We can use key() function for apply-templates element also.
 Below is one example :

<xsl:param name="country_name"/>
                <xsl:key match="/ns1:POC_KeyProcessRequest/ns1:SupplierDetails/ns1:Details" name="Supplier_name_byCountry" use="ns1:Country"/>
  <xsl:template match="/">
  <ns1:POC_KeyProcessResponse>
      <ns1:Details>
      <xsl:apply-templates select="key('Supplier_name_byCountry',$country_name)"/>
      </ns1:Details>
      </ns1:POC_KeyProcessResponse>
  </xsl:template>
  <xsl:template match="/ns1:POC_KeyProcessRequest/ns1:SupplierDetails/ns1:Details">
  <ns1:name>
  <xsl:value-of select="./ns1:SupplierName"/>
  </ns1:name>
  </xsl:template>


This the source xml :

<POC_KeyProcessRequest xmlns="http://xmlns.oracle.com/POC_Key">
   <SupplierDetails>
      <Details>
         <SupplierName>Debdeep</SupplierName>
         <Country>india</Country>
         <Product>amway</Product>
      </Details>
      <Details>
         <SupplierName>mousumi</SupplierName>
         <Country>usa</Country>
         <Product>lakme</Product>
      </Details>
      <Details>
         <SupplierName>kajol</SupplierName>
         <Country>india</Country>
         <Product>bullet</Product>
      </Details>
   </SupplierDetails>
</POC_KeyProcessRequest>


And this is how the output looks like when param value for country_name was passed as india:

<ns1:POC_KeyProcessResponse xmlns:ns1="http://xmlns.oracle.com/POC_Key">
   <ns1:Details>
      <ns1:name>Debdeep</ns1:name>
      <ns1:name>kajol</ns1:name>
   </ns1:Details>
</ns1:POC_KeyProcessResponse>