We help IT Professionals succeed at work.
Get Started

Using XSLT 1.0  to convert XML to HTML Table

SusanLIMS
SusanLIMS asked
on
1,315 Views
Last Modified: 2014-05-21
I want to convert an XML file to an HTML table using XSLT.  This is happening inside another application, and only XSL version 1.0 is supported.  I'm new at using XSLT, so any help would be much appreciated!

Following are the original XML and the desired HTML output.  Note that the output is a "flat" table, where the XML input has levels.  I've also attached these as files if that is helpful.

Thanks much!

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<LabRequest>
      <PATIENT>
            <X_EXTERNAL_ID>9649ABC113</X_EXTERNAL_ID>
            <FIRST_NAME>Nancy</FIRST_NAME>
            <LAST_NAME>Smithson</LAST_NAME>
      </PATIENT>
      <CUSTOMER>
            <NAME>JUNK_ALFALFA</NAME>
            <X_CITY>Oklahoma City</X_CITY>
            <X_STATE>OK</X_STATE>
            <X_ZIPCODE>11557</X_ZIPCODE>
      </CUSTOMER>
      <SAMPLE>
            <X_TUBE_ID>Tube001</X_TUBE_ID>
            <SAMPLED_DATE>2014-03-20T09:05:00.000-05:00</SAMPLED_DATE>
            <X_SPECIMEN_SOURCE>BLOOD</X_SPECIMEN_SOURCE>      
            <ANALYSIS>
                  <X_LOINC>34487-9</X_LOINC>      
            </ANALYSIS>      
            <ANALYSIS>
                  <X_LOINC>12345-1</X_LOINC>      
            </ANALYSIS>
      </SAMPLE>
      <SAMPLE>
            <X_TUBE_ID>Tube002</X_TUBE_ID>
            <SAMPLED_DATE>2014-05-25T05:33:00.000-05:00</SAMPLED_DATE>
            <X_SPECIMEN_SOURCE>SERUM</X_SPECIMEN_SOURCE>      
            <ANALYSIS>
                  <X_LOINC>55888-1</X_LOINC>      
            </ANALYSIS>      
      </SAMPLE>
</LabRequest>

Desired HTML:
<table>
      <tr>
            <th>Table</th>
            <th>Field</th>
            <th>Value</th>
      </tr>
      <tr>
            <td>PATIENT</td>
            <td>X_EXTERNAL_ID</td>
            <td>9649ABC113</td>
      </tr>
      <tr>
            <td>PATIENT</td>
            <td>FIRST_NAME</td>
            <td>Nancy</td>
      </tr>
      <tr>
            <td>PATIENT</td>
            <td>LAST_NAME</td>
            <td>Smithson</td>
      </tr>
      <tr>
            <td>CUSTOMER</td>
            <td>NAME</td>
            <td>JUNK_ALFALFA</td>
      </tr>
      <tr>
            <td>CUSTOMER</td>
            <td>X_CITY</td>
            <td>Oklahoma City</td>
      </tr>
      <tr>
            <td>CUSTOMER</td>
            <td>X_STATE</td>
            <td>OK</td>
      </tr>
      <tr>
            <td>CUSTOMER</td>
            <td>X_ZIPCODE</td>
            <td>11557</td>
      </tr>
      <tr>
            <td>SAMPLE</td>
            <td>X_TUBE_ID</td>
            <td>Tube001</td>
      </tr>
      <tr>
            <td>SAMPLE</td>
            <td>SAMPLED_DATE</td>
            <td>03/20/2014 09:05:00 AM</td>
      </tr>
      <tr>
            <td>SAMPLE</td>
            <td>X_SPECIMEN_SOURCE</td>
            <td>BLOOD</td>
      </tr>
      <tr>
            <td>ANALYSIS</td>
            <td>X_LOINC</td>
            <td>34487-9</td>
      </tr>      
      <tr>
            <td>ANALYSIS</td>
            <td>X_LOINC</td>
            <td>12345-1</td>
      </tr>
      <tr>
            <td>SAMPLE</td>
            <td>X_TUBE_ID</td>
            <td>Tube002</td>
      </tr>
      <tr>
            <td>SAMPLE</td>
            <td>SAMPLED_DATE</td>
            <td>05/25/2014 05:33:00 AM</td>
      </tr>
      <tr>
            <td>SAMPLE</td>
            <td>X_SPECIMEN_SOURCE</td>
            <td>SERUM</td>
      </tr>
      <tr>
            <td>ANALYSIS</td>
            <td>X_LOINC</td>
            <td>55888-1</td>
      </tr>      
</table>
ExampleShort2.xml
ExampleShort2.html
Comment
Watch Question
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 2 Answers and 7 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE