开发者

How to transform custom XML into HTML table

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:

<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>kart2006@gmail.com</td>
            <td>karthikrangaraj@yahoo.com</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>karthycse@yahoo.co.in</td>
            <td></td>
            <td></td>
         </tr>
         <tr>
            <td>ColomboGiorgia</td>
            <td>Giorgia Colombo</td>
            <td></td>
            <td></td>
            <td>giorgiacolombo89@libero.it</td>
            <td></td>
         </tr>
      </table>
   </body>
</html>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜