XSLT 在不使用 for-each 的情况下推进节点
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....
完成此操作后,需要做什么才能进入下一行?我的想法是,我必须将根模板匹配修改为
,然后在每行之后调用它。
在每一行的内部,我需要创建几个变量以供最后使用。
此外,所有行都包含相同数量的数据。希望这清楚地说明了我正在尝试做什么,如果需要我提供任何其他信息,请告诉我。
Pretty green on XSLT and one of the systems I'm working on is using it to generate some tables on the front end. Basically performing a query against a db2 instance the result set is being parsed into xml and the output is similar to...
<ResultSet>
<Row>
<node1></node1>
<node2></node2>
<etc>
</Row>
<Row>
</Row>
</ResultSet>
I'm wondering how to advance a node without being required to use a for-each loop. This is from my understanding of variables inside of XSLT (which is limited).
At the end of the page I have to create a table using the variables that I create above. What is assumed of the result set is that it will return three rows and no more/less. Some of the code from my xslt is as follows...
<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....
Once this is done what needs to be done to advance to the next row? My though was that I would have to modify the root template match to <xsl:template match="/summarizedLoads/">
and then call that after each row.
Inside of each row I'll need to be creating several variables for use at the end.
Also all rows contain the same amounts of data. Hopefully this is clear as to what I'm trying to do and if anything else is needed from me please let me know.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您有以下 XML:
要仅选择 3 行,您可以使用 XPath:
//row[position() < 4]
,例如 XSLT:输出应该是:
Suppose you have following XML:
To select only 3 rows you can use XPath:
//row[position() < 4]
, e.g. XSLT:Output should be:
XSLT 的最佳优点是使用嵌套模板。您已经有了一个模板;让我们在当前的 match = "Row" 的下方再创建一个。在该模板中,执行所有特定于行的操作。然后从主模板 (match = "/") 中调用它,您希望最后的行如下所示:
如果您想要所有行而不仅仅是前 3 行,您可以这样做:
点代表当前元素。由于我们位于主模板中,因此它是根元素 ResultSet。 /Row 意味着我们将第一个与 Row 匹配的模板应用于所有后代 Row 元素。
The sweet spot in XSLT is when you use nested templates. You've got one template already; let's make another one below the one you currently have where match = "Row". In that template, do all your row-specific stuff. Then call it from within your main template (match = "/") where you want your final rows to be like so:
If you wanted all the rows instead of just the first 3, you would do this instead:
The dot stands for the current element. Since we're in the main template, that's the root element ResultSet. /Row means we're applying the first template that matches Row to all the descendant Row elements.