xml - 无法读取 xsl 中的值
问题描述
<results>
<metadata>
<column name="title" type="S" length="4000" nullable="true" />
<column name="schbat" type="S" length="32" nullable="true" />
<column name="pck_cnt" type="I" length="4" nullable="true" />
</metadata>
<data>
<row>
<field>Wave</field>
<field>1209180044-</field>
<field>4</field>
</row>
<row>
<field>Wave</field>
<field>1209180045-</field>
<field>1</field>
</row>
<row>
</data>
</results>
- xslt
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:key name="kString" match="field" use="."/>
<xsl:template match="/">
<html>
<body style="text-align: center;">
<table width="450" cellpadding="5" cellspacing="0" style="border: 1px solid #CCCCCC" >
<thead>
<tr>
<th colspan="5" bgcolor="#FFFFFF" style="font-family: 'Arial', sans-serif; color: #ba1e29; border-bottom: 2px solid #ffffff; border-right: 2px solid #ffffff;"><span style="font-family: 'Oswald', sans-serif; text-align: center; font-weight: 500; color: #3f3f3f; line-height: 1; margin-bottom: 1rem; text-transform: uppercase;"><strong>Results For Processing on <span style="color: #ba1e29">5-4-2020</span></strong></span></th>
</tr>
<xsl:variable name="set" select="moca-results/metadata/column" />
<xsl:variable name="count" select="count($set)" />
<tr>
<xsl:for-each select="$set">
<th width="20%" style="font-size:13px;font-family: 'Arial', sans-serif;background-color: #f0f0fa; color: #ba1e29; border-bottom: 2px solid #ffffff; border-right: 2px solid #ffffff;"><xsl:value-of select="@name" /></th>
</xsl:for-each>
</tr>
</thead>
<tbody>
<xsl:variable name="bodyset" select="results/data/row" />
<xsl:variable name="bodycount" select="count($bodyset)" />
<xsl:variable name="set" select="results/metadata/column" />
<xsl:variable name="count" select="count($set)" />
<xsl:for-each select="$bodyset">
<tr>
<xsl:for-each select="$set">
<td align="center" style="font-family: 'Arial', sans-serif;background-color: #f0f0fa; color: #414143; border-right: 2px solid #ffffff;">
<xsl:value-of select="filed" />
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</tbody>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
*输出
标题 | 施巴特 | pck_cnt
波 | 1209180044- | 4
波 | 1209180045- | 1
根据列数,我将在行中归档(意味着如果元数据中的列数为 3,那么我将在行元素中得到 3 个归档。但我无法读取那些归档的值(每次当我在时,我总是结果得到波浪)
解决方案
我有一种感觉,你把这件事复杂化了。考虑以下示例:
XML
<results>
<metadata>
<column name="title" type="S" length="4000" nullable="true" />
<column name="schbat" type="S" length="32" nullable="true" />
<column name="pck_cnt" type="I" length="4" nullable="true" />
</metadata>
<data>
<row>
<field>Wave</field>
<field>1209180044-</field>
<field>4</field>
</row>
<row>
<field>Wave</field>
<field>1209180045-</field>
<field>1</field>
</row>
</data>
</results>
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/results">
<html>
<body>
<table border="1">
<thead>
<tr>
<xsl:for-each select="metadata/column">
<th>
<xsl:value-of select="@name" />
</th>
</xsl:for-each>
</tr>
</thead>
<tbody>
<xsl:for-each select="data/row">
<tr>
<xsl:for-each select="field">
<td>
<xsl:value-of select="." />
</td>
</xsl:for-each>
</tr>
</xsl:for-each>
</tbody>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
结果
<html>
<body>
<table>
<thead>
<tr>
<th>title</th>
<th>schbat</th>
<th>pck_cnt</th>
</tr>
</thead>
<tbody>
<tr>
<td>Wave</td>
<td>1209180044-</td>
<td>4</td>
</tr>
<tr>
<td>Wave</td>
<td>1209180045-</td>
<td>1</td>
</tr>
</tbody>
</table>
</body>
</html>
渲染:
推荐阅读
- reactjs - 将 useState 挂钩存储在一个公共对象中不起作用
- mysql - JSON_EXTRACT 在两个相似环境中的问题
- python - 将 CPLEX 12.10 安装到 Python 3.8 时出错
- angular - 为什么迁移到 Angular 6 后 Angular Lazy 加载和路由不起作用?
- pine-script - 策略和指标放置
- jquery - 数据表操作列无法正常工作
- html - 位置粘性在 IOS 设备或 iPhone 设备上不起作用
- python - 如何使用 begin() 函数的返回值作为 eind() 函数的参数?
- javascript - 在测试套件中运行时,云函数中事务中的数据始终为空
- three.js - 如何将 shadertoy 着色器(带缓冲区)转换为在 three.js 中使用?