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 8685345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:32:47+00:00 2026-06-12T22:32:47+00:00

I need to transform a div-based XHTML layout into a table based layout using

  • 0

I need to transform a div-based XHTML layout into a table based layout using XSLT 1.0. For the basic transformation, I have a stylesheet (below) that creates the table structure just fine.

I cannot figure out how to parse multiple class attributes on the input XHTML to add table-specific attributes to the output. (Yes, I would like these as new table attributes, even though classes are copied across)

My sample XHTML is:

<div class="table align-center">
    <div class="tr">
        <div class="td"><p>Table Cell 1</p></div>
        <div class="td"><p>Table Cell 2</p></div>
    </div>
</div>

A basic XSL that builds the table structure OK, is as follows:

<xsl:template match="node()|@*">
    <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="div[contains(@class, 'table')]">
    <table>
        <xsl:copy-of select="attribute::node()"/>
        <xsl:apply-templates/>
    </table>
</xsl:template>

<xsl:template match="div[contains(@class, 'tr')]">
    <tr>
        <xsl:copy-of select="attribute::node()"/>
        <xsl:apply-templates/>
    </tr>
</xsl:template>

<xsl:template match="div[contains(@class, 'td')]">
    <td>
        <xsl:copy-of select="attribute::node()"/>
        <xsl:apply-templates/>
    </td>
</xsl:template>

This stylesheet produces:

<table class="table align-center">
    <tr class="tr">
        <td class="td"><p>Table Cell 1</p></td>
        <td class="td"><p>Table Cell 2</p></td>
    </tr>
</table>

What I would like to produce is this:

<table class="table align-center" align="center">
    <tr class="tr">
        <td class="td"><p>Table Cell 1</p></td>
        <td class="td"><p>Table Cell 2</p></td>
    </tr>
</table>

Is it possible to do this with XSLT 1.0? I’d like the solution to be general enough to add 2 or more classes and parse them to add the required table attributes.

Thank you!

  • 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-06-12T22:32:48+00:00Added an answer on June 12, 2026 at 10:32 pm

    This XSLT 1.0 style-sheet…

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes"/>
    <xsl:strip-space elements="*" />
    
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    
      <xsl:template match="div[starts-with(@class, 'table ')]">
        <table>
            <xsl:call-template name="extract-class">
              <xsl:with-param name="class-list" select="normalize-space( substring-after(@class,'table '))" />
            </xsl:call-template>
            <xsl:apply-templates select="@*|node()"/>
        </table>
    </xsl:template>
    
    <xsl:template match="div[starts-with(@class, 'tr ')]">
        <tr>
            <xsl:apply-templates select="@*|node()"/>
        </tr>
    </xsl:template>
    
    <xsl:template match="div[starts-with(@class, 'td ')]">
        <td>
            <xsl:apply-templates select="@*|node()"/>
        </td>
    </xsl:template>
    
    <xsl:template name="extract-class">
      <xsl:param name="class-list" />
      <xsl:if test="contains($class-list,'-')">
        <xsl:variable name="name-value" select="substring-before(concat($class-list,' '),' ')" />
        <xsl:attribute name="{substring-before($name-value,'-')}">
          <xsl:value-of select="substring-after($name-value,'-')" />
        </xsl:attribute>
        <xsl:call-template name="extract-class">
          <xsl:with-param name="class-list" select="normalize-space(substring-after($class-list,' '))" />
        </xsl:call-template> 
      </xsl:if>  
    </xsl:template>
    
    </xsl:stylesheet>
    

    …when applied to this document…

    <div class="table align-center border-1 cellspacing-5">
        <div class="tr">
            <div class="td"><p>Table Cell 1</p></div>
            <div class="td"><p>Table Cell 2</p></div>
        </div>
    </div>
    

    …yields…*

    <table align="center" border="1" cellspacing="5" class="table align-center border-1 cellspacing-5">
      <tr class="tr">
        <td class="td">
          <p>Table Cell 1</p>
        </td>
        <td class="td">
          <p>Table Cell 2</p>
        </td>
      </tr>
    </table>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XSLT 1.0 (2.0 is not an option) stylesheet which produces XHTML.
I have an XSLT transform I am using to process an XML file, inserting
I need to transform my nested sets structure (mysql) into json for this spacetree
I need to transform a list into dictionary as follows. The odd elements has
For an iPad application I need to transform some CoffeeScript files into JavaScript files
I need a way to transform numeric HTML entities into their plain-text character equivalent.
I am attempting to rotate or transform a div using the CSS Transform property.
Lets say i have the following web page: <html> <body> <div class="transform"> <span>1</span> </div>
I have 2 files. 1->index.xsl <xsl:stylesheet version=1.0 xmlns:xsl=http://www.w3.org/1999/XSL/Transform> <!-- Includes --> <xsl:include href=navigation.xsl />
i have image set div tag like below <div style=width: 600px; background: #CCC;padding: 50px;

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.