Reputation: 369
I currently trying to use XSL to transform the following XML structure
<table name="PERSONS" schema="INO_PERSONS_MGMT" syntheticPK="ID">
<columns>
<column type="NUMBER" primary="true" unique="true">key</column>
<column type="varchar">name</column>
<column type="varchar">comment</column>
<column type="varchar">email</column>
</columns>
<records>
<record key="1" name="Test1" email="[email protected]"/>
<record key="2" name="Test2" comment="something" mail="[email protected]"/>
</records>
to the following xml output:
<table:table table:name="INO_PERSONS_MGMT.PERSONS">
<table:table-row>
<table:table-cell>
<text:p>key</text:p>
</table:table-cell>
<table:table-cell>
<text:p>name</text:p>
</table:table-cell>
<table:table-cell>
<text:p>comment</text:p>
</table:table-cell>
<table:table-cell>
<text:p>email</text:p>
</table:table-cell>
</table:table-row>
<table:table-row>
<table:table-cell>
<text:p>NUMBER</text:p>
</table:table-cell>
<table:table-cell>
<text:p>varchar</text:p>
</table:table-cell>
<table:table-cell>
<text:p>varchar</text:p>
</table:table-cell>
<table:table-cell>
<text:p>varchar</text:p>
</table:table-cell>
</table:table-row>
<table:table-row>
<table:table-cell>
<text:p>1</text:p>
</table:table-cell>
<table:table-cell>
<text:p>Test1</text:p>
</table:table-cell>
<table:table-cell>
<text:p></text:p>
</table:table-cell>
<table:table-cell>
<text:p>[email protected]</text:p>
</table:table-cell>
</table:table-row>
<table:table-row>
<table:table-cell>
<text:p>2</text:p>
</table:table-cell>
<table:table-cell>
<text:p>Test2</text:p>
</table:table-cell>
<table:table-cell>
<text:p>something</text:p>
</table:table-cell>
<table:table-cell>
<text:p>[email protected]</text:p>
</table:table-cell>
</table:table-row>
</table:table>
So the structure of the data table-rows (row 2 and 3) should be defined by the structure of the columns. So it should iterate over the given columns and generate the table cells even if they are not defined in the records.
Upvotes: 0
Views: 35
Reputation: 116959
The output that you show is not well-formed XML because the table:
prefix is not bound to a namespace.
To minimize the example to the actual problem, consider the following stylesheet:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/table">
<xsl:variable name="cols" select="columns/column" />
<table name="{@name}">
<!-- header rows -->
<row>
<xsl:for-each select="$cols">
<cell>
<xsl:value-of select="."/>
</cell>
</xsl:for-each>
</row>
<row>
<xsl:for-each select="$cols">
<cell>
<xsl:value-of select="@type"/>
</cell>
</xsl:for-each>
</row>
<!-- data rows -->
<xsl:for-each select="records/record">
<xsl:variable name="curr-row" select="." />
<row>
<xsl:for-each select="$cols">
<cell>
<xsl:value-of select="$curr-row/@*[name()=current()]"/>
</cell>
</xsl:for-each>
</row>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
Applied to your input example, the result will be:
<?xml version="1.0" encoding="UTF-8"?>
<table name="PERSONS">
<row>
<cell>key</cell>
<cell>name</cell>
<cell>comment</cell>
<cell>email</cell>
</row>
<row>
<cell>NUMBER</cell>
<cell>varchar</cell>
<cell>varchar</cell>
<cell>varchar</cell>
</row>
<row>
<cell>1</cell>
<cell>Test1</cell>
<cell/>
<cell>[email protected]</cell>
</row>
<row>
<cell>2</cell>
<cell>Test2</cell>
<cell>something</cell>
<cell/>
</row>
</table>
This should be easy enough for you to adapt to your situation.
Note that "email"
is not the same as "mail"
- which is why the last cell of the second row is empty.
Upvotes: 2