Displaying Data Using XSLT

Discussion in 'JavaScript and AJAX' started by MinalS, Sep 23, 2014.

  1. MinalS

    MinalS New Member

    Jul 8, 2014
    Likes Received:
    Trophy Points:
    There are two elements that are used to specify conditions for formatting:
    1. The if element
    2. The choose element
    1. The if element

    In XSLT, the if element provides a if – then construct. The syntax of if element is as shown below:
    <xsl: if test = “condition” >
     [ actions to be performed if the condition is true ]
    </xsl: if>
    In the above syntax, the test attribute specifies the criteria for performing the action. The
    condition specifies the Boolean expression that evaluates as true or false.

    Consider an example to display the name of employee whose ID is greater than 200.
    < xsl: if test = “ID” [ .gt; 200 ] “ >
    	<xsl: value – of select=”EMPNAME” />
    </xsl: if>
    2. The choose element

    The choose element helps the user to choose from many possible actions. Multiple conditions can be tested through the choose element. The element contains one or more when elements. The otherwise element is placed after the when element. If there are more choices, the choose element behaves as if – then – else construct.

    The syntax for the choose element is as shown below:

    <xsl: choose>
    	<xsl: when test=”condition” >
    	[ action to be taken ]
    	</xsl: when>
    	<xsl: otherwise>
    	[ action to be taken ]
    	</xsl: otherwise>
    </xsl: choose>

    XPath in XML

    The data in XML is contained in tags. Sometimes the text needs to be searched before it is formatted and send to the style sheet. The XPath concept is used for searching and retrieving data from an XML file. It considers the XML document as tree of interrelated branches and nodes.

    Consider the XML document as shown below:

    <? xml version =”1.0” ?>
    The following figure shows the tree structure of the XML document.


    The above diagram shows the nodes in an XML document. The DEPARTMENTINFO, DEPARTMENT, MANGAER and NOOFEMPLOYEES are the element nodes and the DEPTID and TYPE are attribute nodes. The values represent the text nodes.

    There are expressions and functions used for matching nodes in an XML document. XPath expressions cdn be used to retrieve the data based on the specific conditions. User can apply constraints through the filter clause known as filter pattern. The pattern is used to evaluate the XML document and extract the node set matching the pattern.

    A list of operators and special characters used to create XPath expressions.

    1. / : It selects the immediate child elements of the element. The example is as mentioned below:


    2. // : It searches the element at any node level. The example is as mentioned below:

    // ORDERNO

    3. .. : It selects the element which exists within the parent of the current element.

    .. ORDERNO

    4. * : selects all the elements.

    5. @ : It uses the prefix for the attribute.

    @ STUDID

    6. : - It separates the namespace prefix from the element or attribute name.

    7. ( ) : It is used to group the operations.

    8. [ ] : It applies the filter to the expression.

    XPath Functions

    The XPath functions are divided into following categories.
    1. string functions
    2. node – set functions
    3. Boolean functions
    4. numeric functions
    1. string functions

    The XPath string functions are used to perform string operations. The functions can be finding length, converting string.

    The following list demonstrates the XPath string functions.
    • string ( obj ? ): It is used to convert the argument into a string value. The example, string ( “D001”) returns D001 as a string value.
    • starts – with ( str, str ): It accepts two arguments. It returns true if the first argument starts with the second, else returns false. The example, starts – with (‘ India’ , ‘In’ ) returns true.
    • contains ( str, str): It accepts two strings as arguments and returns true if the first argument contains the second, else false. The example, contains (‘Funny’, ‘nn’ ) returns true.
    • string – length( str? ): It accepts one argument and returns the number of characters. The example, string – length ( “DAIL” ) returns 4.
    2. node set function

    The node set function is used to manipulate the node sets or information about the node.

    The following list demonstrates some of the node set functions.
    • last () – It returns the number of the last node in the node set. It returns the value 6, if the sixth node is the last node in the current node set.
    • position() – It returns the index number of the current node within the parent node.
    • count ( ns) – It returns the number of occurrences of the node in the XML document.
    • id ( obj ) – It returns the element with the specified unique ID.
    3. Boolean function

    The Boolean function returns either true or false.

    The following list shows some of the Boolean functions.
    • Boolean ( obj ? ) – It converts the argument to a Boolean value. If the parameter obj is a node set, it returns true.
    • not ( Boolean ) – It returns true if the argument passed is false.
    4. numeric function

    XPath provides numeric functions for adding, finding and converting string into numbers.

    The following list shows some of the numeric functions.
    • number ( obj? ) – It converts the argument into a number and returns the result.
    • sum (ns ) – It returns the sum of al the nodes in the node set that is used as an argument.
    • floor ( num ) – It returns the largest integer that is less than or equal to the argument.
    • ceiling ( num ) – It returns the smallest integer that is greater than or equal to the argument.
    • round ( num ) – It rounds the number to the nearest integer.


    An example to demonstrate the XPath concept is as shown below:

    Consider the book.xml file. The code in the file is as shown below:

    <? xml version=”1.0” encoding=”utf-8” ?>
    	<book category=”Cooking” >
    	<title>Veg Special</title>
    	<author>Tarla Dalal </author>
    	<book category=”Novel” >
    	<title>Famous Five</title>
    	<author>John </author>
    <book category=”Coding” >
    	<author>Thomas </author>
    The HTML page contains the following code

    <script language=”javascript”>
    function loadXMLDoc (dname)
    	if ( window.XMLHttpRequest)
    		xhttp=new XMLHttpRequest ();
    		xhttp=new ActiveXObject ( “Microsoft.XMLHTTP”);
    		xhttp.open(“GET”, dname,false);
    		catch (err)
    		return xhttp;
    	var x=loadXMLDoc (“books.xml”);
    	var xml=x.responseXML;
    	path=”/bookstore/book/price[ text () ]”;
    	if( window.ActiveObject || xhttp.responseType == “msxml-document” )
    		xml.setProperty(“SelectionLanguage”, XPath);
    		for ( int i=0; i<nodes.length; i++)
    The output for the code is as shown below:

    Last edited by a moderator: Jan 21, 2017

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice