Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries >Thread Prev - Creating a table with XSLT >Thread Next - Re: Creating a table with XSLT Re: Creating a table with XSLTTo: NULL Date: 3/5/2005 12:48:00 PM Tempore 13:01:13, die Saturday 05 March 2005 AD, hinc in foro {microsoft.public.xsl} scripsit JoKer <ugo+x@l...>:
> I would like to create a table via XSLT where putting fields values in order
> of 1_1 (first cell in first row and columns), 1_2 (second cell in first row
> and second column), 1_5 (fifth cell in first row and fifth column), 2_1
> (first cell in second row first column) and so on like in a matrix.
Hi,
Supposing that the XML looks like:
<root>
<n ID="1_1">a</n>
<n ID="1_3">d</n>
<n ID="1_4">e</n>
<n ID="2_1">f</n>
<n ID="3_5">g</n>
</root>
and that the according ouput should look like:
___________
|a| |d|e| |
|f| | | | |
| | | | |g|
- - - - -
You can use something like this:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:key name="field" match="n" use="@ID"/>
<xsl:variable name="maxrow">
<xsl:apply-templates select="//n" mode="X">
<xsl:sort select="substring-before(ID,'_')" data-type="number"/>
</xsl:apply-templates>
</xsl:variable>
<xsl:variable name="maxcol">
<xsl:apply-templates select="//n" mode="X">
<xsl:sort select="substring-after(ID,'_')" data-type="number"/>
</xsl:apply-templates>
</xsl:variable>
<xsl:variable name="ColCount" select="number(substring-after($maxcol,'_'))"/>
<xsl:variable name="RowCount" select="number(substring-before($maxrow,'_'))"/>
<xsl:template match="*" mode="X">
<xsl:if test="position() = last()"><xsl:value-of select="@ID"/></xsl:if>
</xsl:template>
<xsl:template match="root">
<table>
<xsl:call-template name="rows"/>
</table>
</xsl:template>
<xsl:template name="rows">
<xsl:param name="rc" select="1"/>
<xsl:if test="$rc <= $RowCount">
<tr>
<xsl:call-template name="cols">
<xsl:with-param name="rc" select="$rc"/>
</xsl:call-template>
</tr>
<xsl:call-template name="rows">
<xsl:with-param name="rc" select="$rc + 1"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template name="cols">
<xsl:param name="rc"/>
<xsl:param name="cc" select="1"/>
<xsl:if test="$cc <= $ColCount">
<td>
<xsl:value-of select="key('field',concat($rc,'_',$cc))"/>
</td>
<xsl:call-template name="cols">
<xsl:with-param name="cc" select="$cc + 1"/>
<xsl:with-param name="rc" select="$rc"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
regards,
--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-view.cgi?userid=38041)
"Η αλήθεια και το λάδι πάντα βγαίνουν από πάνω"
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
