Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1011753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T09:16:55+00:00 2026-05-16T09:16:55+00:00

I’m working on an XSL template to convert an XHTML/hResume document to plain text,

  • 0

I’m working on an XSL template to convert an XHTML/hResume document to plain text, and I’m having trouble with the table layout (no, not layout tables). At the moment I’ve got the following, using the excellent Dave Pawson’s padding template:

<variable name="newline" select="'&#10;'"/>
<template match="xhtml:table">
    <variable name="maxWidth">
        <for-each select="xhtml:tr/xhtml:th | xhtml:tr/xhtml:td">
            <sort select="string-length(child::text()|child::node())" order="descending" data-type="number"/>
            <if test="position() = 1">
                <value-of select="string-length(child::text()|child::node())"/>
            </if>
        </for-each>
    </variable>
    <for-each select="xhtml:tr">
        <for-each select="xhtml:th|xhtml:td">
            <variable name="string">
                <for-each select="child::text()|child::node()">
                    <value-of select="."/>
                </for-each>
            </variable>
            <value-of select="$string"/>
            <call-template name="append-pad">
                <with-param name="length" select="$maxWidth - string-length($string)"/>
            </call-template>
            <text>&#32;</text>
        </for-each>
        <value-of select="$newline"/>
    </for-each>
    <value-of select="$newline"/>
</template>

This produces columns of equal width, but I’d like to improve it in a couple ways:

  • Find and use the max width of each column. For that it’s necessary to store a flexible number of values. I can change maxWidth to do this in the simple cases, but how do you handle spanning columns?
  • Center the contents of spanning columns.

Are there any templates to do something like this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T09:16:56+00:00Added an answer on May 16, 2026 at 9:16 am

    With a “global” (for every cell in table) $maxWith you could handle colspans like this stylesheet (preserving your own logic):

    <stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform" xmlns:xhtml="http://www.w3.org/1999/xhtml">
        <output method="text"/>
        <variable name="newline" select="'&#10;'"/>
        <template match="xhtml:table">
            <variable name="maxWidth">
                <for-each select="xhtml:tr/xhtml:th | xhtml:tr/xhtml:td">
                    <sort select="string-length(child::text()|child::node())" order="descending" data-type="number"/>
                    <if test="position() = 1">
                        <value-of select="string-length(child::text()|child::node())"/>
                    </if>
                </for-each>
            </variable>
            <for-each select="xhtml:tr">
                <for-each select="xhtml:th|xhtml:td">
                    <variable name="string">
                        <for-each select="child::text()|child::node()">
                            <value-of select="."/>
                        </for-each>
                    </variable>
                    <variable name="padding">
                        <choose>
                            <when test="@colspan">
                                <value-of select="$maxWidth * @colspan + @colspan - 1 - string-length($string)"/>
                            </when>
                            <otherwise>
                                <value-of select="$maxWidth - string-length($string)"/>
                            </otherwise>
                        </choose>
                    </variable>
                    <value-of select="$string"/>
                    <call-template name="append-pad">
                        <with-param name="length" select="$padding"/>
                    </call-template>
                    <text>&#32;</text>
                </for-each>
                <value-of select="$newline"/>
            </for-each>
            <value-of select="$newline"/>
        </template>
        <template name="append-pad">
            <param name="length" select="0"/>
            <if test="$length != 0">
                <value-of select="'&#32;'"/>
                <call-template name="append-pad">
                    <with-param name="length" select="$length - 1"/>
                </call-template>
            </if>
        </template>
    </stylesheet>
    

    Input:

    <table xmlns="http://www.w3.org/1999/xhtml">
        <tr>
            <th>First</th>
            <th>Second</th>
            <th>Third</th>
        </tr>
        <tr>
            <td>One</td>
            <td>Two</td>
            <td>Three</td>
        </tr>
        <tr>
            <td colspan="2">Uno</td>
            <td>Tres</td>
        </tr>
    </table>
    

    Output:

    First  Second Third  
    One    Two    Three  
    Uno           Tres   
    

    EDIT: In order to center the cells with colspan, use this stylesheet (now with my own logic):

    <stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform" xmlns:xhtml="http://www.w3.org/1999/xhtml">
        <output method="text"/>
        <variable name="newline" select="'&#10;'"/>
        <template match="xhtml:table">
            <apply-templates>
                <with-param name="maxWidth">
                    <for-each select="xhtml:tr/xhtml:th | xhtml:tr/xhtml:td">
                        <sort select="string-length(.)" order="descending" data-type="number"/>
                        <if test="position() = 1">
                            <value-of select="string-length(.)"/>
                        </if>
                    </for-each>
                </with-param>
            </apply-templates>
            <value-of select="$newline"/>
        </template>
        <template match="xhtml:tr">
            <param name="maxWidth"/>
            <apply-templates>
                <with-param name="maxWidth" select="$maxWidth"/>
            </apply-templates>
            <value-of select="$newline"/>
        </template>
        <template match="xhtml:th|xhtml:td">
            <param name="maxWidth"/>
            <variable name="string">
                <for-each select="child::text()|child::node()">
                    <value-of select="."/>
                </for-each>
            </variable>
            <variable name="padding">
                <choose>
                    <when test="@colspan">
                        <value-of select="($maxWidth * @colspan + @colspan - 1 - string-length($string)) div 2"/>
                    </when>
                    <otherwise>
                        <value-of select="$maxWidth - string-length($string)"/>
                    </otherwise>
                </choose>
            </variable>
            <if test="@colspan">
                <call-template name="append-pad">
                    <with-param name="length" select="floor($padding)"/>
                </call-template>
            </if>
            <value-of select="$string"/>
            <call-template name="append-pad">
                <with-param name="length" select="ceiling($padding)"/>
            </call-template>
            <text>&#32;</text>
        </template>
        <template name="append-pad">
            <param name="length" select="0"/>
            <if test="$length != 0">
                <value-of select="'&#32;'"/>
                <call-template name="append-pad">
                    <with-param name="length" select="$length - 1"/>
                </call-template>
            </if>
        </template>
    </stylesheet>
    

    Output:

    First  Second Third  
    One    Two    Three  
         Uno      Tres   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.