【发布时间】:2020-11-04 18:16:20
【问题描述】:
我是 xslt 的新手,在开发将 XML 文件转换为 html 表的 XSL 文件时遇到问题。我们能以某种方式控制<td> 的关闭吗?例如,如果size-for-desktop=8 colspan 是8,则关闭td 并且对于下一个aga-block,如果size-for-desktop=4 仅在总和达到</td> 时控制关闭</td>@(size-for-desktop=4 + size -for-desktop=4) 和下一个aga-block,如果size-for-desktop=2 控制关闭</td> 仅当总和达到8(size-for-desktop=2 + size-for-desktop=6)
XML 文件:
<aga-root>
<aga-section type="default">
<aga-block size-for-desktop="8" >
<aga-text>1</aga-text>
</aga-block>
<aga-block size-for-desktop="4" >
<aga-text>2</aga-text>
</aga-block>
<aga-block size-for-desktop="4" >
<aga-text>3</aga-text>
</aga-block>
<aga-block size-for-desktop="2" >
<aga-text>4</aga-text>
</aga-block>
<aga-block size-for-desktop="6" >
<aga-text>5</aga-text>
</aga-block>
<aga-block size-for-desktop="8" >
<aga-text>6</aga-text>
</aga-block>
</aga-section>
</aga-root>
XSL 文件:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.1">
<xsl:output method="html" encoding="UTF-8"/>
<xsl:param name="AccessKey" />
<xsl:template match="/">
<html>
<head>
<meta content="text/html charset=utf-8" http-equiv="Content-Type" />
<meta content="width=device-width, initial-scale=1.0" name="viewport" />
<title>Test</title>
</head>
<body>
<table width="100%" cellpadding="0" cellspacing="0" border="0" bgcolor="#ffffff">
<tbody>
<xsl:for-each select="//aga-section">
<xsl:call-template name="ExploreAgaSection">
<xsl:with-param name="Node" select="." />
</xsl:call-template>
</xsl:for-each>
</tbody>
</table>
</body>
</html>
</xsl:template>
<xsl:template name="ExploreAgaSection">
<xsl:param name="Node" />
<tr>
<table width="600" cellpadding="0" cellspacing="0" align="center" border="0" style="background-color:#fff; border-collapse:collapse; ">
<xsl:choose>
<xsl:when test="@size-for-desktop = 8">
<xsl:for-each select="aga-block[@size-for-desktop]">
<tr>
<td>
<xsl:attribute name="colspan"><xsl:value-of select="@size-for-desktop"/></xsl:attribute>
<xsl:call-template name="ExploreAgaText">
<xsl:with-param name="Node" select="." />
</xsl:call-template>
</td>
</tr>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<tr>
<xsl:for-each select="aga-block[@size-for-desktop!=8]">
<td>
<xsl:attribute name="colspan"><xsl:value-of select="@size-for-desktop"/></xsl:attribute>
<xsl:call-template name="ExploreAgaText">
<xsl:with-param name="Node" select="." />
</xsl:call-template>
</td>
</xsl:for-each>
</tr>
</xsl:otherwise>
</xsl:choose>
</table>
</tr>
</xsl:template>
<xsl:template name="ExploreAgaText">
<xsl:param name="Node" />
<xsl:copy-of select="$Node/aga-text/node()" />
</xsl:template>
</xsl:stylesheet>
当前输出:
<table width="100%" cellpadding="0" cellspacing="0" border="0" bgcolor="#ffffff">
<tbody>
<tr>
<table width="600" cellpadding="0" cellspacing="0" align="center" border="0" style="background-color:#fff; border-collapse:collapse; ">
<tr>
<td colspan="4">2</td>
<td colspan="4">3</td>
<td colspan="2">4</td>
<td colspan="6">5</td>
</tr>
</table>
</tr>
</tbody>
</table>
预期输出
<table width="100%" cellpadding="0" cellspacing="0" border="0" bgcolor="#ffffff">
<tbody>
<tr>
<table width="600" cellpadding="0" cellspacing="0" align="center" border="0" style="background-color:#fff; border-collapse:collapse; ">
<tr>
<td colspan="8">1</td>
</tr>
<tr>
<td colspan="4">2</td>
<td colspan="4">3</td>
</tr>
<tr>
<td colspan="2">4</td>
<td colspan="6">5</td>
</tr>
<td colspan="8">6</td>
</tr>
</table>
</tr>
</tbody>
</table>
提前致谢。
【问题讨论】:
-
一些事情:你如何确定每行 colspan 的最大总和是多少?您使用的是哪个 XSLT 引擎?
-
@Sebastien 感谢您的回复。 colspan 的最大总和默认设置为 8 并且 "xslt-processor": "^0.11.5"
-
我认为这不是 XSLT 引擎名称!
-
哎呀。我很抱歉。你的意思是环境,它的nodejs
标签: xml xslt xslt-1.0 xsl-stylesheet