0
votes

I have an XML file and need to transform it into an HTML table with XSLT. I tried to follow an example from w3cschools. It's too burdensome to type it in here because it's not in English, but I'll try to explain my problem:

I have a root element "catalog" and multiple child elements "cd". When I edit the table in XSLT, I only get the values for the first child node "cd".

Can anyone tell where the mistake is?

1
You really do need to show your XSLT, because otherwise it will be almost impossible for anyone to tell you where your mistake is. And you should also show a sample of your XML. It does not have to the complete XML, but maybe one with just one or two cd elements in. It doesn't matter if the names of the CDs are not in English. Thank you. - Tim C
Please provide a minimal reproducible example if you want people helping you debug the problem. - Patrick Mevzek

1 Answers

0
votes

Assuming the XML that you are using looks as below

<catalog>
    <cd>
        <title>Empire Burlesque</title>
        <artist>Bob Dylan</artist>
        <price>10.90</price>
        <year>1985</year>
    </cd>
    <cd>
        <title>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <price>9.90</price>
        <year>1988</year>
    </cd>
    <cd>
        <title>Greatest Hits</title>
        <artist>Dolly Parton</artist>
        <price>9.90</price>
        <year>1982</year>
    </cd>
</catalog>

the XSLT to print the data into a table with all the values is as below. The <xsl:for-each> loop for the repeating <cd> elements will result in a tabular format for the data.

<xsl:template match="catalog">
    <html>
        <body>
            <table border="1" cellspacing="0" cellpadding="2"> 
                <tr bgcolor="#9acd32">
                    <th style="text-align:left">Title</th>
                    <th style="text-align:left">Artist</th>
                    <th style="text-align:right">Price</th>
                    <th style="text-align:right">Year</th>
                </tr>
                <xsl:for-each select="cd">
                    <tr>
                        <td style="text-align:left"><xsl:value-of select="title" /></td>
                        <td style="text-align:left"><xsl:value-of select="artist" /></td>
                        <td style="text-align:right"><xsl:value-of select="price" /></td>
                        <td style="text-align:right"><xsl:value-of select="year" /></td>
                    </tr>
                </xsl:for-each>
            </table>
        </body>
    </html>
</xsl:template>