XML与XSL表格匹配故障:转换HTML时无法填充数据
问题:XSL转换XML时表格无法填充数据
尝试用XSL将XML数据填充到HTML表格,但转换后表格无数据,怀疑是缺少表格ID调用参数,相关代码如下:
XSL代码
<div class="table"> <table border="1"> <h3>NLS Alert details</h3> <tr> <th>NLS "Record ID"</th> <th>Name</th> <th>NLS Alert Description</th> <th>AML DD Comments</th> <th>AFC Comments</th> </tr> <xsl:for-each select="NewDataSet/_x0039_c07da72-63fc-45fe-9ab7-4c787b1e6984"> <xsl:if test="normalize-space(AMLNLSRecordID) or normalize-space(AMLNLSAlertName) or normalize-space(AMLNLSAlertDescription) or normalize-space(AMLDDAFCComments) or normalize-space(AMLAFCComments)"> <tr> <td> <xsl:value-of select="AMLNLSRecordID"/> </td> <td> <xsl:value-of select="AMLNLSAlertName"/> </td> <td> <xsl:value-of select="AMLNLSAlertDescription"/> </td> <td> <xsl:value-of select="AMLDDAFCComments"/> </td> <td> <xsl:value-of select="AMLAFCComments"/> </td> </tr> </xsl:if> </xsl:for-each> </table> </div> </div>
XML代码
<Table TableId="NLSAlertDetailsTable"> <TableRow RowNumber="1"> <AMLNLSRecordID>1</AMLNLSRecordID> <AMLNLSAlertName>Name 1 Surname 1</AMLNLSAlertName> <AMLNLSAlertDescription>1</AMLNLSAlertDescription> <AMLDDAFCComments>1</AMLDDAFCComments> <AMLAFCComments>Test</AMLAFCComments> </TableRow> <TableRow RowNumber="2"> <AMLNLSRecordID>2</AMLNLSRecordID> <AMLNLSAlertName>Name 2 Surname 2</AMLNLSAlertName> <AMLNLSAlertDescription>2</AMLNLSAlertDescription> <AMLDDAFCComments>2</AMLDDAFCComments> <AMLAFCComments>Test 1</AMLAFCComments> </TableRow> <TableRow RowNumber="3"> <AMLNLSRecordID>3</AMLNLSRecordID> <AMLNLSAlertName>Name 3 Surname 3</AMLNLSAlertName> <AMLNLSAlertDescription>3</AMLNLSAlertDescription> <AMLDDAFCComments>3</AMLDDAFCComments> <AMLAFCComments>Test 2</AMLAFCComments> </TableRow> <TableRow RowNumber="4"> <AMLNLSRecordID>4</AMLNLSRecordID> <AMLNLSAlertName>Name 4 Surname 4</AMLNLSAlertName> <AMLNLSAlertDescription>4</AMLNLSAlertDescription> <AMLDDAFCComments>4</AMLDDAFCComments> <AMLAFCComments>Test 3</AMLAFCComments> </TableRow> </Table>
问题排查与解决方案
核心原因
XSL中的节点选择路径与XML实际结构完全不匹配:
- XSL使用
NewDataSet/_x0039_c07da72-63fc-45fe-9ab7-4c787b1e6984作为循环目标,但XML的根节点是<Table>,数据节点是<TableRow>,两者没有对应关系。 - 表格ID
NLSAlertDetailsTable不影响数据选择,XSL是基于XML节点层级结构定位数据,而非标签属性。
修正步骤
- 调整节点选择路径:将
xsl:for-each的select值改为匹配XML结构的Table/TableRow(若XML存在上层根节点,比如<NewDataSet>,则改为NewDataSet/Table/TableRow)。 - 修复多余标签:原XSL末尾多了一个
</div>,需删除以保证结构合法。 - 优化HTML结构:将
<h3>移出<table>标签内部(不符合HTML规范)。
修改后的完整XSL代码
<div class="table"> <h3>NLS Alert details</h3> <table border="1"> <tr> <th>NLS "Record ID"</th> <th>Name</th> <th>NLS Alert Description</th> <th>AML DD Comments</th> <th>AFC Comments</th> </tr> <xsl:for-each select="Table/TableRow"> <xsl:if test="normalize-space(AMLNLSRecordID) or normalize-space(AMLNLSAlertName) or normalize-space(AMLNLSAlertDescription) or normalize-space(AMLDDAFCComments) or normalize-space(AMLAFCComments)"> <tr> <td><xsl:value-of select="AMLNLSRecordID"/></td> <td><xsl:value-of select="AMLNLSAlertName"/></td> <td><xsl:value-of select="AMLNLSAlertDescription"/></td> <td><xsl:value-of select="AMLDDAFCComments"/></td> <td><xsl:value-of select="AMLAFCComments"/></td> </tr> </xsl:if> </xsl:for-each> </table> </div>
内容的提问来源于stack exchange,提问作者DataCrunch
相关产品推荐
相关产品推荐

