Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
809 views
in Technique[技术] by (71.8m points)

xslt - Convert XML text into table

In the example below we are trying to convert XML data into an HTML <table>:

INPUT XML:

<?xml version="1.0" encoding="UTF-8"?>
<NM_TORP_TABLE>
<NM_TORP_TABLE_LIST>
    <NM_TORP_LABEL>Nr.</NM_TORP_LABEL>
    <NM_TORPCOLUMN_DATA>1 <BR/>2 <BR/>3 <BR/>4 <BR/>5 <BR/>6 <BR/>7 <BR/>8 <BR/>9 
        <BR/>10<BR/>11<BR/>12<BR/>13<BR/>14<BR/>15<BR/>16</NM_TORPCOLUMN_DATA>
</NM_TORP_TABLE_LIST>
<NM_TORP_TABLE_LIST>
    <NM_TORP_LABEL>Latitude</NM_TORP_LABEL>
    <NM_TORPCOLUMN_DATA>5° 10’ 6" S<BR/>3° 31’ 8" S<BR/>5° 19’ 7" S<BR/>3° 1’ 2" S <BR/>3° 9’ 6" S 
        <BR/>3° 20’ 9" S<BR/>5° 8’ 3" S <BR/>3° 55’ 9" S<BR/>4° 49’ 3" S<BR/>4° 49’ 8" S<BR/>3° 23’ 9" S<BR/>4° 12’ 3" S<BR/>4° 15’ 3" S<BR/>4° 54’ 0" S<BR/>3° 39’ 9" S<BR/>5° 20’ 3" S</NM_TORPCOLUMN_DATA>
</NM_TORP_TABLE_LIST>
<NM_TORP_TABLE_LIST>
    <NM_TORP_LABEL>Longitude</NM_TORP_LABEL>
    <NM_TORPCOLUMN_DATA>107° 38’ 5" E<BR/>110° 4’ 4" E<BR/>109° 5’ 8" E<BR/>109° 50’ 2" E<BR/>109° 47’ 4" E<BR/>108° 46’ 9" E<BR/>109° 52’ 4" E<BR/>107° 47’ 6" E<BR/>107° 42’ 3" E<BR/>107° 42’ 2" E<BR/>111° 35’ 24" E<BR/>111° 32’ 1" E<BR/>110° 43’ 5" E<BR/>110° 46’ 2" E<BR/>108° 32’ 9" E<BR/>109° 11’ 3" E</NM_TORPCOLUMN_DATA>
</NM_TORP_TABLE_LIST>
</NM_TORP_TABLE>

EXPECTED OUTPUT:

<table>
<tr>
    <td>Nr.</td>
    <td>Latitude</td>
    <td>Longitude</td>
</tr>
<tr>
    <td>1</td>
    <td>5° 10’ 6" S</td>
    <td>107° 38’ 5" E</td>
</tr>
<tr>
    <td>2</td>
    <td>3° 31’ 8" S</td>
    <td>110° 4’ 4" E</td>
</tr>
<tr>
    <td>3</td>
    <td>5° 19’ 7" S</td>
    <td>109° 5’ 8" E</td>
</tr>
.......    
</table>

XSLT CODE:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="1.0">
<xsl:output method="html"/>
<xsl:template match="/">
    <table>
        <tbody>
            <xsl:for-each select="NM_TORP_TABLE/NM_TORP_TABLE_LIST">
                <tr>
                    <td><xsl:value-of select="NM_TORP_LABEL"/></td>
                    <td><xsl:value-of select="NM_TORPCOLUMN_DATA"/></td>
                </tr>
            </xsl:for-each>
        </tbody>
    </table>
</xsl:template>
</xsl:stylesheet>

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

question from:https://stackoverflow.com/questions/65889989/convert-xml-text-into-table

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your attempt completely disregards the (suboptimal) structure of your input XML. You need to create a row for each text node within the first NM_TORPCOLUMN_DATA element, and populate its cells from the corresponding text nodes in all the NM_TORPCOLUMN_DATA elements.

This is of course assuming that all NM_TORPCOLUMN_DATA elements have exactly the same number of text nodes - otherwise I don't see how this task is possible at all.

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/NM_TORP_TABLE">
    <xsl:variable name="cols" select="NM_TORP_TABLE_LIST" />
    <table border="1">
        <!-- header -->
        <tr>
            <xsl:for-each select="$cols">
                <th>
                    <xsl:value-of select="NM_TORP_LABEL"/>
                </th>
            </xsl:for-each>
        </tr>
        <!-- data -->
        <xsl:for-each select="$cols[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]"/>
                    </td>
                </xsl:for-each>
            </tr>
        </xsl:for-each> 
    </table>
</xsl:template>

</xsl:stylesheet>

Applied to your input example, this will produce:

Result

<?xml version="1.0" encoding="UTF-8"?>
<table>
  <tr>
    <th>Nr.</th>
    <th>Latitude</th>
    <th>Longitude</th>
  </tr>
  <tr>
    <td>1 </td>
    <td>5° 10’ 6" S</td>
    <td>107° 38’ 5" E</td>
  </tr>
  <tr>
    <td>2 </td>
    <td>3° 31’ 8" S</td>
    <td>110° 4’ 4" E</td>
  </tr>
  <tr>
    <td>3 </td>
    <td>5° 19’ 7" S</td>
    <td>109° 5’ 8" E</td>
  </tr>
  <tr>
    <td>4 </td>
    <td>3° 1’ 2" S </td>
    <td>109° 50’ 2" E</td>
  </tr>
  <tr>
    <td>5 </td>
    <td>3° 9’ 6" S 
        </td>
    <td>109° 47’ 4" E</td>
  </tr>
  <tr>
    <td>6 </td>
    <td>3° 20’ 9" S</td>
    <td>108° 46’ 9" E</td>
  </tr>
  <tr>
    <td>7 </td>
    <td>5° 8’ 3" S </td>
    <td>109° 52’ 4" E</td>
  </tr>
  <tr>
    <td>8 </td>
    <td>3° 55’ 9" S</td>
    <td>107° 47’ 6" E</td>
  </tr>
  <tr>
    <td>9 
        </td>
    <td>4° 49’ 3" S</td>
    <td>107° 42’ 3" E</td>
  </tr>
  <tr>
    <td>10</td>
    <td>4° 49’ 8" S</td>
    <td>107° 42’ 2" E</td>
  </tr>
  <tr>
    <td>11</td>
    <td>3° 23’ 9" S</td>
    <td>111° 35’ 24" E</td>
  </tr>
  <tr>
    <td>12</td>
    <td>4° 12’ 3" S</td>
    <td>111° 32’ 1" E</td>
  </tr>
  <tr>
    <td>13</td>
    <td>4° 15’ 3" S</td>
    <td>110° 43’ 5" E</td>
  </tr>
  <tr>
    <td>14</td>
    <td>4° 54’ 0" S</td>
    <td>110° 46’ 2" E</td>
  </tr>
  <tr>
    <td>15</td>
    <td>3° 39’ 9" S</td>
    <td>108° 32’ 9" E</td>
  </tr>
  <tr>
    <td>16</td>
    <td>5° 20’ 3" S</td>
    <td>109° 11’ 3" E</td>
  </tr>
</table>

Rendered

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...