html - 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
解决方案
您希望每输出一行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
因为您的表中实际上应该只有一个这样的元素,而不是每一行一个)
推荐阅读
- firebase - Firebase 实时数据库在尝试进行身份验证时返回 401?
- ios - Apple 登录无法进行测试飞行
- javascript - 将随机输入值转换为数字以进行计数器计算
- php - 选择表单不从下拉列表中发送选定的值
- python - 在fuzzywuzzy和所有最大相似选择中使用Process.extract
- python - 如何在 CNN 上进行无监督学习
- mysql - Ruby on rails 数据获取问题
- php - PHP 数组循环帮助 - WordPress ACF
- haskell - 将类型类及其实例拆分为 Haskell 中的不同子模块
- python - 来自 Popen 的流式输出