Link to home
Start Free TrialLog in
Avatar of numberkruncher
numberkruncherFlag for United Kingdom of Great Britain and Northern Ireland

asked on

XSLT lookup column for table.

First off, I am using XSLT 2.0 with Saxon.

I need a function which will lookup the column which is associated with a cell, or return null. The function must take into consideration that a column may span multiple cells (this is the part that I am struggling with).
XML Example:
===========
<table>
	<column test="1" span="2"/>
	<column test="2"/>
	<column test="3" span="2"/>
	<thead>
		<row>
			<cell>A</cell>
			<cell>B</cell>
			<cell>C</cell>
			<cell>D</cell>
			<cell>E</cell>
		</row>
	</thead>
</table>
 
 
 
Requirement:
===========
 
<xsl:function name="my:lookup-column">
  <!--  ???  -->
</xsl:function>
 
 
<xsl:template match="cell">
   <!-- Lookup column -->
   <xsl:variable name="column-info" select="my:lookup-column(.)"/>
 
 
   <!-- If a column was found then... -->
   <xsl:if test="$column-info">
      <!-- Display the test attribute of the column -->
      <xsl:value-of select="concat(.,'(',$column-info/@test,'), ')"/>
   </xsl:if>
</xsl:template>
 
 
 
Output:
======
 
1, 1, 2, 3, 3
 
A(1), B(1), C(2), D(3), E(3)

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of abel
abel
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of numberkruncher

ASKER

Hi Abel!

The second example is the one that I am after. The column element contains several attributes which are required when rendering each cell within the table. I have not long got home from work, and am going to have a play with the examples you have provided me with.

At the moment I have a template which matches each row, and then another which matches each cell. So access to the columns is needed.

Your first example gave me an idea. Would it better to have a template which matches against the rows and then the columns and then somehow select the associated cells? or is that just going to complicate things?

Thanks again, your a star!
> or is that just going to complicate things?

from my experience you can take it that it complicates things. But you can of course try ;-). It won't be too hard because your system is relatively simple, but using a function the way I laid out is quite easy to manage. And don't worry about performance things (like the function being called many times or something), because functions in XSLT are bound to always give the same result on the same input, which means that they will be well-optimized in a compiled stylesheet.
Thank you very much again for your help!