How to use XSL to transpose tables and rows

Hello,

A colleague gave me an XML file similar to the following:

<reports>
   <weekly_report>
       <report_date>5 April 2005</report_date>
       <shipments>503</shipments>
       <invoices>500</invoices>
       <claims>26</claims>
   </weekly_report>
   <weekly_report>
       <report_date>12 April 2005</report_date>
       <shipments>496</shipments>
       <invoices>496</invoices>
       <claims>18</claims>
   </weekly_report>
</reports>

I know how to use XSL to put this in a table as follows:

Report Date    Shipments     Invoices    Claims
5 April 2005        503              500          26
12 April 2005      496              496          18

But he would like the rows and columns transposed--i.e.,

Report Date       5 April 2005      12 April 2005
Shipments               503                  496
Invoices                   500                  496
Claims                      26                    18

Is there an easy way to do this with XSL while still keeping the current structure of the XML file?  If not, is there a difficult way? ;)  or some other option (like JavaScript)?

Many thanks--

Kathryn
KathrynGZAsked:
Who is Participating?
 
dualsoulConnect With a Mentor Commented:
yes, why not :)
it's rahter easy, for instance this one:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
      
      <xsl:template match="/">
            <table>
                  <tbody>

                        <tr>
                              <td>Report Date:</td>
                              <xsl:apply-templates select="/reports/weekly_report/report_date" />
                        </tr>
                        <tr>
                              <td>Shipments:</td>
                              <xsl:apply-templates select="/reports/weekly_report/shipments" />
                        </tr>
                        <tr>
                              <td>Invoices:</td>
                              <xsl:apply-templates select="/reports/weekly_report/invoices" />
                        </tr>
                        <tr>
                              <td>Claims: </td>
                              <xsl:apply-templates select="/reports/weekly_report/claims" />
                        </tr>
                  </tbody>
            </table>
      </xsl:template>
      
      <xsl:template match="report_date | shipments | invoices | claims">
            <td><xsl:value-of select="." /></td>
      </xsl:template>
      
</xsl:stylesheet>
0
 
KathrynGZAuthor Commented:
Perfect!! Worked exactly as needed the first time. To say thanks, I added an extra 25 points :)

Kathryn
0
 
dualsoulCommented:
;)
0
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.

All Courses

From novice to tech pro — start learning today.