Skip to content Skip to sidebar Skip to footer

Convert XML List To HTML Table Using XSL

Hello I was struggling with this for the past days and I could not find a good answer nor solution. I have an XML file with a list of objects like this:

Solution 1:

lineitmes folllow a regular pattern.

Then I believe it could be simply:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/LineItems">
    <xsl:variable name="columns" select="count(ColumnMetadata)"/>
    <table border="1">
        <thead >
            <tr>
                <xsl:for-each select="ColumnMetadata">
                    <th>
                        <xsl:value-of select="Term"/>
                    </th>
                </xsl:for-each>
            </tr>
        </thead>
        <tbody>
            <xsl:for-each select="LineItems[position() mod $columns = 1]">
                <tr>
                    <xsl:for-each select=". | following-sibling::LineItems[position() &lt; $columns]">
                        <td>
                            <xsl:value-of select="*"/>
                        </td>
                    </xsl:for-each>
                </tr>
            </xsl:for-each>
        </tbody>
    </table>
</xsl:template>

</xsl:stylesheet>

Post a Comment for "Convert XML List To HTML Table Using XSL"