Re: [xsl] XML to Table

Subject: Re: [xsl] XML to Table
From: "Veronica Sanchez" <vsguiseris@xxxxxxxxxxx>
Date: Tue, 20 Apr 2004 10:54:49 +0200
thanks!!! Both suggestions have been very useful to me, but now I have two xsl that make the transform quickly:

<?xml version="1.0" encoding="UTF-8"?>
<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="BASE/ROWSET">
<BASE>
<ROWSET>
<xsl:for-each select="ROW">
<ROW>
<xsl:variable name="row" select="@num"/>
<xsl:attribute name="num"><xsl:value-of select="$row"/></xsl:attribute>
<xsl:for-each select="//DETALLE/CAMPO">
<xsl:element name="{@nom}">
<xsl:value-of select="$row/parent::ROW/*[name()=current()/@nom]"/>
</xsl:element>
</xsl:for-each>
</ROW>
</xsl:for-each>
</ROWSET>
<xsl:copy-of select="//DETALLE"/>
</BASE>
</xsl:template>
</xsl:stylesheet>


----------------------

<?xml version="1.0" encoding="UTF-8"?>
<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="/BASE">
		<xsl:copy>
			<ROWSET>
				<xsl:apply-templates select="ROWSET/ROW"/>
			</ROWSET>
			<xsl:copy-of select="//DETALLE"/>
		</xsl:copy>
	</xsl:template>

<xsl:template match="ROW">
<xsl:copy>
<xsl:attribute name="num"><xsl:value-of select="current()/@num"/></xsl:attribute>
<xsl:apply-templates select="../../DETALLE">
<xsl:with-param name="prow" select="current()/@num"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>


<xsl:template match="DETALLE">
<xsl:param name="prow" select="0"/>
<xsl:for-each select="CAMPO">
<xsl:element name="{current()/@nom}">
<xsl:value-of select="/BASE/ROWSET/ROW[@num = $prow]/*[name()=current()/@nom]"/>
</xsl:element>
</xsl:for-each>
</xsl:template>


</xsl:stylesheet>

What is better, with or without templates?

-----------------------

I have a xml like this:

<?xml version="1.0" encoding="UTF-8"?>
<BASE>
	<ROWSET>
		<ROW num="1">
			<LITVERSION>14 Hdi 70 Cv Magic</LITVERSION>
			<ORDENVERSION>9800079</ORDENVERSION>
			<NOVIN8>26961143</NOVIN8>
			<ASALON>31</ASALON>
			<CODCOLOR>YT</CODCOLOR>
			<COLOR>Gris Orageux</COLOR>
			<CODTAPIZADO>Z6FT</CODTAPIZADO>
			<TAPIZADO>Z6FT</TAPIZADO>
			<CODCPER>MZ02</CODCPER>
			<!-- ... almost 40 fields, sometimes some of them are absent -->
		</ROW>
		<ROW num="2">... <!-- variable num of rows -->
	<ROWSET>
	<DETALLE> <!-- To specify the fields to visualizing -->
		<CAMPO nom="LITVERSION" class="a" orden="1" check="1"/>
		<CAMPO nom="NOVIN8" class="b" orden="2" enlace="doAccion"  check="1"/>
		<CAMPO nom="COLOR" class="c" orden="3"/>
		<CAMPO nom="TAPIZADO" class="a" orden="4"/>
		<CAMPO nom="OPCIONES" class="b" orden="5"/>
	</DETALLE>
</BASE>


With one of the previos xsl i get the next xml:



<?xml version="1.0" encoding="UTF-8"?> <BASE> <ROWSET> <ROW num="1"> <LITVERSION>14 Hdi 70 Cv Magic</LITVERSION> <NOVIN8>26961143</NOVIN8> <COLOR>Gris Orageux</COLOR> <TAPIZADO>Z6FT</TAPIZADO> <OPCIONES>MZ02</OPCIONES> </ROW> <ROW num="2"> <LITVERSION>14 Hdi 70 Cv Magic</LITVERSION> <NOVIN8>26987553</NOVIN8> <COLOR>Azul Lucia</COLOR> <TAPIZADO>Z6FT</TAPIZADO> <OPCIONES>MZ02</OPCIONES> </ROW> <ROW num="3"> <LITVERSION>14 Hdi 70 Cv Magic</LITVERSION> <NOVIN8>26997843</NOVIN8> <!-- ... --> </ROW> <!-- ... --> <ROWSET> <DETALLE> <!-- To specify the fields to visualizing --> <CAMPO nom="LITVERSION" class="a" orden="1" check="1"/> <CAMPO nom="NOVIN8" class="b" orden="2" enlace="doAccion" check="1"/> <CAMPO nom="COLOR" class="c" orden="3"/> <CAMPO nom="TAPIZADO" class="a" orden="4"/> <CAMPO nom="OPCIONES" class="b" orden="5"/> </DETALLE> </BASE>


Now i have another xsl to obtain the table:



<?xml version="1.0" encoding="UTF-8"?>
<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:variable name="c"><xsl:text>'</xsl:text></xsl:variable>


<xsl:template match="/BASE">
<STYLE TYPE="text/css">
.a { BACKGROUND-COLOR: #FFFFAA; BORDER: #dddddd 1pt solid; FONT-SIZE: .785em; TEXT-ALIGN: right; }
.b { BACKGROUND-COLOR: #FFFFDA; BORDER: #dddddd 1pt solid; FONT-SIZE: .785em; TEXT-ALIGN: right; }
.c { BACKGROUND-COLOR: #FFAACC; BORDER: #dddddd 1pt solid; FONT-SIZE: .785em; TEXT-ALIGN: right; }
</STYLE>
<table border="1">
<xsl:for-each select="ROWSET/ROW">
<tr>
<xsl:attribute name="id"><xsl:value-of select="@num"/></xsl:attribute>
<xsl:apply-templates select="*"><xsl:with-param name="row" select="@num"/></xsl:apply-templates>
</tr>
</xsl:for-each>
</table>
</xsl:template>


<xsl:template match="ROW/*">
<xsl:param name="row" select="0"/>
<xsl:variable name="nombre" select="name()"/>
<xsl:variable name="valor" select="."/>
<xsl:variable name="campo" select="//DETALLE/CAMPO[@nom=$nombre]"/>
<td>
<xsl:attribute name="class"><xsl:value-of select="$campo/@class"/></xsl:attribute>
<xsl:if test="$campo/@check and $valor!='' ">
<input type="checkbox">
<xsl:attribute name="name"><xsl:value-of select="concat($nombre, $row)"/></xsl:attribute>
</input>
</xsl:if>
<xsl:variable name="enlace" select="$campo/@enlace"/>
<xsl:if test="$enlace and $valor!='' ">
<a>
<xsl:attribute name="href">
<xsl:value-of select="concat($enlace,'(',$c,$valor,$c,')')"/>
</xsl:attribute>
<xsl:value-of select="$valor"/>
</a>
</xsl:if>
<xsl:if test="not ($enlace)">
<xsl:value-of select="$valor"/>
</xsl:if>
</td>
</xsl:template>


</xsl:stylesheet>


and the result xml is:



<?xml version="1.0" encoding="UTF-8"?>
<STYLE TYPE="text/css">
.a { BACKGROUND-COLOR: #FFFFAA; BORDER: #dddddd 1pt solid; FONT-SIZE: .785em; TEXT-ALIGN: right; }
.b { BACKGROUND-COLOR: #FFFFDA; BORDER: #dddddd 1pt solid; FONT-SIZE: .785em; TEXT-ALIGN: right; }
.c { BACKGROUND-COLOR: #FFAACC; BORDER: #dddddd 1pt solid; FONT-SIZE: .785em; TEXT-ALIGN: right; }
</STYLE>
<table border="1">
<tr id="1">
<td class="a"><input type="checkbox" name="LITVERSION1"/>14 Hdi 70 Cv Magic</td>
<td class="b">
<input type="checkbox" name="NOVIN81"/>
<a href="doAccion('26961143')">26961143</a>
</td>
<td class="c">Gris Orageux</td>
<td class="a">Z6FT</td>
<td class="b">MZ02</td>
</tr>
<tr id="2">
<td class="a"><input type="checkbox" name="LITVERSION2"/>14 Hdi 70 Cv Magic</td>
<td class="b">
<input type="checkbox" name="NOVIN82"/>
<a href="doAccion('26987553')">26987553</a>
</td>
<td class="c">Azul Lucia</td>
<td class="a">Z6FT</td>
<td class="b">MZ02</td>
</tr>
<!-- ... -->
</table>



It would be better to use 'template name' for each case? (one for 'checks', another for 'links'...)
I have tried to do it but it seemed to me that the code was complicated very much, probably I have complicated something that surely is very simple.



Thanks again.


_________________________________________________________________
?Estas pensando en cambiar de coche? Todas los modelos de serie y extras en MSN Motor. http://motor.msn.es/researchcentre/


Current Thread