Create dynamic checklistbox using xsl

When we use database we design Item table like following:

ID Name Description Price Per Unit Batter Topping Quantity
0001 Cake Chocolate Cake 0.55 Regular Maple 100
0002 Cake Blueberry Cake 0.55 Blueberry Maple 200
0003 Raised Raised Glazed 0.55 Regular Glazed 150
0004 Buttermilk Chocolate Buttermilk 1.55 Chocolate   180
0005 Bar Chocolate Bar 0.75 Regular Maple 100

The XML file structure looks like this:

<?xml version=”1.0″ encoding=”utf-8″?>
   <Item>
        <ID><![CDATA[0001]]></ID>
        <Name>Cake</Name>
        <Description><![CDATA[Chocolate Cake]]></Description>
        <PricePerUnit>0.55</PricePerUnit>
        <Batter>Regular</Batter>
        <Topping><![CDATA[Maple]]></Topping>
        <Quantity>100</Quantity>
   </Item>
   <Item>
        <ID><![CDATA[0002]]></ID>
        <Name>Cake</Name>
        <Description><![CDATA[Blueberry Cake]]></Description>
        <PricePerUnit>0.55</PricePerUnit>
        <Batter>Blueberry</Batter>
        <Topping><![CDATA[Maple]]></Topping>
        <Quantity>200</Quantity>
   </Item>
   <Item>
        <ID><![CDATA[0003]]></ID>
        <Name>Raised</Name>
        <Description><![CDATA[Raised Glazed]]></Description>
        <PricePerUnit>0.55</PricePerUnit>
        <Batter>Regular</Batter>
        <Topping><![CDATA[Glazed]]></Topping>
        <Quantity>150</Quantity>
   </Item>
   <Item>
        <ID><![CDATA[0004]]></ID>
        <Name>Buttermilk</Name>
        <Description><![CDATA[Chocolate Buttermilk]]></Description>
        <PricePerUnit>1.55</PricePerUnit>
        <Batter>Chocolate</Batter>
        <Topping><![CDATA[]]></Topping>
        <Quantity>180</Quantity>
   </Item>
   <Item>
        <ID><![CDATA[0005]]></ID>
        <Name>Bar</Name>
        <Description><![CDATA[Chocolate Bar]]></Description>
        <PricePerUnit>0.75</PricePerUnit>
        <Batter>Regular</Batter>
        <Topping><![CDATA[Maple]]></Topping>
        <Quantity>100</Quantity>
   </Item>

In XSL file we have a CkecklistBox. We need to fetch all Item name from the Items.xml file. The default file for OptDialog.xsl file is OptDialog.xml file. We have to add two templates to display checklistbox with item names.

<xsl:key name=”XmlItem” match=”Identification” use=”Name”/>
  <xsl:variable name=”itemnames” select=”document(‘Items.xml’)” />
 <!– collect all catalog names for checklistbox –>
  <xsl:template match=”itemnames/Item/Name” name=”XmlItemref”>
    <xsl:variable name=”ItemName”>
 <!– fetch all item name and store into ItemName variable–>
    <xsl:variable name=”elemName” select=”$itemnames/Item/Name”/>
    <xsl:for-each select=”$elemName”>
      <xsl:value-of select=”.”/>,
    </xsl:for-each>
    </xsl:variable>
 <!– add checklistbox for display item name–>
    <div style=”background-color:white;height:200px;width:100px;font-family:Verdana; font-size:8pt;float:left;overflow-y:scroll;overflow-x:visible;position:relative;” name=”lstItemNames” id=”lstItemNames” OnClick=”JavaScript:getItemName();”>
      <xsl:attribute name=”scope”><![CDATA[report]]></xsl:attribute>
      <xsl:attribute name=”xpath”><![CDATA[//Item/Name]]></xsl:attribute>
  <!– call item-tokenize template to split items–>
      <xsl:call-template name=”item-tokenize”>
        <xsl:with-param name=”list” select=”$ItemName” />
        <xsl:with-param name=”delimiter” select=”‘,'”/>
      </xsl:call-template>
    </div>
  </xsl:template>

The item-tokenize tamplate:

<!– split string tokenizer Template –>
  <xsl:template name=”item-tokenize”>
    <xsl:param name=”list” />
    <xsl:param name=”delimiter” />
    <xsl:variable name=”newlist”>
      <xsl:choose>
        <xsl:when test=”contains($list, $delimiter)”>
          <xsl:value-of select=”normalize-space($list)” />
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select=”concat( normalize-space($list), $delimiter )”/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable name=”first” select=”substring-before($newlist, $delimiter)” />
    <xsl:variable name=”remaining” select=”substring-after($newlist, $delimiter)” />
    <xsl:element name=”newitem”>
      <INPUT NAME=”chkOptions” TYPE=”CHECKBOX”>
        <xsl:attribute name=’value’>
          <xsl:value-of select=”$first”/>
        </xsl:attribute>
      </INPUT>
      <xsl:value-of select=”$first”/>
    </xsl:element>
    <xsl:if test=”$remaining”>
      <xsl:call-template name=”item-tokenize”>
        <xsl:with-param name=”list” select=”$remaining” />
        <xsl:with-param name=”delimiter”>
          <xsl:value-of select=”$delimiter”/>
        </xsl:with-param>
      </xsl:call-template>
    </xsl:if>
  </xsl:template>

Now call the template into table to display the checklistbox:

<TABLE width=100%border=0cellpadding=5style=font-family:Verdana; font-size:8pt>

<TR>

<TD colspan=2“>

<xsl:call-template name=XmlItemref>

</xsl:call-template>

</TD>

</TR>

</TABLE>

The output is given below:

Advertisements