[xsl] Creating HTML by selecting XML attribute names

Subject: [xsl] Creating HTML by selecting XML attribute names
From: "Julian Karsten Arthur" <julian.arthur@xxxxxxxxxxxxxxxxxx>
Date: Thu, 15 Sep 2005 14:38:09 -0300
Hello,

I am trying to convert some XML to HTML using the latest version of Xalan.
However, I am having difficulty in selecting the correct elements from my XML
source file. What I am trying to do is select the data from the
rows/row/fields in the order it appears in the columns/column, and output it
to a HTML table. The XML itself is generated so the attributes of the columns
could change each time. I.e. in this example we have the attributes 'name' and
'age', but next time round they could be 'title', 'name', 'address'.

How can I select each of the attributes of the column, and then query the
fields in the order specified in the source file?

I have something that works for the XML in this example, but not if the XML
changes.

XML file >>>

<?xml version="1.0" encoding="UTF-8"?>
<table>
<table-config>
	<classes>
	<table-title-class>tituloClass</table-title-class>
	...
	</classes>
	<columns>
	<column field="name" label="Nome"/>
	<column align="right" field="age" label="Idade"/>
	</columns>
</table-config>
<rows>
	<row>
	<field age="45" name="Nome 1"/>
	</row>
	<row>
	<field age="54" name="Nome 2"/>
	</row>
	...
</rows>

XSL file excerpt >>>

<xsl:template match="rows">
    <xsl:apply-templates select="row/field"/>
    </xsl:template>

    <xsl:template match="row/field">
    <!--
    I have this: <field age="45" name="Nome 1"/>. But the attributes may
change.
    I need to get a handle on the attributes.
    -->

    <xsl:variable name="fieldName" select="@name"/>
    <xsl:variable name="fieldValue" select="@age"/>
    <tr>
    <td>
        <xsl:value-of select="$fieldName"/><br/>
    </td>
    <td>

        <!-- This successfully loops through the columns -->
        <xsl:for-each select="/table/table-config/columns/column">

        <xsl:if test="@field = 'age'">
        <xsl:attribute name="align">
            <xsl:value-of select="@align"/>
        </xsl:attribute>

        </xsl:if>
        </xsl:for-each>
        <xsl:value-of select="$fieldValue"/><br/>
    </td>
    </tr>
    </xsl:template>

The output I want is:
Name		Age
Nome 1		45
Nome 2             54

...but it needs to change if the input XML provides additional columns. Is
this possible using XSLT?

Thoughts appreciated. With Regards,
Julian

Current Thread