问题描述:

I have an XML file generated using javax.sql.rowset.WebRowSet.writeXml which looks like:

<metadata>

This section has column properties like name / label etc

</metadata>

<data>

<currentRow>

<columnValue>Ken</columnValue>

<columnValue>12</columnValue>

<columnValue>USA</columnValue>

</currentRow>

</data>

I want to convert this to look like:

<Class>

<Student>

<name>Ken</name>

<ID>12</ID>

<location>USA</location>

</Student>

</Class>

How can I do the conversion? I need this to transform the XML to a HTML table.

网友答案:

The following stylesheet produces the desired result:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <Class>
            <xsl:apply-templates select="/*/data/currentRow" />
        </Class>
    </xsl:template>
    <xsl:template match="currentRow">
        <Student>
            <xsl:apply-templates select="columnValue" />
        </Student>
    </xsl:template>
    <xsl:template match="columnValue[1]">
        <name><xsl:apply-templates/></name>
    </xsl:template>
    <xsl:template match="columnValue[2]">
        <ID><xsl:apply-templates/></ID>
    </xsl:template>
    <xsl:template match="columnValue[3]">
        <location><xsl:apply-templates/></location>
    </xsl:template>
</xsl:stylesheet>

Note: Added a root node to the given source to make it well-formed.

相关阅读:
Top