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 <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>
5
  • Where exactly are you stuck with this? Commented Aug 11, 2016 at 16:07
  • @michael.hor257k How to count number of tags with colxxx in each row node Commented Aug 11, 2016 at 16:26
  • Why would you need to count them? -- Please post your stylesheet so we can fix the issue you are having problems with. We are not here to write your code for you. Commented Aug 11, 2016 at 16:30
  • @michael.hor257k Thanks for responding. I am unable to write any thing for this part of code. If you can direct me towards any thing i will write it my self. Just give me a head start. Commented Aug 11, 2016 at 16:33
  • @michael.hor257k Please see the updated question. I have added code into it. Commented Aug 11, 2016 at 16:53

1 Answer 1

1

--- 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:

enter image description here

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for the answer. But there is a slight problem. :) There are many Labels in that XML. This code is printing the all the headers at top. I just need to put then in sequence as they are in XML and just need to bold where ITEM='Label'.
Please edit your question and show an example of input with multiple label rows, and the expected output.
Isn't this solution assuming that @* will return the attributes in a predictable order? I think the apply-templates select="@*" should have a child xsl:sort select="name()"
@MichaelKay I believe it's quite safe to assume that (a) the order of attributes is the same in each row and (b) the processor will return them in document order (even though it is not obligated to do so).
@michael.hor257k Thanks a lot. it worked like charm . I
|

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.