• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 993
  • Last Modified:

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:
	<column test="1" span="2"/>
	<column test="2"/>
	<column test="3" span="2"/>
<xsl:function name="my:lookup-column">
  <!--  ???  -->
<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,'), ')"/>
1, 1, 2, 3, 3
A(1), B(1), C(2), D(3), E(3)

Open in new window

  • 2
  • 2
1 Solution
What do you mean in this context with lookup column? The "de facto" way in XSLT to create a lookup table is to make one in XML:

<xsl:variable name="lookup">
    <find name="column" id="someid" />

<xsl:template match="column[name() = $lookup/find/@name and @test = $lookup/find/@id]">
   <xsl:text> do something with column</xsl:text>
but when reading onwards in your code, it seems that you actually want to know what column a certain cell belongs to and then get the @test value of it. The simplest approach, using your requested design, is the following (not tested, may have some glitches):

-- Abel --


<!-- example of how to deal with spanning columns -->
<xsl:variable name="columns">
   <xsl:apply-templates select="/table/column" mode="select-columns" />
<xsl:template match="column" mode="select-columns">
    <xsl:for-each select="1 to @span">
        <xsl:copy-of select="." />
<xsl:function name="my:lookup-column">
   <xsl:param name="cell" />
   <xsl:value-of select="$columns/column[count($cell/preceding-sibling::cell) + 1]/@test" />

Open in new window

numberkruncherAuthor Commented:
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.
numberkruncherAuthor Commented:
Thank you very much again for your help!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now