Popular Posts

Monday, January 13, 2014

Dynamic Context Variable Set Action

I had a requirement where I need to create and store as many context variable depending on the number of elements received with incoming request message. For example, if incoming request messages is.....................

<in>
   <element>100</element>
   <element>200</element>
    <element>300</element>
    <element>400</element>
</in>
 
I needed to create & store context variables like this.. 
 
var://context/TEMP/element1
var://context/TEMP/element2
var://context/TEMP/element3
var://context/TEMP/element4
 
Each context variable contains <element> node. So if incoming number of element nodes are 10 in the request, total 10 context variable needs to be created.
This is the XSL that did work for me.. 
 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dp="http://www.datapower.com/extensions" xmlns:dpconfig="http://www.datapower.com/param/config" xmlns:str="http://exslt.org/strings" xmlns:dpfunc="http://www.datapower.com/extensions/functions" xmlns:regexp="http://exslt.org/regular-expressions" extension-element-prefixes="dp" exclude-result-prefixes="dp dpconfig str dpfunc regexp">
<!--
Requirement is to Create & Set Context Variables Dynamically
 
var://context/TEMP/element1
var://context/TEMP/element2
var://context/TEMP/element3
...
..
var://context/TEMP/elementN
('N' decided on the basis of number of incomig elements received (input/element))
-->
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
  <xsl:template match="/">
     <xsl:for-each select="input/element">
     <!-- Capture position of each "<element>" tag --> 
       <xsl:variable name="i" select="position()" />
       <dp:set-variable name="concat('var://context/TEMP/element',$i)"  value="." /> 
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
 

No comments:

Post a Comment