Nimble Fungus
Nimble Fungus

Reputation: 548

Convert dynamic XML to multiple HTML table using XSLT

I want to transform a XML into a readable HTML. Below I am putting a sample part of my XML that I am unable to transform myself and needs some help. The XML may have a variable number of columns that will be generated by the name col1,col2---colxxx. Here If ITEM=Label I am adding before their names I want to put all the <nonLog> records in one table and all the <log> records in another from wise. That means We will have Non-Logs for form 1 Logs for form 1 Non-logs for form 2 then Logs for from 2 .. so on and so fourth

 <Post>
      <FormData>
        <SUBJECT>94</SUBJECT>
        <FORM_OID>TOX</FORM_OID>
        <NonLog>
          <ID>1</ID>
          <SUBJECT_ID>94</SUBJECT_ID>
          <FORM_OID>TOX</FORM_OID>
          <ITEM>Label</ITEM>
          <col1>Visit</col1>
          <col2> AV</col2>
          <col3>AC</col3>
        </NonLog>
        <NonLog>
          <ID>2</ID>
          <SUBJECT_ID>94</SUBJECT_ID>
          <FORM_OID>TOX</FORM_OID>
          <ITEM>Data</ITEM>
          <col1>1t</col1>
          <col2>No</col2>
          <col3></col3>
        </NonLog>   
        <Log>
          <ID>5</ID>
          <SUBJECT_ID>94</SUBJECT_ID>
          <FORM_OID>TOX</FORM_OID>
          <ITEM>Label</ITEM>
          <LOG_REC_POSITION>1</LOG_REC_POSITION>
          <col1>Pat Name</col1>
          <col2>Doc Name</col2>
        </Log>
        <Log>
          <ID>5</ID>
          <SUBJECT_ID>94</SUBJECT_ID>
          <FORM_OID>TOX</FORM_OID>
          <ITEM>Label</ITEM>
          <LOG_REC_POSITION>1</LOG_REC_POSITION>
          <col1>Sam</col1>
          <col2>Dr Mike</col2>
        </Log>
    </Form Data>
   <FormData>
        <SUBJECT>94</SUBJECT>
        <FORM_OID>TOX2</FORM_OID>
        <NonLog>
          <ID>1</ID>
          <SUBJECT_ID>94</SUBJECT_ID>
          <FORM_OID>TOX2</FORM_OID>
          <ITEM>Label</ITEM>
          <col1>Visit</col1>
          <col2> AV</col2>
          <col3>AC</col3>
        </NonLog>
        <NonLog>
          <ID>2</ID>
          <SUBJECT_ID>94</SUBJECT_ID>
          <FORM_OID>TOX2</FORM_OID>
          <ITEM>Data</ITEM>
          <col1>1t</col1>
          <col2>No</col2>
          <col3></col3>
        </NonLog>   
        <Log>
          <ID>5</ID>
          <SUBJECT_ID>94</SUBJECT_ID>
          <FORM_OID>TOX2</FORM_OID>
          <ITEM>Label</ITEM>
          <LOG_REC_POSITION>1</LOG_REC_POSITION>
          <col1>Pat Name</col1>
          <col2>Doc Name</col2>
        </Log>
        <Log>
          <ID>5</ID>
          <SUBJECT_ID>94</SUBJECT_ID>
          <FORM_OID>TOX2</FORM_OID>
          <ITEM>Label</ITEM>
          <LOG_REC_POSITION>1</LOG_REC_POSITION>
          <col1>Sam</col1>
          <col2>Dr Mike</col2>
        </Log>
    </Form Data>
    </Post>

The expected output HTML is

 <Table>
   <tr><td>
   <table>
        <tr><td><b>visit no</b></td></tr>
        <tr><td>1</td></tr>
        <tr><td><b>Date</b></td></tr>
        <tr><td>8/11/2018</td></tr>
    </table>    
    </td></tr>
    <table>
        <tr><td><b>Pat Name</b></td><td><b>Doc Name</b></td></tr>
        <tr><td>Sam Hul</td><td>Dr Mike</td></tr>
    </table><table>
        <tr><td><b>visit no</b></td></tr>
        <tr><td>1</td></tr>
        <tr><td><b>Date</b></td></tr>
        <tr><td>8/11/2018</td></tr>
    </table>    
    </td></tr>
    <table>
        <tr><td><b>Pat Name</b></td><td><b>Doc Name</b></td></tr>
        <tr><td>Sam Hul</td><td>Dr Mike</td></tr>
    </table>

</table>

Upvotes: 0

Views: 267

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117100

it might be possible that there may be multiple Labels in a form. But the whole form is either of type Log or Non_log . So we just need two tables in a form.

If so, you could simply hard-code the two tables.

Try something like:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="UTF-8" />
<xsl:strip-space elements="*"/>

<xsl:template match="/Post">
    <html>
        <body>
            <table>
                <tr>
                    <xsl:apply-templates select="FormData/NonLog[1]" mode="header"/>
                </tr>
                <xsl:apply-templates select="FormData/NonLog[position() > 1]" />
            </table>    
            <table>
                <tr>
                    <xsl:apply-templates select="FormData/Log[1]" mode="header"/>
                </tr>
                <xsl:apply-templates select="FormData/Log[position() > 1]" />
            </table>    
        </body>
    </html>
</xsl:template>

<xsl:template match="FormData/*" mode="header">
    <xsl:for-each select="*[starts-with(name(), 'col')]">
        <th>
            <xsl:value-of select="."/>
        </th>
    </xsl:for-each>
</xsl:template>

<xsl:template match="FormData/*">
    <tr>
        <xsl:for-each select="*[starts-with(name(), 'col')]">
            <td>
                <xsl:value-of select="."/>
            </td>
        </xsl:for-each>
    </tr>
</xsl:template>

</xsl:stylesheet>

Upvotes: 1

Related Questions