elwood Posted February 23, 2011 Share Posted February 23, 2011 Hi there!I'm trying to set the align-attribute for every td-element according to the valign-attribute of the col-element with the same column-attribute. Does anybody kno how to do this?I'm a newbie and would be glad for every help. Thank you!Example col-element with column="c0" has the valign-attribute "left", so the td-element with column="c0" should get the align-attribute "left" as well.My XML-File: <?xml version="1.0" encoding="windows-1252" ?> <report records="25" lines="25" columns="5" rows="25"><column_desc><col colnum="c0" fieldname="TEST1" alias="TEST1" datatype="char" width="30" focus_format="A30" description="" accept="" help_message="" title="Test 1" within="" property="" reference="" valign="left" /> <col colnum="c1" fieldname="TEST2" alias="TEST2" datatype="char" width="5" focus_format="A5" description="" accept="" help_message="" title="Test 2" within="" property="" reference="" valign="left" /> <col colnum="c2" fieldname="TEST3" alias="TEST3" datatype="integer" width="3" focus_format="I3" description="" accept="" help_message="" title="Test 3" within="" property="" reference="" valign="right" /> <col colnum="c3" fieldname="TEST4" alias="TEST4" datatype="char" width="40" focus_format="A40" description="" accept="" help_message="" title="Test 4" within="" property="" reference="" valign="left" /> <col colnum="c4" fieldname="TEST5" alias="TEST5" datatype="integer" width="3" focus_format="I3" description="" accept="" help_message="" title="Test 5" within="" property="" reference="" valign="right" /> </column_desc><table><tr linetype="data" linenum="1"><td colnum="c0">TEST1</td> <td colnum="c1">USER</td> <td colnum="c2" rawvalue="5">5</td> <td colnum="c3">Test Daten</td> <td colnum="c4" rawvalue="15">15</td> </tr></table></report> My XSL File: <?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="html" indent="yes"/><xsl:template match="/"> <html> <body> <table> <xsl:apply-templates/> </table> </body> </html></xsl:template><xsl:template match="column_desc"> <tr> <xsl:apply-templates select="col"/> </tr></xsl:template><xsl:template match="col"> <th> <xsl:attribute name="align"> <xsl:value-of select="@valign" /> </xsl:attribute> <xsl:value-of select="@title"/> </th></xsl:template><xsl:template match="tr"> <tr> <xsl:apply-templates select="td"/> </tr></xsl:template><xsl:template match="td"> <td> <input align="right" type="text"> <xsl:attribute name="value"> <xsl:value-of select="."/> </xsl:attribute> </input> </td></xsl:template></xsl:stylesheet> Link to comment Share on other sites More sharing options...
JohnBampton Posted February 23, 2011 Share Posted February 23, 2011 Hi, if I understand you correctly - you need something like this: <xsl:template match="td"> <xsl:variable name="colnum" select="@colnum"></xsl:variable> <td align="{//col[@colnum = $colnum]/@valign}"> <input align="right" type="text"> <xsl:attribute name="value"> <xsl:value-of select="."/> </xsl:attribute> </input> </td> </xsl:template> Cheers, John Bampton. Link to comment Share on other sites More sharing options...
Martin Honnen Posted February 23, 2011 Share Posted February 23, 2011 With XSLT, any time you want to cross reference elements you should consider defining a key with xsl: key and to use the key function: <xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="html" indent="yes"/><xsl:key name="col-by-colnum" match="col" use="@colnum"/><xsl:template match="/"> <html> <body> <table> <xsl:apply-templates/> </table> </body> </html></xsl:template><xsl:template match="column_desc"> <tr> <xsl:apply-templates select="col"/> </tr></xsl:template><xsl:template match="col"> <th align="{@valign}"> <xsl:value-of select="@title"/> </th></xsl:template><xsl:template match="tr"> <tr> <xsl:apply-templates select="td"/> </tr></xsl:template><xsl:template match="td"> <td align="{key('col-by-colnum', @colnum)/@valign}"> <input align="right" type="text" value="{.}"/> </td></xsl:template></xsl:stylesheet> That is my suggestion for your main question. However as you seem to want to transform your XML table format into a HTML table format and you want to align columns I would consider not to put the align attribute on each cell but rather to create HTML "col" elements with the needed "align" attribute. Link to comment Share on other sites More sharing options...
elwood Posted February 23, 2011 Author Share Posted February 23, 2011 Thank you both for your replies! I tried the second answer and it worked perfectly. That is my suggestion for your main question. However as you seem to want to transform your XML table format into a HTML table format and you want to align columns I would consider not to put the align attribute on each cell but rather to create HTML "col" elements with the needed "align" attribute.And yes Martin, you were right, I'm going to use the col element. I totally forgot that it exists. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.