I have an XML file and I’m trying convert it into a (table( HTML file.
This is my XML file:
<?xml version="1.0" encoding="UTF-8"?>
<CONTACTS>
<CONTACT>
<FirstName>AfgZohal</FirstName>
<LastName>Zohal Afg</LastName>
<EMAILS/>
</CONTACT>
<CONTACT>
<FirstName>Rangarajkarthik</FirstName>
<LastName>karthik Rangaraj</LastName>
<EMAILS>
<EMail>
<Type>gmail</Type>
<Value>kart2006@gmail.com</Value>
</EMail>
<EMail>
<Type>yahoo</Type>
<Value>karthikrangaraj@yahoo.com</Value>
</EMail>
</EMAILS>
</CONTACT>
<CONTACT>
<FirstName>ReganPaul</FirstName>
<LastName>Paul Michael Regan</LastName>
<URL>http://www.facebook.com/profile.php?id=1660466705</URL>
<EMAILS/>
</CONTACT>
<CONTACT>
<FirstName>keyankarthik</FirstName>
<LastName>karthik keyan</LastName>
<EMAILS>
<EMail>
<Type>yahoo</Type>
<Value>karthycse@yahoo.co.in</Value>
</EMail>
</EMAILS>
</CONTACT>
<CONTACT>
<FirstName>ColomboGiorgia</FirstName>
<LastName>Giorgia Colombo</LastName>
<EMAILS>
<EMail>
<Type>libero</Type>
<Value>giorgiacolombo89@libero.it</Value>
</EMail>
</EMAILS>
</CONTACT>
</CONTACTS>
This is my XSL file:
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<!-- / forward slash is used to denote a patern that matches
the root node of the XML document -->
<xsl:template match ="/" >
<html>
<head>
<title> ContactMatrix</title>
</head>
<body>
<xsl:apply-templates />
</body>
</html>
</xsl:template>
<xsl:template match="CONTACTS" >
<table width="400" border="1" >
<tr bgcolor = "#546789" >
<td>FirstName</td>
<td>LastName</td>
<td>Gmail</td>
<td>Yahoo</td>
<td>Libero</td>
<td>URL</td>
</tr>
<xsl:for-each select="CONTACT" >
<tr>
<td> <xsl:value-of select="FirstName"/> </td>
<td> <xsl:value-of select="LastName"/> </td>
<!-- here we use /@ to access the value of an attribute -->
<td> <xsl:value-of select="Type/Value=@gmail.com"/> </td>
<td> <xsl:value-of select="@yahoo.com"/> </td>
<td> <xsl:value-of select="@libero.it"/> </td>
<td> <xsl:value-of select="URL"/> </td>
</tr>
</xsl:for-each>
</table>
</xsl:template >
</xsl:stylesheet >
In my html table, the value for gmail,yahoo,libero = False.
This is the sample code in html file for the FirstName:Rangarajkarthik
<tr>
<td>Rangarajkarthik</td><td>karthik Rangaraj</td><td>false</td><td>false</td><td>false</td><td></td>
</tr>
Please assist me.
You are not correctly selecting the element value for the emails. For instance, by the instruction:
You are asking for selecting a node with name
@yahoo.comamong the child nodes ofCONTACT. You should use:This instruction gets the text of
Value, child ofEMailwhoseTypeis ‘yahoo’. The code within theselectattribute is just an XPath selection using a predicate[..]to match the wanted element. I really suggest to have a look at W3C tutorial site, and spend some minute on it.Your
xsl:for-eachapproach to build a table is not wrong and you are close to a good solution; however a more XSLT way to accomplish that is just usingxsl:apply-templatesin the correct place.When applied to your input, produces: