Search code examples
xmlxsltxpathxslt-1.0xquery

How to transform XML text into table format


In the example below we are trying to convert XML data into an HTML and here we are facing the challenge due to inconsistant for the element like as '<NM_TORPCOLUMN_DATA>' & '<NM_TORPCOLUMN_DATA><P>' :

Note# If we are removing '<P>' element from the '<NM_TORPCOLUMN_DATA>' then xslt code working fine.

Reference URL : https://xsltfiddle.liberty-development.net/nbiE1aA/1

INPUT XML: enter image description here

<?xml version="1.0" encoding="UTF-8"?>
<NM_TORP_TABLE>
<NM_TORP_TABLE_LIST>
    <NM_TORP_LABEL>Nr</NM_TORP_LABEL>
    <NM_TANDP_TITLE>1</NM_TANDP_TITLE>
    <NM_TORPCOLUMN_DATA>1<BR/>2<BR/>2.1<BR/>2.2<BR/>2.3<BR/>2.4<BR/>2.5<BR/>2.6<BR/>2.7<BR/>2.8<BR/>2.9<BR/>2.10<BR/>2.11<BR/>2.12<BR/>2.13<BR/>2.14<BR/>2.15<BR/>2.16<BR/>2.17<BR/>2.18<BR/>2.19<BR/>2.20<BR/>3<BR/>3.1<BR/>3.2<BR/>3.3<BR/>3.4<BR/>3.5<BR/>3.6<BR/>3.7<BR/>3.8<BR/>3.9<BR/>3.10<BR/>3.11<BR/>3.12<BR/>3.13<BR/>3.14<BR/>3.15<BR/>3.16<BR/>3.17<BR/>3.18<BR/>3.19<BR/>3.20</NM_TORPCOLUMN_DATA>
</NM_TORP_TABLE_LIST>
<NM_TORP_TABLE_LIST>
    <NM_TORP_LABEL>Description</NM_TORP_LABEL>
    <NM_TANDP_TITLE>2</NM_TANDP_TITLE>
    <NM_TORPCOLUMN_DATA><P>BTJT-B Platform<BR/>Offshore Pipeline 16â€from Platform BTJT-B to Platform BTJT-A<BR/>BTJT-B Platform<BR/>Subsea Riser Flange BTJT-B<BR/>IP-01<BR/>KP.0+000<BR/>KP.0+500<BR/>KP.1+000<BR/>KP.1+500<BR/>KP.2+000<BR/>KP.2+500<BR/>KP.3+000<BR/>KP Target Box / Subsea<BR/>IP-02<BR/>Subsea Flange / Middle Spool<BR/>Subsea Flange / Middle Spool<BR/>Subsea Flange / Middle Spool<BR/>IP-03<BR/>Subsea Flange / Middle Spool<BR/>IP-04<BR/>Subsea Riser Flange BTJT-A<BR/>Existing BTJT-A Platform<BR/>Offshore Pipeline from Platform BTJT-A to Platform BTJT-B<BR/>Existing BTJT-A Platform<BR/>Subsea Riser Flange BTJT-A<BR/>IP-01<BR/>Subsea Flange (Riser Spool)<BR/>IP-02<BR/>Subsea Flange / Middle Spool<BR/>Subsea Flange / Middle Spool<BR/>Subsea Flange / Middle Spool<BR/>IP-02<BR/>Target Box / Subsea<BR/>KP.0+500<BR/>KP.1+000<BR/>KP.1+500<BR/>KP.2+000<BR/>KP.2+500<BR/>KP.3+000<BR/>Target Box / Subsea<BR/>IP-04<BR/>Subsea Riser Flange TJT-B<BR/>BTJT-B Platform</P>
    </NM_TORPCOLUMN_DATA>
</NM_TORP_TABLE_LIST>
<NM_TORP_TABLE_LIST>
    <NM_TORP_LABEL>Latitude</NM_TORP_LABEL>
    <NM_TANDP_TITLE>3</NM_TANDP_TITLE>
    <NM_TORPCOLUMN_DATA>
        <P>06-38-11.314S</P>
        <P>06-38-11.314S<BR/>06-38-10.725S<BR/>06-38-10.505S<BR/>06-38-10.283S<BR/>06-37-56.584S<BR/>06-37-42.885S<BR/>06-37-29.185S<BR/>06-37-15.486S<BR/>06-36-01.787S<BR/>06-36-48.087S<BR/>06-36-45.917S<BR/>06-36-45.790S<BR/>06-36-46.018S<BR/>06-36-46.668S<BR/>06-36-47.318S<BR/>06-36-47.976S<BR/>06-36-47.828S<BR/>06-36-46.499S<BR/>06-36-46.256S<BR/>06-36-45.409S</P>
        <P>06-36-45.409S<BR/>06-36-46.250S<BR/>06-36-46.438S<BR/>06-36-47.794S<BR/>06-36-47.871S<BR/>06-36-47.213S<BR/>06-36-46.558S<BR/>06-36-45.903S<BR/>06-36-45.553S<BR/>06-36-45.656S<BR/>06-36-50.239S<BR/>06-37-03.954S<BR/>06-37-17.670S<BR/>06-37-31.385S<BR/>06-37-45.100S<BR/>06-37-58.815S<BR/>06-38-10.096S<BR/>06-38-10.211S<BR/>06-38-10.818S<BR/>06-38-11.314S</P>
    </NM_TORPCOLUMN_DATA>
</NM_TORP_TABLE_LIST>
<NM_TORP_TABLE_LIST>
    <NM_TORP_LABEL>Longitude</NM_TORP_LABEL>
    <NM_TANDP_TITLE>4</NM_TANDP_TITLE>
    <NM_TORPCOLUMN_DATA>
        <P>113-19-37.766E</P>
        <P>113-19-37.766E<BR/>113-19-38.534E<BR/>113-19-38.609E<BR/>113-19-38.609E<BR/>113-19-29.831E<BR/>113-19-21.053E<BR/>113-19-12.275E<BR/>113-19-3.498E<BR/>113-18-54.721E<BR/>113-18-45.944E<BR/>113-18-45.944E<BR/>113-18-44.479E<BR/>113-18-43.461E<BR/>113-18-43.566E<BR/>113-18-37.67E<BR/>113-18-34.743E<BR/>113-18-34.53E<BR/>113-18-32.699E<BR/>113-18-32.876E<BR/>113-18-33.453E</P>
        <P>113-18-33.453E<BR/>113-18-32.92E<BR/>113-18-32.783E<BR/>113-18-34.65E<BR/>113-18-34.756E<BR/>113-18-37.687E<BR/>113-18-40.608E<BR/>113-18-43.529E<BR/>113-18-45.086E<BR/>113-18-45.152E<BR/>113-18-48.076E<BR/>113-18-56.829E<BR/>113-19-5.581E<BR/>113-19-14.334E<BR/>113-19-23.086E<BR/>113-19-31.839E<BR/>113-19-39.039E<BR/>113-19-39.112E<BR/>113-19-38.511E<BR/>113-19-37.766E</P>
    </NM_TORPCOLUMN_DATA>
</NM_TORP_TABLE_LIST>
</NM_TORP_TABLE>

EXPECTED OUTPUT:

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<table style="font-style:italic; margin-left:100px;" cellpadding="10" cellspacing="0" border="1">
<tbody>
    <tr>
        <th>Nr</th>
        <th>Description</th>
        <th>Latitude</th>
        <th>Longitude</th>
    </tr>
    <tr>
        <td>1</td>
        <td>BTJT-B Platform</td>
        <td> 06-38-11.314S</td>
        <td> 113-19-37.766E</td>
    </tr>
    <tr>
        <td>2</td>
        <td>Offshore Pipeline 16â€from Platform BTJT-B to Platform BTJT-A</td>
        <td> 06-38-11.314S</td>
        <td> 113-19-37.766E</td>
    </tr>
    <tr>
        <td>2.1</td>
        <td>BTJT-B Platform</td>
        <td>06-38-10.725S</td>
        <td>113-19-38.534E</td>
    </tr>
    <tr>
        <td>2.2</td>
        <td>Subsea Riser Flange BTJT-B</td>
        <td>06-38-10.505S</td>
        <td>113-19-38.609E</td>
    </tr>
    ............
</tbody>
</table>

XSLT CODE:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
    <xsl:for-each select="./NM_TORP_TABLE">
        <xsl:variable name="cols" select="NM_TORP_TABLE_LIST" />
        <table style="font-style:italic; margin-left:100px;" cellpadding="10" cellspacing="0" border="1">
            <tbody>
                <tr>
                    <xsl:for-each select="$cols">
                        <th>
                            <xsl:value-of select="NM_TORP_LABEL"/>
                        </th>
                    </xsl:for-each>
                </tr>
                <xsl:for-each select="NM_TORP_TABLE_LIST[1]/NM_TORPCOLUMN_DATA/text()">
                    <xsl:variable name="row" select="position()" />
                    <tr>
                        <xsl:for-each select="$cols">
                            <td>
                                <xsl:value-of select="NM_TORPCOLUMN_DATA/text()[$row]"/>
                                <!--<xsl:value-of select="NM_TORPCOLUMN_DATA/text()[$row] | NM_TORPCOLUMN_DATA/P/text()[$row]"/>-->
                            </td>
                        </xsl:for-each>
                    </tr>
                </xsl:for-each>
            </tbody>
        </table>
    </xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Solution

  • I think you want <xsl:value-of select="NM_TORPCOLUMN_DATA/descendant::text()[normalize-space()][$row]"/>.