XSLT lookup column for table.

Posted on 2009-04-17
Last Modified: 2013-11-18
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

Question by:numberkruncher
    LVL 39

    Accepted 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

    LVL 13

    Author Comment

    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!
    LVL 39

    Expert Comment

    > 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.
    LVL 13

    Author Closing Comment

    Thank you very much again for your help!

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    SQL Remove Elements from XML 6 38
    WordPress/WooCommerce security best practices? 2 58
    ColdFusion Rereplace 3 51
    Select distinct 25 44
    I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
    Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL ( several years ago, it seemed like now was a good time to updat…
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now