在XSLT上非常绿色,我正在研究的一个系统是使用它在前端生成一些表。基本上对db2实例执行查询,结果集被解析为xml,输出类似于...
<ResultSet>
<Row>
<node1></node1>
<node2></node2>
<etc>
</Row>
<Row>
</Row>
</ResultSet>
我想知道如何在不需要使用for-each循环的情况下推进节点。这是我对XSLT内部变量的理解(这是有限的)。
在页面的末尾,我必须使用我在上面创建的变量创建一个表。结果集的假设是它将返回三行而不是更多/更少。我的xslt中的一些代码如下......
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
....after some html population...
<tbody>
<tr>
<td>Column</td>
<td class="rightAligned">
<xsl:call-template name="formatAsCurrencyNoDecimals">
<xsl:with-param name="numberToFormat"
select="summarizedLoads/summary/total" />
</xsl:call-template>
</td>
.....xsl continues for this row....
一旦完成,需要做什么才能进入下一行?我的意思是我必须将根模板匹配修改为<xsl:template match="/summarizedLoads/">
,然后在每行之后调用它。
在每一行的内部,我需要创建几个变量以供最终使用。
此外,所有行都包含相同数量的数据。希望这很清楚我正在尝试做什么,如果我需要其他任何东西,请告诉我。
答案 0 :(得分:1)
假设您有以下XML:
<root>
<row>1</row>
<row>2</row>
<row>3</row>
<row>4</row>
<row>5</row>
</root>
要仅选择3行,您可以使用XPath://row[position() < 4]
,例如XSLT:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates select="//row[position() < 4]"/>
</xsl:template>
<xsl:template match="row">
<xsl:copy-of select="."/>
</xsl:template>
</xsl:stylesheet>
输出应为:
<row>1</row>
<row>2</row>
<row>3</row>
答案 1 :(得分:0)
XSLT中的最佳位置是使用嵌套模板时。你已经有了一个模板;让我们在你当前拥有的那个下面制作另一个匹配=“行”。在该模板中,执行所有特定于行的内容。然后从你的主模板(match =“/”)中调用它,你希望你的最终行是这样的:
<xsl:apply-templates select = "./Row[0]"/>
<xsl:apply-templates select = "./Row[1]"/>
<xsl:apply-templates select = "./Row[2]"/>
如果您想要所有行而不是前3行,那么您可以这样做:
<xsl:apply-templates select = "./Row"/>
点代表当前元素。因为我们在主模板中,所以这是根元素ResultSet。 / Row表示我们将第一个匹配Row的模板应用于所有后代Row元素。