0

我有下面的 XML 文件,我正在尝试为其创建一个 XSLT 模板来将一些节点制成表格。请有人可以帮助我。

<Main>
    <Document>Doc.1</Document>
    <Cini>DDFR</Cini>
    <List>
        <SubList>
            <CdTa>ABC</CdTa>
            <NN>XYZ</NN>
            <ND>
                <RiS>
                    <RiN>
                        <NSE14>
                            <MNRs>
                                <MRD>
                                    <NR>
                                        <N1>393</N1>
                                        <N2>720</N2>
                                        <SNR>
                                            <NR_i>203</NR_i>
                                            <NR_f>49994</NR_f>
                                        </SNR>
                                    </NR>
                                </MRD>
                                <MRD>
                                    <NR>
                                        <N1>687</N1>
                                        <N2>345</N2>
                                        <SNR>
                                            <NR_i>55005</NR_i>
                                            <NR_f>1229996</NR_f>
                                        </SNR>
                                    </NR>
                                </MRD>
                            </MNRs>
                            <GNRs>
                                <RD>
                                    <NR>
                                        <N1>649</N1>
                                        <N2>111</N2>
                                        <SNR>
                                            <NR_i>55400</NR_i>
                                            <NR_f>877</NR_f>
                                        </SNR>
                                    </NR>
                                </RD>
                            </GNRs>
                            <MSNRs>
                                <NR>
                                    <N1>748</N1>
                                    <N2>5624</N2>
                                    <SNR>
                                        <NR_i>8746</NR_i>
                                        <NR_f>7773</NR_f>
                                    </SNR>
                                </NR>
                                <NR>
                                    <N1>124</N1>
                                    <N2>54</N2>
                                    <SNR>
                                        <NR_i>8847</NR_i>
                                        <NR_f>5526</NR_f>
                                    </SNR>
                                </NR>
                            </MSNRs>
                        </NSE14>
                        <NSE12>
                            <MBB>990</MBB>
                            <MRB>123</MRB>
                        </NSE12>
                        <MGE13>
                            <TBB>849</TBB>
                            <TRB>113</TRB>
                        </MGE13>
                    </RiN>
                </RiS>
            </ND>
        </SubList>
    </List>
</Main>

这个 XSLT 代码

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html> 
<body>
  <table border="1">
<tr bgcolor="#FFF833">
      <th style="text-align:left">MNRs</th>
      <th style="text-align:left">GNRs</th>
      <th style="text-align:left">MSNRs</th>
</tr>
    <tr bgcolor="#9acd32">
      <th style="text-align:left">N1</th>
      <th style="text-align:left">N2</th>
      <th style="text-align:left">NR_i</th>
      <th style="text-align:left">NR_f</th>
      <th style="text-align:left">N1</th>
      <th style="text-align:left">N2</th>
      <th style="text-align:left">NR_i</th>
      <th style="text-align:left">NR_f</th>
      <th style="text-align:left">N1</th>
      <th style="text-align:left">N2</th>
      <th style="text-align:left">NR_i</th>
      <th style="text-align:left">NR_f</th>
    </tr>

    <xsl:for-each select="//MNRs//NR">
    <tr>
      <td><xsl:value-of select="N1"/></td>
      <td><xsl:value-of select="N2"/></td>
      <xsl:for-each select="SNR">
          <td><xsl:value-of select="NR_i"/></td>
          <td><xsl:value-of select="NR_f"/></td>
      </xsl:for-each>
    </xsl:for-each>

    <xsl:for-each select="//GNRs//NR">
      <td><xsl:value-of select="N1"/></td>
      <td><xsl:value-of select="N2"/></td>
      <xsl:for-each select="SNR">
          <td><xsl:value-of select="NR_i"/></td>
          <td><xsl:value-of select="NR_f"/></td>
      </xsl:for-each>
    </xsl:for-each>

    <xsl:for-each select="//MSNRs//NR">
      <td><xsl:value-of select="N1"/></td>
      <td><xsl:value-of select="N2"/></td>
      <xsl:for-each select="SNR">
          <td><xsl:value-of select="NR_i"/></td>
          <td><xsl:value-of select="NR_f"/></td>
      </xsl:for-each>
    </tr>
    </xsl:for-each>
  </table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

使用当前的 XSLT 代码,我得到的输出不是我想要获得的结构,因为我只得到了一些值。问题是有 3 个父节点具有相同的子节点名称。父节点是 MNRs、GNRs 和 MSNRs

+------+------+-------+---------+----+----+------+------+----+----+------+------+
| MNRs | GNRs | MSNRs |         |    |    |      |      |    |    |      |      |
+------+------+-------+---------+----+----+------+------+----+----+------+------+
| N1   | N2   | NR_i  | NR_f    | N1 | N2 | NR_i | NR_f | N1 | N2 | NR_i | NR_f |
+------+------+-------+---------+----+----+------+------+----+----+------+------+
| 393  | 720  | 203   | 49994   |    |    |      |      |    |    |      |      |
+------+------+-------+---------+----+----+------+------+----+----+------+------+
| 687  | 345  | 55005 | 1229996 |    |    |      |      |    |    |      |      |
+------+------+-------+---------+----+----+------+------+----+----+------+------+

我想得到这个输出:

+------+------+-------+---------+-----+-----+-------+------+-----+------+------+------+
|             MNRs              |          GNRs            |           MSNRs          |
+------+------+-------+---------+-----+-----+-------+------+-----+------+------+------+
| N1   | N2   | NR_i  | NR_f    | N1  | N2  | NR_i  | NR_f | N1  | N2   | NR_i | NR_f |
+------+------+-------+---------+-----+-----+-------+------+-----+------+------+------+
| 393  | 720  | 203   | 49994   | 649 | 111 | 55400 | 877  | 748 | 5624 | 8746 | 7773 |
+------+------+-------+---------+-----+-----+-------+------+-----+------+------+------+
| 687  | 345  | 55005 | 1229996 |     |     |       |      | 124 | 54   | 8847 | 5526 |
+------+------+-------+---------+-----+-----+-------+------+-----+------+------+------+ 

谢谢你的帮助。

4

2 回答 2

1

这一点都不是微不足道的,尤其是。在 XSLT 1.0 中。我建议你这样尝试:

XSL 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
    <html> 
        <body>
            <table border="1" width="80%">
                <!-- header -->
                <tr>
                    <th colspan="4">MNRs</th>
                    <th colspan="4">GNRs</th>
                    <th colspan="4">MSNRs</th>
                </tr>
                <tr>
                    <!-- MNRs -->
                    <th>N1</th>
                    <th>N2</th>
                    <th>NR_i</th>
                    <th>NR_f</th>
                    <!-- GNRs -->
                    <th>N1</th>
                    <th>N2</th>
                    <th>NR_i</th>
                    <th>NR_f</th>
                    <!-- MSNRs -->
                    <th>N1</th>
                    <th>N2</th>
                    <th>NR_i</th>
                    <th>NR_f</th>
                </tr>
                <!-- data -->
                <xsl:call-template name="rows">
                    <xsl:with-param name="MNRs" select="//MNRs//NR"/>
                    <xsl:with-param name="GNRs" select="//GNRs//NR"/>
                    <xsl:with-param name="MSNRs" select="//MSNRs//NR"/>
                </xsl:call-template>
            </table>
        </body>
    </html>     
</xsl:template>

<xsl:template name="rows">
    <xsl:param name="MNRs"/>
    <xsl:param name="GNRs"/>
    <xsl:param name="MSNRs"/>
    <xsl:param name="i" select="1"/>
    <xsl:if test="$MNRs[$i] or $GNRs[$i] or $MSNRs[$i]">
        <tr>
            <!-- MNRs -->
            <td>
                <xsl:value-of select="$MNRs[$i]/N1"/>
            </td>
            <td>
                <xsl:value-of select="$MNRs[$i]/N2"/>
            </td>
            <td>
                <xsl:value-of select="$MNRs[$i]/SNR/NR_i"/>
            </td>
            <td>
                <xsl:value-of select="$MNRs[$i]/SNR/NR_f"/>
            </td>
            <!-- GNRs -->
            <td>
                <xsl:value-of select="$GNRs[$i]/N1"/>
            </td>
            <td>
                <xsl:value-of select="$GNRs[$i]/N2"/>
            </td>
            <td>
                <xsl:value-of select="$GNRs[$i]/SNR/NR_i"/>
            </td>
            <td>
                <xsl:value-of select="$GNRs[$i]/SNR/NR_f"/>
            </td>
            <!-- MSNRs -->
            <td>
                <xsl:value-of select="$MSNRs[$i]/N1"/>
            </td>
            <td>
                <xsl:value-of select="$MSNRs[$i]/N2"/>
            </td>
            <td>
                <xsl:value-of select="$MSNRs[$i]/SNR/NR_i"/>
            </td>
            <td>
                <xsl:value-of select="$MSNRs[$i]/SNR/NR_f"/>
            </td>
        </tr>
        <!-- recursive call -->
        <xsl:call-template name="rows">     
            <xsl:with-param name="MNRs" select="$MNRs"/>
            <xsl:with-param name="GNRs" select="$GNRs"/>
            <xsl:with-param name="MSNRs" select="$MSNRs"/>
            <xsl:with-param name="i" select="$i + 1"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>

</xsl:stylesheet>

当应用于您的示例输入时,结果将是:

结果

<html>
<body>
<table border="1" width="80%">
<tr>
<th colspan="4">MNRs</th>
<th colspan="4">GNRs</th>
<th colspan="4">MSNRs</th>
</tr>
<tr>
<th>N1</th>
<th>N2</th>
<th>NR_i</th>
<th>NR_f</th>
<th>N1</th>
<th>N2</th>
<th>NR_i</th>
<th>NR_f</th>
<th>N1</th>
<th>N2</th>
<th>NR_i</th>
<th>NR_f</th>
</tr>
<tr>
<td>393</td>
<td>720</td>
<td>203</td>
<td>49994</td>
<td>649</td>
<td>111</td>
<td>55400</td>
<td>877</td>
<td>748</td>
<td>5624</td>
<td>8746</td>
<td>7773</td>
</tr>
<tr>
<td>687</td>
<td>345</td>
<td>55005</td>
<td>1229996</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td>124</td>
<td>54</td>
<td>8847</td>
<td>5526</td>
</tr>
</table>
</body>
</html>

呈现为:

在此处输入图像描述

于 2019-06-05T07:29:36.933 回答
1

另一种选择是生成 3 个单独的表格并并排显示它们。这要简单得多 - 但结果在视觉上有所不同,并且差异取决于所使用的浏览器(我认为这可以通过更多 CSS 来解决):

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
    <html> 
        <body>
            <xsl:for-each select="//MNRs | //GNRs | //MSNRs">
                <table  border="1" width="25%" style="display:inline-block">
                    <!-- header -->
                    <tr>
                        <th colspan="4">
                            <xsl:value-of select="name()"/>
                        </th>
                    </tr>
                    <tr>
                        <th>N1</th>
                        <th>N2</th>
                        <th>NR_i</th>
                        <th>NR_f</th>
                    </tr>
                    <!-- data -->
                    <xsl:for-each select=".//NR">
                        <tr>
                            <td>
                                <xsl:value-of select="N1"/>
                            </td>
                            <td>
                                <xsl:value-of select="N2"/>
                            </td>
                            <td>
                                <xsl:value-of select="SNR/NR_i"/>
                            </td>
                            <td>
                                <xsl:value-of select="SNR/NR_f"/>
                            </td>
                        </tr>
                    </xsl:for-each>
                </table>
            </xsl:for-each>
        </body>
    </html>     
</xsl:template>

结果

<html>
<body>
<table border="1" width="25%" style="display:inline-block">
<tr><th colspan="4">MNRs</th></tr>
<tr>
<th>N1</th>
<th>N2</th>
<th>NR_i</th>
<th>NR_f</th>
</tr>
<tr>
<td>393</td>
<td>720</td>
<td>203</td>
<td>49994</td>
</tr>
<tr>
<td>687</td>
<td>345</td>
<td>55005</td>
<td>1229996</td>
</tr>
</table>
<table border="1" width="25%" style="display:inline-block">
<tr><th colspan="4">GNRs</th></tr>
<tr>
<th>N1</th>
<th>N2</th>
<th>NR_i</th>
<th>NR_f</th>
</tr>
<tr>
<td>649</td>
<td>111</td>
<td>55400</td>
<td>877</td>
</tr>
</table>
<table border="1" width="25%" style="display:inline-block">
<tr><th colspan="4">MSNRs</th></tr>
<tr>
<th>N1</th>
<th>N2</th>
<th>NR_i</th>
<th>NR_f</th>
</tr>
<tr>
<td>748</td>
<td>5624</td>
<td>8746</td>
<td>7773</td>
</tr>
<tr>
<td>124</td>
<td>54</td>
<td>8847</td>
<td>5526</td>
</tr>
</table>
</body>
</html>

在 Safari 中渲染:

在此处输入图像描述

在 Firefox 中渲染:

在此处输入图像描述

于 2019-06-05T07:55:54.510 回答