Navigation Bar

Saturday, August 21, 2021

XML to HTML with XSL stylesheet

XSL is a stylesheet language for manipulating xml documents. 
Xml documents have structure but no format. 
So it becomes imperative to transform complex xml documents into more readable output formats like html. 
This can be done very effectively using XSL which can map the elements to other formatting languages.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html> 
<body>
  <h2>HR Employee Details</h2>
  <table border="1">
	<tr bgcolor="#9acd32">
	  <th style="text-align:left">Employee Name</th>
	  <th style="text-align:left">Email Id</th>
	  <th style="text-align:left">Telephone</th>
	  <th style="text-align:left">Job Title</th>
	  <th style="text-align:left">Salary</th>
	  <th style="text-align:left">Hire Date</th>
	</tr>
	<xsl:for-each select="Employee/EmployeeDetails">
	<tr>
	  <td><xsl:value-of select="name"/></td>
	  <td><xsl:value-of select="email"/></td>
	  <td><xsl:value-of select="telephone"/></td>
	  <td><xsl:value-of select="job"/></td>
	  <td><xsl:value-of select="salary"/></td>
	  <td><xsl:value-of select="hire_date"/></td>
	</tr>
	</xsl:for-each>
  </table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="EmployeeDetails.xsl"?>
<Employee>
<EmployeeDetails>
	<name>William Gietz</name>
	<email>wgietz2@oramail.com</email>
	<telephone>515.123.8181</telephone>
	<job>Public Accountant</job>
	<salary>8390</salary>
	<hire_date>24-AUG-2001</hire_date>
</EmployeeDetails>
<EmployeeDetails>
	<name>Stuart Little</name>
	<email>slittle@oramail.com</email>
	<telephone>515.156.8075</telephone>
	<job>CEO</job>
	<salary>1</salary>
	<hire_date>01-JAN-1991</hire_date>
</EmployeeDetails>
</Employee>
The corresponding HTML transformed code
<html> 
<body>
<h2>HR Employee Details</h2>
<table border="1">
    <tr bgcolor="#9acd32">
      <th style="text-align:left">Employee Name</th>
      <th style="text-align:left">Email Id</th>
	  <th style="text-align:left">Telephone</th>
	  <th style="text-align:left">Job Title</th>
	  <th style="text-align:left">Salary</th>
	  <th style="text-align:left">Hire Date</th>
    </tr>
	<tr>	
		<td>William Gietz</td>
		<td>wgietz2@oramail.com</td>
		<td>515.123.8181</td>
		<td>Public Accountant</td>
		<td>8390</td>
		<td>24-AUG-2001</td>
	</tr>
	<tr>
		<td>Stuart Little</td>
		<td>slittle@oramail.com</td>
		<td>515.156.8075</td>
		<td>CEO</td>
		<td>1</td>
		<td>01-JAN-1991</td>
	</tr>
</table>	
</body>
</html>
In Oracle, you can achieve the same result using the XMLTRANSFORM function as below. XMLTRANSFORM is a SQL function that can be called from PLSQL. 
It accepts two arguments, both of which need to be XMLTYPE, one being the XML document that you want to transform and the other is the XSLT document.
DECLARE
l_xml XMLTYPE;
l_xsl XMLTYPE;
l_transformed XMLTYPE;

BEGIN

   l_xml := XMLTYPE.CREATEXML('<?xml version="1.0" encoding="UTF-8"?>
								<?xml-stylesheet type="text/xsl" href="EmployeeDetails.xsl"?>
								<Employee>
								<EmployeeDetails>
								  <name>William Gietz</name>
								  <email>wgietz2@oramail.com</email>
								  <telephone>515.123.8181</telephone>
								  <job>Public Accountant</job>
								  <salary>8390</salary>
								  <hire_date>24-AUG-2001</hire_date>
								</EmployeeDetails>
								<EmployeeDetails>
								  <name>Stuart Little</name>
								  <email>slittle@oramail.com</email>
								  <telephone>515.156.8075</telephone>
								  <job>CEO</job>
								  <salary>1</salary>
								  <hire_date>01-JAN-1991</hire_date>
								</EmployeeDetails>
								</Employee>');

   l_xsl := XMLTYPE.CREATEXML('<?xml version="1.0" encoding="UTF-8"?>
								  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
								  <xsl:template match="/">
								  <html> 
								  <body>
									<h2>HR Employee Details</h2>
									<table border="1">
									  <tr bgcolor="#9acd32">
										<th style="text-align:left">Employee Name</th>
										<th style="text-align:left">Email Id</th>
									  <th style="text-align:left">Telephone</th>
										<th style="text-align:left">Job Title</th>
									  <th style="text-align:left">Salary</th>
										<th style="text-align:left">Hire Date</th>
									  </tr>
									  <xsl:for-each select="Employee/EmployeeDetails">
									  <tr>
										<td><xsl:value-of select="name"/></td>
										<td><xsl:value-of select="email"/></td>
									  <td><xsl:value-of select="telephone"/></td>
										<td><xsl:value-of select="job"/></td>
									  <td><xsl:value-of select="salary"/></td>
										<td><xsl:value-of select="hire_date"/></td>
									  </tr>
									  </xsl:for-each>
									</table>
								  </body>
								  </html>
								  </xsl:template>
								  </xsl:stylesheet>');

   SELECT XMLTRANSFORM(l_xml, l_xsl)
   INTO l_transformed
   FROM dual;

   DBMS_OUTPUT.PUT_LINE(l_transformed.getstringval());

END;
/
Or you can also use 'transform' member function of XMLTYPE. Below is an example.
DECLARE

l_xml XMLTYPE;
l_xsl XMLTYPE;
l_transformed XMLTYPE;

BEGIN

 l_xml := XMLTYPE.CREATEXML('<?xml version="1.0" encoding="UTF-8"?>
								<?xml-stylesheet type="text/xsl" href="EmployeeDetails.xsl"?>
								<Employee>
								<EmployeeDetails>
								  <name>William Gietz</name>
								  <email>wgietz2@oramail.com</email>
								  <telephone>515.123.8181</telephone>
								  <job>Public Accountant</job>
								  <salary>8390</salary>
								  <hire_date>24-AUG-2001</hire_date>
								</EmployeeDetails>
								<EmployeeDetails>
								  <name>Stuart Little</name>
								  <email>slittle@oramail.com</email>
								  <telephone>515.156.8075</telephone>
								  <job>CEO</job>
								  <salary>1</salary>
								  <hire_date>01-JAN-1991</hire_date>
								</EmployeeDetails>
								</Employee>');

 l_xsl := XMLTYPE.CREATEXML('<?xml version="1.0" encoding="UTF-8"?>
								  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
								  <xsl:template match="/">
								  <html> 
								  <body>
									<h2>HR Employee Details</h2>
									<table border="1">
									  <tr bgcolor="#9acd32">
										<th style="text-align:left">Employee Name</th>
										<th style="text-align:left">Email Id</th>
									  <th style="text-align:left">Telephone</th>
										<th style="text-align:left">Job Title</th>
									  <th style="text-align:left">Salary</th>
										<th style="text-align:left">Hire Date</th>
									  </tr>
									  <xsl:for-each select="Employee/EmployeeDetails">
									  <tr>
										<td><xsl:value-of select="name"/></td>
										<td><xsl:value-of select="email"/></td>
									  <td><xsl:value-of select="telephone"/></td>
										<td><xsl:value-of select="job"/></td>
									  <td><xsl:value-of select="salary"/></td>
										<td><xsl:value-of select="hire_date"/></td>
									  </tr>
									  </xsl:for-each>
									</table>
								  </body>
								  </html>
								  </xsl:template>
								  </xsl:stylesheet>');

 l_transformed := l_xml.transform(xsl => l_xsl);

 DBMS_OUTPUT.PUT_LINE(l_transformed.getstringval());

END;
/

God's Word for the day

True and False Wisdom
The wise remain silent until the right moment,
  But a boasting fool misses the right moment
Whoever talks too much is detested
  and whoever pretends to authority is hated
Sirach 20:7-8

Gospel teachings of Jesus 
Woe to unrepentent cities
Woe to you Chorazin! Woe to you, Bethsaida!
  For if the deeds of power done in you had been done in Tyre and Sidon,
They would have repented long ago in sackcloth and ashes.
  But I tell you, on the day of judgement it will be more tolerable
for Tyre and Sidon than for you.
  And you Capernaum, will you be exalted to heaven?
No you will be brought down to Hades.
  For if the deeds of power done in you had been done in Sodom,
It would have remained until this day.
  But I tell you on the day of judgement, it will be more tolerable
For the land of Sodom than for you.
Mathew 11:21-24

No comments:

Post a Comment