3

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>[email protected]</Value>
        </EMail>
        <EMail>
        <Type>yahoo</Type>
        <Value>[email protected]</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>[email protected]</Value>
        </EMail>
    </EMAILS>
</CONTACT>
<CONTACT>
    <FirstName>ColomboGiorgia</FirstName>
    <LastName>Giorgia Colombo</LastName>
    <EMAILS>
        <EMail>
        <Type>libero</Type>
        <Value>[email protected]</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/[email protected]"/> </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.

3
  • So, what is the question? I don't see any. Commented Jun 18, 2011 at 20:49
  • I've changed the title to match better the contents of this question. Feel free to revert back to the original if you care about it. Commented Jun 20, 2011 at 21:38
  • It's ok and It's well framed question. Commented Jun 23, 2011 at 19:56

1 Answer 1

3

You are not correctly selecting the element value for the emails. For instance, by the instruction:

<xsl:value-of select="@yahoo.com"/>

You are asking for selecting a node with name @yahoo.com among the child nodes of CONTACT. You should use:

  <xsl:value-of select="EMAILS/
                EMail[Type='yahoo']/Value"/>

This instruction gets the text of Value, child of EMail whose Type is 'yahoo'. The code within the select attribute 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-each approach 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 using xsl:apply-templates in the correct place.

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

    <xsl:output method="html" indent="yes"/>
    <xsl:strip-space elements="*"/> 

    <xsl:template match="CONTACTS" >
        <html>
            <head>
                <title>ContactMatrix</title>
            </head>
            <body>
                <table width="400" border="1" >
                    <tr bgcolor = "#546789" >
                        <th>FirstName</th>
                        <th>LastName</th>
                        <th>Gmail</th>
                        <th>Yahoo</th>
                        <th>Libero</th>
                        <th>URL</th>
                    </tr>
                    <xsl:apply-templates select="CONTACT"/>
                </table>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="CONTACT">
        <tr>
            <td><xsl:value-of select="FirstName"/> </td>
            <td><xsl:value-of select="LastName"/> </td>
            <td><xsl:value-of select="EMAILS/
                    EMail[Type='gmail']/Value"/>
          </td>
            <td><xsl:value-of select="EMAILS/
                    EMail[Type='yahoo']/Value"/> 
            </td>
            <td><xsl:value-of select="EMAILS/
                    EMail[Type='libero']/Value"/> 
            </td>
            <td><xsl:value-of select="URL"/></td>
        </tr>
    </xsl:template>

</xsl:stylesheet>

When applied to your input, produces:

<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

      <title>ContactMatrix</title>
   </head>
   <body>
      <table width="400" border="1">
         <tr bgcolor="#546789">
            <th>FirstName</th>
            <th>LastName</th>
            <th>Gmail</th>
            <th>Yahoo</th>
            <th>Libero</th>
            <th>URL</th>
         </tr>
         <tr>
            <td>AfgZohal</td>
            <td>Zohal Afg</td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
         </tr>
         <tr>
            <td>Rangarajkarthik</td>
            <td>karthik Rangaraj</td>
            <td>[email protected]</td>
            <td>[email protected]</td>
            <td></td>
            <td></td>
         </tr>
         <tr>
            <td>ReganPaul</td>
            <td>Paul Michael Regan</td>
            <td></td>
            <td></td>
            <td></td>
            <td>http://www.facebook.com/profile.php?id=1660466705</td>
         </tr>
         <tr>
            <td>keyankarthik</td>
            <td>karthik keyan</td>
            <td></td>
            <td>[email protected]</td>
            <td></td>
            <td></td>
         </tr>
         <tr>
            <td>ColomboGiorgia</td>
            <td>Giorgia Colombo</td>
            <td></td>
            <td></td>
            <td>[email protected]</td>
            <td></td>
         </tr>
      </table>
   </body>
</html>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for the correction and this information is really useful to me. Definitely I'll spend some extra time to learn properly. I thank you very much once again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.