首页 > 解决方案 > XML , XSL 解析 - 无法显示表中的所有行

问题描述

我正在尝试从 XML 文档中生成 PDF。请在下面找到我的 XML 和 XSL。

我期望它应该显示标签下的所有行,但我只得到每个标签中的第一个元素(行)。

请在下面找到我的 xml

<receipt>
<order>
    <page></page>
    <page>
        <line_number>1</line_number>
        <product_code>S10</product_code>
        <line_number>2</line_number>
        <product_code>S20</product_code>
        <line_number>3</line_number>
        <product_code>S92</product_code>
    </page>
    <page>
        <line_number>6</line_number>
        <product_code>S92</product_code>
        <line_number>7</line_number>
        <product_code>S31</product_code>
        <line_number>8</line_number>
        <product_code>S31</product_code>
    </page>
</order>
</receipt>

请找到我的xsl

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml"   
                              xmlns:date="http://exslt.org/dates-and-times" extension-element-prefixes="date">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

    <xsl:template match="/receipt">
        <html>
            <head>
            <style>@page {size: a4 landscape;}</style>
            </head>
            <body>

                <table >
                    <thead>
                        <tr >
                            <th >Line</th>
                            <th>Item Code</th>
                        </tr>
                    </thead>
                <xsl:for-each select="order/page" >
                                    <tbody>
                                        <tr style="font-size: 9px; ">
                                            <td ><xsl:value-of select="line_number" /></td>
                                            <td ><xsl:value-of select="product_code" /></td>
                                        </tr>
                                    </tbody>
                </xsl:for-each>
                </table>
                <br />

            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

在输出中,只有标签中的第一个元素出现,而不是每个标签下的所有元素(行)。

例如 :

输出 :

1     s10
6     s92

预期产出

1     s10
2     s20
3     s92
6     s92
7     s31
8     s31  

标签: htmlxmlxslt

解决方案


您希望每输出一行line_number,而不是每行page,因此您xsl:for-each需要选择这些line_number元素

<xsl:for-each select="order/page/line_number">

然后要获得 theline_number和 following的值product_code,请执行此操作...

<td><xsl:value-of select="." /></td>
<td><xsl:value-of select="following-sibling::product_code[1]" /></td>

尝试这个...

<xsl:template match="/receipt">
    <html>
        <head>
        <style>@page {size: a4 landscape;}</style>
        </head>
        <body>
            <table >
                <thead>
                    <tr>
                        <th>Line</th>
                        <th>Item Code</th>
                    </tr>
                </thead>
                <tbody>
                    <xsl:for-each select="order/page/line_number">
                        <tr style="font-size: 9px; ">
                            <td><xsl:value-of select="." /></td>
                            <td><xsl:value-of select="following-sibling::*[1][self::product_code]" /></td>
                        </tr>
                    </xsl:for-each>
                </tbody>
            </table>
            <br />
        </body>
    </html>
</xsl:template>

请注意,这确实假设每个line_number都将跟随一个product_code.

(我还将tbody元素的创建移到了外部,xsl:for-each因为您的表中实际上应该只有一个这样的元素,而不是每一行一个)


推荐阅读