Make your own free website on Tripod.com
Enterprise Computing - XML





  Home
  Servlet
  JDBC
  EJB
  XML
  JSP
  RMI
  JavaIDL
  Protocols
  UML

  Books

  Contact

Introduction
Extensible Markup Language (XML) like HTML uses tags. In HTML, where the tag tells the browser how to represent the data, but in XML the tags are just delimiters and XML tags are well formed. By this way XML represents only content. This leverage the use of data present in the XML, developer now can use this data to interpret and present the data completely specific to the application requirement. XML documents are used to exchange data between dissimilar environment. XML is platform independent and well supported. To learn more about XML click here.

As I said earlier, XML is the best choice to exchange data between dissimilar environment it is important to know how to transform XML. These transformation may involve translating to other formats (HTML, WML, comma delimited text file...) or translating the XML vocabularies. Here we can see various transforming techniques.

XSL Transformations (XSLT) is a part of Extensible Stylesheet Language (XSL). XSL in addition to XSLT includes vocabulary for specifying formatting. XSLT is written in XML. XSLT processor transforms the XML document based on the XSL stylesheet into requested format. This is achieved by building the document tree and manipulating the tree structure to get the desired format. By this way the elements in the original XML document can be interchanged or translated. The example below uses the Xalan-J(XSLT Processor). You can get information about Xalan-J from here.

Example
Scenario:
A search request generates the following XML,
<?xml version="1.0" ?>
<linkmanager>
 <linkset name="Search results">
   <link ref="search1.com">
     <desc>The first one</desc>
   </link>
   <link ref="search2.com">
     <desc>The second one</desc>
   </link>
</linkset>
<linkset name="Related links">
   <link ref="relate1.com" status="best">
   </link>
   <link ref="relate2.com" status="better">
   </link>
   <link ref="relate3.com" status="good">
   </link>
</linkset>
</linkmanager>
If there is no description(desc), use the link reference(ref) as the description. If there is a status(more related to the search criteria) about the link distinguish them using the font color(red for more related, orange for medium related and green for average related).
Basically the XML document need to translated as follows,
<html>
    <head>
        <title>Linkmanager Listing</title>
    </head>
    <body>
        <h3>Search results</h3>
        <ul>
            <li>
                <a href="http://search1.com">
		The first one</a>
            </li>
        </ul>
        <ul>
            <li>
                <a href="http://search2.com">
		The second one</a>
            </li>
        </ul>
        <h3>Related links</h3>
        <ul>
            <li>
                <a href="http://relate1.com">
                    <font color="red">relate1.com
			</font>
                </a>
            </li>
        </ul>
        <ul>
            <li>
                <a href="http://relate2.com">
                    <font color="orange">relate2.com
			</font>
                </a>
            </li>
        </ul>
        <ul>
            <li>
                <a href="http://relate3.com">
                    <font color="green">relate3.com
			</font>
                </a>
            </li>
        </ul>
    </body>
</html>
The following XSL stylesheet does the work.
<?xml version="1.0"?> 
  1. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL
  /Transform" version="1.0">
  2. <xsl:output method="html" indent="yes"/> 
  3. <xsl:strip-space elements="*"/> 

    <!--======================================--> 
    <!-- Root Element Template                --> 
    <!-- This template specifies what creates --> 
    <!-- the root element of the result tree. --> 
    <!-- In this case, it tells the XSL       --> 
    <!-- processor to start with the          --> 
    <!-- <linkmanager> element.               --> 
    <!--======================================--> 
   4. <xsl:template match="/"> 
      5. <xsl:apply-templates select="linkmanager"/> 
    </xsl:template> 

    <!--==================================================--> 
    <!-- <linkmanager> template                           --> 
    <!-- This template creates the <html> structure.      --> 
    <!-- Other templates fill in the rest of the document.--> 
    <!--==================================================--> 
    6. <xsl:template match="linkmanager"> 
      <html> 
        <head> 
          <title> 
            7. <xsl:text>Linkmanager Listing</xsl:text> 
           </title> 
        </head> 
        <body> 
          8. <xsl:apply-templates select="linkset"/> 
        </body> 
      </html> 
    </xsl:template> 
    <!--===============================================--> 
    <!-- <linkset> template                            --> 
    <!-- This template processes <linkset> tags.       --> 
    <!--===============================================--> 

    9. <xsl:template match="linkset"> 
      <h3> 
         10. <xsl:value-of select="./@name" /> 
      </h3> 
      11. <xsl:apply-templates select="link"/> 
    </xsl:template> 

    <!--======================================--> 
    <!-- <link> template                      --> 
    <!-- This template generates output for a --> 
    <!-- <link> element.                      --> 
    <!--======================================--> 
    12. <xsl:template match="link"> 
      <ul>
	<li>
	    13. <xsl:element name="a"> 
            
	    14. <xsl:attribute name="href"> 
            	15. <xsl:text>http://</xsl:text>
		16. <xsl:value-of select="./@ref"/> 
            </xsl:attribute> 
            
	    17. <xsl:choose>
	      18. <xsl:when test='./@status'>
	      	19. <xsl:element name="font"> 
              	20. <xsl:attribute name="color">
		  20. <xsl:choose>
		   21. <xsl:when test="./@status = 'best'">
			22. <xsl:text>red</xsl:text>
		   </xsl:when>
		   23. <xsl:when test="./@status = 'better'">
			24. <xsl:text>orange</xsl:text>
		   </xsl:when>
		   25. <xsl:when test="./@status = 'good'">
			26. <xsl:text>green</xsl:text>
		   </xsl:when>
		  </xsl:choose>	
              	</xsl:attribute>
              	27. <xsl:choose>
			28. <xsl:when test="not(desc)">
		  	  29. <xsl:value-of select="./@ref"/> 
			</xsl:when>
			30. <xsl:otherwise>
		  	  31. <xsl:value-of select="./desc"/> 
			</xsl:otherwise>
	      	</xsl:choose>
	        </xsl:element>
	      </xsl:when>
	      32. <xsl:otherwise>
	        33. <xsl:value-of select="./desc"/> 
	      </xsl:otherwise>	
	    </xsl:choose> 	
	    </xsl:element> 
	</li>
      </ul>	
    </xsl:template> 
  </xsl:stylesheet>
Click here, to view the generated HTML page
This transformation can be done at command level or write a servelt to generate dynamically. I will write on this ASAP.
more to come.....


Click on the image to order the book from Amazon.com

Proffesional XML - Mark Birbeck et. al.
cover
Search books!

Search Now:
In Association with Amazon.com