Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Creating a table with XSLT

From: "Joris Gillis" <roac@-------.-->
To: 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 &lt;= $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 &lt;= $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)
"Η αλήθεια και το λάδι πάντα βγαίνουν από πάνω"


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent