XSLT 行计数器 - 有那么难吗? [英] XSLT line counter - is it that hard?

查看:22
本文介绍了XSLT 行计数器 - 有那么难吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次我需要使用 JScript 在 XSLT 中进行行计数时,我都会作弊,但在这种情况下我不能这样做.我只想在整个输出文件中写出一个行计数器.这个基本示例有一个简单的解决方案:

I have cheated every time I've needed to do a line count in XSLT by using JScript, but in this case I can't do that. I simply want to write out a line counter throughout an output file. This basic example has a simple solution:

<xsl:for-each select="Records/Record">
   <xsl:value-of select="position()"/>
</xsl:for-each>

输出将是:

1

2

3

4

等等...

但是如果嵌套的 foreach 的结构更复杂怎么办:

But what if the structure is more complex with nested foreach's :

<xsl:for-each select="Records/Record">
   <xsl:value-of select="position()"/>
   <xsl:for-each select="Records/Record">
       <xsl:value-of select="position()"/>
   </xsl:for-each>
</xsl:for-each>

在这里,内部 foreach 只会重置计数器(所以你会得到 1, 1, 2, 3, 2, 1, 2, 3, 1, 2 等等).有谁知道我如何输出文件中的位置(即行数)?

Here, the inner foreach would just reset the counter (so you get 1, 1, 2, 3, 2, 1, 2, 3, 1, 2 etc). Does anyone know how I can output the position in the file (ie. a line count)?

推荐答案

XML 文件中的一行与元素实际上并不相同.在您的第一个示例中,您并没有真正计算行数 - 而是计算元素的数量.

A line in an XML file is not really the same as an element. In your first example you don't really count the lines - but the number of elements.

XML 文件可能如下所示:

An XML file could look like this:

<cheeseCollection>
<cheese country="Cyprus">Gbejna</cheese><cheese>Liptauer</cheese><cheese>Anari</cheese>
</cheeseCollection>

或者完全相同的 XML 文件看起来像这样:

Or the exact same XML file can look like this:

<cheeseCollection>
    <cheese
       country="Cyprus">Gbejna</cheese>
    <cheese>Liptauer</cheese>
    <cheese>Anari</cheese>
</cheeseCollection>

XSLT 将完全相同的解释 - 它不会真正打扰换行符.

which the XSLT will interpet exactly the same - it will not really bother with the line breaks.

因此,很难使用 XSLT 以您想要的方式显示行号 - 它并不是真正用于那种解析.

Therefore it's hard to show line numbers in the way you want using XSLT - it's not really meant for for that kind of parsing.

如果我错了,有人会纠正我,但我会说你需要 Javascript 或其他一些脚本语言来做你想做的事.

Someone correct me if I'm wrong, but I'd say you would need Javascript or some other scripting language to do what you want.

这篇关于XSLT 行计数器 - 有那么难吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆