Reputation: 548
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 <B>
before their names.
Sample XML is as follows
<Root>
<row ID="1" ITEM="Label" col1="visit no">
<row ID="1" ITEM="Data" col1="1">
<row ID="1" ITEM="Label" col1="Date">
<row ID="1" ITEM="Data" col1="8/11/2018">
<row ID="1" ITEM="Label" col1="PatName" col2="DocName" col3="DocName" />
<row ID="2" ITEM="Data" col1="Sam Hul" col2="Dr Mike" col3="Vegas Hospital"/>
<row ID="2" ITEM="Data" col1="Dan Brown" col2="Dr Matt" col3="California Hospital"/>
I want to convert the above XML into below HTML content. The col1,col2, col3 are being generated using a loop. so they will have names will have an incremental number after col.
The expected output HTML is
<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>
<tr><td><b>PatName</b></td><td><b>DocName</b></td><td><b>DocName</b></td></tr>
<tr><td>Sam Hul</td><td>Dr Mike</td><td>Vegas Hospital</td></tr>
<tr><td>Dan Brown</td><td>Dr Matt</td><td>California Hospital</td></tr>
</table>
This is what i am trying at this moment.Buts its generating the single TR
<tr>
<xsl:for-each select="row/@*">
<td><xsl:value-of select="."/></td>
</xsl:for-each>
</tr>
Upvotes: 0
Views: 720
Reputation: 117175
--- edited in response to changed reqirement ---
That's a strange way to build a table, but if that's what you want, try:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/Root">
<table border="1">
<xsl:for-each select="row">
<tr>
<xsl:apply-templates select="@*[starts-with(name(), 'col')]"/>
</tr>
</xsl:for-each>
</table>
</xsl:template>
<xsl:template match="@*[../@ITEM='Label']">
<th>
<xsl:value-of select="."/>
</th>
</xsl:template>
<xsl:template match="@*">
<td>
<xsl:value-of select="."/>
</td>
</xsl:template>
</xsl:stylesheet>
Applied to a well-formed (!) XML input:
XML
<Root>
<row ID="1" ITEM="Label" col1="visit no"/>
<row ID="1" ITEM="Data" col1="1"/>
<row ID="1" ITEM="Label" col1="Date"/>
<row ID="1" ITEM="Data" col1="8/11/2018"/>
<row ID="1" ITEM="Label" col1="PatName" col2="DocName" col3="DocName" />
<row ID="2" ITEM="Data" col1="Sam Hul" col2="Dr Mike" col3="Vegas Hospital"/>
<row ID="2" ITEM="Data" col1="Dan Brown" col2="Dr Matt" col3="California Hospital"/>
</Root>
the (rendered) result will be:
Upvotes: 1