0

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>
9
  • Can you use XSLT 2.0? Commented Aug 15, 2016 at 17:30
  • probably yes..will that be of more help. Commented Aug 15, 2016 at 17:32
  • Yes. This is basically a grouping issue, and XSLT 2.0 has grouping features. Commented Aug 15, 2016 at 17:33
  • yup then definitely i can use it. Commented Aug 15, 2016 at 17:34
  • Are you sure? This not a matter of decision - the question is does your processor support XSLT 2.0 or not. Commented Aug 15, 2016 at 17:37

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

6 Comments

I can see the data formatted. But there is a problem. There are multiple forms in the XML. all of the data in Non-Log are clubbed together. That should not happen.
Not sure what the problem is. In your question, you said: "I want to put all the <nonLog> records in one table and all the <log> records in another."
I have updated the question. There are multiple form tags in each XML. I want to create a separate table for each form log and non log entries. That means if there are two forms there will be 4 tables created. Two table in each form.
Can you please help me to write this in single table ? Labels and then data. In the same format it is in XML. Labels will go to TH and Data will go to TD.
Just add a xsl:for-each select="FormData" instruction at the body level.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.