Avatar of SusanLIMS
SusanLIMS
 asked on

Using XSLT 1.0 to convert XML to HTML Table

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
XMLHTML

Avatar of undefined
Last Comment
Gertone (Geert Bormans)

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
zc2

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Gertone (Geert Bormans)

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Gertone (Geert Bormans)

I would make another suggestion
I know the result is as you wanted it, but you could add an empty row at the start of each SAMPLE
in order to visualy seperate them, how else is the consumer of this table going to figure out to which sample a certain aspect belongs

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" encoding="iso-8859-1"/>
    <xsl:template match="LabRequest">
        <TABLE>
            <tr>
                <th>Table</th>
                <th>Field</th>
                <th>Value</th>
            </tr>
            <xsl:apply-templates select="*"/>
        </TABLE>
    </xsl:template>
    
    <xsl:template match="PATIENT|CUSTOMER|ANALYSIS">
        <xsl:apply-templates select="*" mode="row"/>
    </xsl:template>
    
    <xsl:template match="SAMPLE">
        <TR><TD colspan=3">-----------------------------------------------------</TD></TR>
        <xsl:apply-templates select="*[not(name() = 'ANALYSIS')]" mode="row"/>
        <xsl:apply-templates select="ANALYSIS"/>
    </xsl:template>
    
    <xsl:template match="*" mode="row">
        <TR>
            <TD><xsl:value-of select="name(..)"/></TD>
            <TD><xsl:value-of select="name()"/></TD>
            <TD><xsl:value-of select="."/></TD>
        </TR>
    </xsl:template>
    
</xsl:stylesheet>

Open in new window


just a thought (and showing you how to)
SusanLIMS

ASKER
This is awesome..thank you so much.  I got the first two examples to work, but I get an error on the 3rd.  I like the idea of a separating line between the samples - any idea what the error might be?
I tried the following two online tools - and both threw an error on the 3rd example that contains the following line:

        <TR><TD colspan=3">-----------------------------------------------------</TD></TR>


http://www.freeformatter.com/xsl-transformer.html#ad-output
http://xslt.online-toolz.com/tools/xslt-transformation.php

Really appreciate the help...thanks...
Gertone (Geert Bormans)

Oh, well, the third example was not tested,
apparently I missed a quote for the attribute

<TR><TD colspan="3">-----------------------------------------------------</TD></TR>

Open in new window

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
SusanLIMS

ASKER
Fantastic...THANKS to both of you!!
Gertone (Geert Bormans)

welcome