如何将 XML 文本转换为表格格式 [英] How to transform XML text into table format

查看:27
本文介绍了如何将 XML 文本转换为表格格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的示例中,我们试图将 XML 数据转换为 HTML,但在这里我们面临着挑战,因为诸如 '<NM_TORPCOLUMN_DATA>' 之类的元素不一致&'<NM_TORPCOLUMN_DATA><P>':

注意#如果我们要删除<P>"'<NM_TORPCOLUMN_DATA>' 中的元素然后 xslt 代码工作正常.

参考网址:

<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/>BR/>3.7<BR/>3.8<BR/>3.9<BR/>3.10<BR/>3.11<BR/>3.12<BR/>3.13<BR/>3.14
3.15
3.16
3.17
3.18
3.19
3.20;</NM_TORP_TABLE_LIST><NM_TORP_TABLE_LIST><NM_TORP_LABEL>说明</NM_TORP_LABEL><NM_TANDP_TITLE>2</NM_TANDP_TITLE><NM_TORPCOLUMN_DATA><P>BTJT-B平台<BR/>海上管道16'从平台BTJT-B到平台BTJT-A<BR/>BTJT-B平台<BR/>海底立管法兰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目标盒/海底<BR/>IP-02<BR/>海底法兰/中间阀芯<BR/>海底法兰/中间阀芯<BR/>海底法兰/中间阀芯<BR/>IP-03<BR/>海底法兰/中间阀芯<BR/>IP-04<BR/>海底立管法兰BTJT-A<BR/>现有BTJT-A平台<BR/>从BTJT-A平台到BTJT-B平台<BR/>现有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/>;BR/>海底法兰/中间阀芯
海底法兰/中间阀芯
IP-02
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/>目标箱/海底<BR/>IP-04<BR/>海底立管法兰TJT-B<BR/>BTJT-B平台

</NM_TORPCOLUMN_DATA></NM_TORP_TABLE_LIST><NM_TORP_TABLE_LIST><NM_TORP_LABEL>纬度</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
06-37-42.885S
06-37-29.185S
06-37-15.486S
06-36-01.787S/>06-36-48.087S<BR/>06-36-45.917S<BR/>06-36-45.790S<BR/>06-36-46.018S<BR/>06-36-46.668S
06-36-47.318S
06-36-47.976S
06-36-47.828S
06-36-46.499S
06-36-47.213S
06-36-46.558S
06-36-45.903S
06-36-45.553S
/>06-36-45.656S<BR/>06-36-50.239S<BR/>06-37-03.954S<BR/>06-37-17.670S<BR/>06-37-31.385S
06-37-45.100S
06-37-58.815S
06-38-10.096S
06-38-10.211S
113-19-21.053E
113-19-12.275E
113-19-3.498E
113-18-54.053E
/>113-18-45.944E<BR/>113-18-45.944E<BR/>113-18-44.479E<BR/>113-18-43.461E<BR/>113-1-43.566E
113-18-37.67E
113-18-34.743E
113-18-34.53E
113-18-32.699E<>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
113-18-37.687E
113-18-40.608E
113-18-43.529E
113-18-45.08/>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.1BR/>>113-19-38.511E<BR/>113-19-37.766E​​</P></NM_TORPCOLUMN_DATA></NM_TORP_TABLE_LIST></NM_TORP_TABLE>

预期输出:

<table style="font-style:italic;margin-left:100px;"单元格填充=10"单元格间距=0"边界=1"><tr><th>Nr</th><第>描述<th>纬度</th><th>经度</th></tr><tr><td>1</td><td>BTJT-B平台</td><td>06-38-11.314S</td><td>113-19-37.766E​​</td></tr><tr><td>2</td><td>海上管道16>从平台BTJT-B到平台BTJT-A</td><td>06-38-11.314S</td><td>113-19-37.766E​​</td></tr><tr><td>2.1</td><td>BTJT-B平台</td><td>06-38-10.725S</td><td>113-19-38.534E</td></tr><tr><td>2.2</td><td>海底立管法兰BTJT-B</td><td>06-38-10.505S</td><td>113-19-38.609E</td></tr>………………</tbody>

XSLT 代码:

<xsl:stylesheet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";xmlns:xsl=http://www.w3.org/1999/XSL/Transform"版本=1.0"><xsl:template match="/"><xsl:for-each select="./NM_TORP_TABLE"><xsl:variable name="cols";选择=NM_TORP_TABLE_LIST";/><table style="font-style:italic;margin-left:100px;"单元格填充=10"单元格间距=0"边界=1"><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";选择=位置()";/><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></xsl:for-each></xsl:模板></xsl:stylesheet>

解决方案

我想你想要 <xsl:value-of select="NM_TORPCOLUMN_DATA/descendant::text()[normalize-space()][$row]"/>.

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:

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

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

解决方案

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

这篇关于如何将 XML 文本转换为表格格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆