Bryant Bryant - 1 year ago 60
HTML Question

How to transform XML attributes to an HTML table

Here is an excerpt from an xsl document (inside a template technically):

<td><xsl:value-of select='@INSTANCE'/></td>
<td><xsl:value-of select='@SWVER'/></td>
<td><xsl:value-of select='@SYSTEMID'/></td>
<td><xsl:value-of select='@SYSTIME'/></td>
<td><xsl:value-of select='@SYSMEM'/></td>
<td><xsl:value-of select='@CUTMEM'/></td>
<td><xsl:value-of select='@FILEMEM'/></td>
<td><xsl:value-of select="@CALCONFIG"/></td>

Is there some way that I can avoid the redundancy of writing out the attributes both as table headers and as the attribute selection? I cannot use external sources.

I was thinking I could define some xsl variable that contains a basic structure, as follows and generate the table from there.


The XML data is just a bunch of tags, of the same value that contain at least the above listed attributes. Each tag looks something like this:


Answer Source

To illustrate the point I made in a comment to your question, consider the following example:


    <item color="red" name="alpha" size="small" garbage="123" id="1"/>
    <item color="green" name="bravo" size="medium" garbage="456" id="2"/>
    <item color="blue" name="charlie" size="large" garbage="789" id="3"/>

XSLT 1.0

<xsl:stylesheet version="1.0" 
<xsl:output method="html" encoding="utf-8"/>
<xsl:strip-space elements="*"/>

<xsl:variable name="columns" select="document('')/xsl:stylesheet/my:columns" />

<xsl:template match="/root">
    <table border="1">
            <xsl:for-each select="$columns/col">
                    <xsl:value-of select="." />
        <xsl:for-each select="item">
            <xsl:variable name="attributes" select="@*" />
                <xsl:for-each select="$columns/col">
                        <xsl:value-of select="$attributes[name()=current()]" />


Result (rendered)

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download