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″?>
        <Description><![CDATA[Chocolate Cake]]></Description>
        <Description><![CDATA[Blueberry Cake]]></Description>
        <Description><![CDATA[Raised Glazed]]></Description>
        <Description><![CDATA[Chocolate Buttermilk]]></Description>
        <Description><![CDATA[Chocolate Bar]]></Description>

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=”.”/>,
 <!– 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=”‘,'”/>

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:when test=”contains($list, $delimiter)”>
          <xsl:value-of select=”normalize-space($list)” />
          <xsl:value-of select=”concat( normalize-space($list), $delimiter )”/>
    <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:value-of select=”$first”/>
    <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”/>

Now call the template into table to display the checklistbox:

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


<TD colspan=2“>

<xsl:call-template name=XmlItemref>





The output is given below:

XSLT(Extensible Stylesheet Language Transformation)

XSLT (Extensible Stylesheet Language Transformation) is a language for transforming XML document into XML, HTML, XHTML, or plain text documents. XSLT serves us many ways:
1. It is a general-purpose translation tool.  It convert from one XML markup vocabulary to another. XSLT transform an XML document into HTML or XHTML so it will easily display in a web browser.
2. It can recognize document content
3. It can generate multiple results from the same content-like HTML, SVG and WAP
4. It uses XPath for navigating XML documents. XPath helps XSLT identify and find nodes in XML documents. Nodes are things like elements, attributes, and other objects in XML.
5. It help us to perform complex sorting and linking easily.
XSLT will create a new XML document based on the content of existing xml file without changing it. It is most often used to convert xml data into web pages/PDF, between different xml schemas.  It is developed by World Wide Web Consortium(W3C). The latest version is 2.0.

In XML document the order of element is meaningful but the order of attribute is not meaningful, comments are same as html comment, CDATA sections often used to ‘markup’ into an element. Like –

<Name><![CDATA[John Smith]]></Name>


Default Rules of XSLT Processing:

  • Document node: xsl:apply-templates
  • Element nodes: xsl:apply-templates
  • Text nodes: output text

From Architectural Point of View:

  • XML Parser – An XML Parser is the component that interprets the XML code. Codes cannot be understood without a parser. An XML parser provides vital information to the program on how to read the file. Parsers come in multiple formats and styles. They can be free-standing software, libraries, modules and classes. All browsers have built-in XML parsers.
  • XSLT Processor – The software responsible for transforming source trees into result trees using an XSLT stylesheet is referred to as the processor.
  • Serialize – serialize the xml document for view the final output

How Does The Parser Work:

Parsers can be a bit complex. The goal is to transform XML into a readable code. Computer hardware only knows one language. It is the software that turns all the different languages into a workable format. Software is smart, but computers by themselves are ignorant. When presented with a bunch of XML code, computer hardware has no clue what it means. Parsers convert that code into something the hardware will recognize.

How XSLT Processor Work:

  1. Loads the input document as a DOM tree (internally the processor optimizes DOM)
  2. Performs a depth-first walk of the input tree
  3. As it walks through the document, selects a template in the stylesheet for the current node
  4. Applies the template, which describes how to create zero, one, or more nodes in the output tree
  5. When the walk is completed, creates a new tree (the output tree) from the input tree and the rules in the templates
  6. Writes the output tree according to the HTML or XML syntax

How Does Serialize The Result:

After your application has evaluated an XPath or XQuery expression or performed a transformation with an XSLT stylesheet, you might want to write the output as an actual XML document represented as a file or as a Java™ string. The process of rendering results as an XML document is known as serialization.


  • Serialize an XSequenceCursor.

    Your application can call the XSequenceCursor.exportSequence method to serialize a sequence that is represented by an instance of the XSequenceCursor interface. The arguments on this method are an instance of the javax.xml.transform.Result interface and optionally an instance of the XOutputParameters interface.

    If the instance of the Result interface is also an instance of the class, the sequence is serialized as described in the XSLT 2.0 and XQuery 1.0 Serialization Recommendation. The StreamResult object can contain an instance of the class or the class, where the processor will write the serialized sequence.

    You can create an instance of the XOutputParameters interface by calling XFactory.newOutputParameters() and call the methods on that object to override the default serialization parameter settings.

    XFactory factory = XFactory.newInstance();
    XPathExecutable expr = factory.prepareXPath("/purchaseOrder/item[@price > 1000]");
    XSequenceCursor exprResult = expr.execute(new StreamSource(inputFile));
    System.out.println("Items purchased costing more than $1000");
    if (exprResult != null) {
        // Set indenting in order to pretty-print result
        XOutputParameters params = factory.newOutputParameters();
        exprResult.exportSequence(new StreamResult(System.out), params);
    } else {
        System.out.println("None found");

    You can also call one of the getOutputParameters() methods on an instance of the XSLTExecutable interface to get the serialization parameters that are associated with a particular output definitionin an XSLT stylesheet. Use the XSLTExecutable.getOutputParameters(javax.xml.namespace.QName) method to get the serialization parameters for a named output definition or the no-argument XSLTExecutable.getOutputParameters() method to get those of the unnamed output definition. You might want do this to perform some post-processing on the result of the transformation using the instance of the XSequenceCursor interface that the transformation produces before serializing the result. If you change the settings of the serialization parameters in the instance of the XOutputParameters interface returned by one of the XSLTExecutable.getOutputParameters() methods, it will not affect the output definition in the stylesheet.

    XFactory factory = XFactory.newInstance();
    XSLTExecutable style = factory.prepareXSLT(new StreamSource("style.xsl"));
    XSequenceCursor xformResult = style.execute(new StreamSource("purchase.xml");
    XOutputParameters params = style.getOutputParameters(new QName("my-output-definition"));
    xformResult.exportSequence(new StreamResult("output.html"), params);

    Note that according to the XSLT 2.0 and XQuery 1.0 Serialization Recommendation, a serialization error results if the sequence that is to be serialized contains attribute nodes or namespace nodes. If the sequence that you need to serialize might contain attribute or namespace nodes, get the values of those nodes as strings or some other appropriate type and serialize those values instead.

  • Serialize a single item.

    You can also serialize just the current item in an instance of the XSequenceCursor interface by using one of the exportItem methods. The exportItem methods are inherited from the XItemView interface, so they can be called on an instance of that interface as well.

    As with the exportSequence method described above, the arguments of the exportItem method are an instance of the javax.xml.transform.Result interface and optionally an instance of the XOutputParameters interface. The effect of calling exportItem is identical to the effect of calling exportSequence with a sequence that consists of just the current item.

  • Serialize the result of a transformation or query directly.

    Your application can also serialize the result of an XSLT transformation or XQuery expression directly by supplying an instance of the javax.xml.transform.Result interface on the XSLTExecutable.execute method or XQueryExecutable.execute method. The serialization parameter settings are determined by the attributes of any applicable xsl:output declaration or xsl:result-document instruction in the case of an XSLT stylesheet, and are always the default values in the case of the result of an XQuery expression.

    XFactory factory = XFactory.newInstance();
    XSLTExecutable style = factory.prepareXSLT(new StreamSource("style.xsl"));
    style.execute(new StreamSource("purchase.xml"),
                  new StreamResult("output.xml"));

    If your application supplies an instance of the XResultResolver interface on a transformation, your application can direct each final result tree to a different destination.

  • Use identity transformation.

    You can use the XML API to transform XML data contained in an instance of javax.xml.transform.Source directly to an instance of a javax.xml.transform.Result. This is often referred to as an identity transformation. See Performing basic XSLT operations for an example.

You can also serialize your objects into XML and then apply to the transform XML Object Serializer to XSLT Transformer .


1. XSL Transformations (XSLT) Version 2.0
2. Serializing the results
3. How an XSLT processor works
4. CSCI E-153, Web Development Using XML