Girish Reddy Irala
Girish Reddy Irala

Reputation: 57

XSLT for XML to HTML table

I have an xml like this.

< cars>  
< car>  
< ford color="black" >eco sport< /ford >    
< maruti color="red" >zen< /maruti>  
< hyundai color="blue" >accent< /hyundai>  
< /car>  
< car>  
< ford color="green" >figo< /ford >    
< maruti color="red" >swift< /maruti>  
< hyundai color="white" >santro< /hyundai>  
< /car>  
< /cars>

I need to have an HTMl table like

COMPANY   COLOR    MODEL  
 ford     black   ecosport  
 maruti   red     zen    
 hyundai  green   figo  
 ford     red     swift  
 maruti   red     zen    
 hyundai  white   santro

Could anyone help me with this.. I am very new to XSLT and spent some valuble time for this but no luck. Thanks in advance. Below is the XSLT i have been trying with

<xsl:template match ="/">
    <html>
      <head>
        <title> Cars </title>
      </head>
      <body>
        <xsl:apply-templates />
      </body>
    </html>
</xsl:template>

<xsl:template match="cars">
    <table width="400" border="1" >
        <tr bgcolor = "#cccccc" >
            <td>COMPANY</td>
            <td>COLOR</td>
            <td>MODEL</td>
        </tr>
<xsl:for-each select="car">
        <tr>
        <xsl:for-each select="./">
            <td><xsl:value-of select="name()"/></td>    
            <td> <xsl:value-of select="@color" /> </td>
                <td> <xsl:value-of select="ford" /> </td>
        </xsl:for-each>
         </tr>
</xsl:for-each>
     </table>
</xsl:template>

Upvotes: 0

Views: 339

Answers (2)

JLRishe
JLRishe

Reputation: 101748

You almost had it. Please give this a try:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
  <xsl:strip-space elements="*" />

  <xsl:template match ="/">
    <html>
      <head>
        <title> Cars </title>
      </head>
      <body>
        <xsl:apply-templates />
      </body>
    </html>
  </xsl:template>

  <xsl:template match="cars">
    <table width="400" border="1" >
      <tr bgcolor = "#cccccc" >
        <th>COMPANY</th>
        <th>COLOR</th>
        <th>MODEL</th>
      </tr>
      <xsl:apply-templates />
    </table>
  </xsl:template>

  <xsl:template match="car/*">
    <tr>
      <td>
        <xsl:value-of select="name()"/>
      </td>
      <td>
        <xsl:value-of select="@color" />
      </td>
      <td>
        <xsl:value-of select="." />
      </td>
    </tr>
  </xsl:template>
</xsl:stylesheet>

Upvotes: 1

Tim C
Tim C

Reputation: 70648

You've a few (small) problems with you XSLT. Firstly, this line is not valid

<xsl:for-each select="./">

What you need to be doing at this point, as you are positioned on a car element, is to iterate over each child element, like so

<xsl:for-each select="*">

Second, you want to create the tr element within this loop, rather than outside as you are currently doing. Thirdly, to get the model, you are doing this

<xsl:value-of select="ford" />

When it should be just this

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

Try this XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/">
        <html>
            <head>
                <title> Cars </title>
            </head>
            <body>
                <xsl:apply-templates/>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="cars">
        <table width="400" border="1">
            <tr bgcolor="#cccccc">
                <td>COMPANY</td>
                <td>COLOR</td>
                <td>MODEL</td>
            </tr>
            <xsl:for-each select="car">
                <xsl:for-each select="*">
                    <tr>
                        <td>
                            <xsl:value-of select="name()"/>
                        </td>
                        <td>
                            <xsl:value-of select="@color"/>
                        </td>
                        <td>
                            <xsl:value-of select="."/>
                        </td>
                    </tr>
                </xsl:for-each>
            </xsl:for-each>
        </table>
    </xsl:template>
</xsl:stylesheet>

When applied to your XML (with the whitespace in element names removed!), the following is output

<html>
    <head>
        <title> Cars </title>
    </head>
    <body>
        <table width="400" border="1">
            <tr bgcolor="#cccccc">
                <td>COMPANY</td>
                <td>COLOR</td>
                <td>MODEL</td>
            </tr>
            <tr>
                <td>ford</td>
                <td>black</td>
                <td>eco sport</td>
            </tr>
            <tr>
                <td>maruti</td>
                <td>red</td>
                <td>zen</td>
            </tr>
            <tr>
                <td>hyundai</td>
                <td>blue</td>
                <td>accent</td>
            </tr>
            <tr>
                <td>ford</td>
                <td>green</td>
                <td>figo</td>
            </tr>
            <tr>
                <td>maruti</td>
                <td>red</td>
                <td>swift</td>
            </tr>
            <tr>
                <td>hyundai</td>
                <td>white</td>
                <td>santro</td>
            </tr>
        </table>
    </body>
</html>

Upvotes: 2

Related Questions