Re: Swapping table rows and columns

Subject: Re: Swapping table rows and columns
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Tue, 06 Jul 1999 08:04:22 -0700
At 99/07/05 14:50 +0100, Steve Slatcher wrote:
>I am considering the design an application using XML and XSLT to display
tables as HTML. In the most obvious design, the XML implicitly defines what
will be displayed as rows and what will be displayed as columns; the outer
level elements in the XML correspond to the outer level <tr> row tags in HTML.
>
>Is it possible to "swap rows and columns" using XSLT?  

You can achieve what you ask for, but the technique isn't "swapping rows
and columns".

>If so, is it really a practical thing to attempt from a point of view of
complexity and efficiency? 

Very much so.

>Or should I resign myself to the fact that the element nesting in the XML
should reflect the final apperance of the table?

Not at all.

The approach to the solution you desire is much like the approach everyone
needs to consider when writing a stylesheet:  traverse your source tree in
the order the result tree needs to be generated.

David's solution did exactly this, though the missing commentary may help
better understand why his solution works as it does.

You've described your source data as being "outer elements" with "inner
elements", representing (respectively) rows and columns in the row.

You've described your desired output as "swapping columns for rows", which
could be reworded as "orient the rows of output as being the corresponding
inner elements within each of the available outer elements".

Can you see I've expressed your problem as the generation of data in the
order of the result tree?  The result tree is a bunch of rows, so I've
described what it is you need in each row: the corresponding inner element
from each of the outer elements.

Without even knowing your input data, David's solution illustrates the
technique generically:  he indexes through each of the "outer constructs",
one "inner construct" item at a time, thus building the result tree in
result tree order.

I've tried to describe this generically because I'm seeing a FAQ being "can
XSL do *this*?  (where *this* is some characterization of a problem)  The
answer is really "if you can describe *this* algorithmically by how to
traverse your source tree in order to obtain, in result tree order, each of
the components of the result tree, yes XSL can do *this*" ... then the only
task for the stylesheet writer is writing that algorithm to walk the source
tree in the order desired (as David did by indexing each of his input row
items as being rows of output).

I hope this helps.

............. Ken

--
G. Ken Holman                    mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.             http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0   +1(613)489-0999   (Fax:-0995)
Website:  XSL/XML/DSSSL/SGML services, training, libraries, products.
Publications:   Introduction to XSLT (3rd Edition) ISBN 1-894049-00-4
Next instructor-led training:   MS'99 1999-08-16  MT'99 1999-12-05/06


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread