[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 634
  • Last Modified:

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
0
KathrynGZ
Asked:
KathrynGZ
  • 2
1 Solution
 
dualsoulCommented:
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now