每次我需要通过使用JScript在XSLT中进行行计数时我都被骗了,但在这种情况下我不能这样做.我只是想在整个输出文件中写出一个行计数器.这个基本示例有一个简单的解决方案
<xsl:for-each select="Records/Record">
<xsl:value-of select="position()"/>
</xsl:for-each>
Run Code Online (Sandbox Code Playgroud)
输出将是:
1
2
3
4
等等...
但是如果嵌套的foreach的结构更复杂呢:
<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>
Run Code Online (Sandbox Code Playgroud)
在这里,内部foreach将重置计数器(所以你得到1,1,2,3,2,1,2,3,1,2等).有谁知道如何输出文件中的位置(即行数)?
虽然标记XML文档序列化的行号是非常不可能的(因为这种序列化本身是模糊的),但完全有可能,并且easy对常规文本的行进行编号.
这种转变:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:call-template name="numberLines"/>
</xsl:template>
<xsl:template name="numberLines">
<xsl:param name="pLastLineNum" select="0"/>
<xsl:param name="pText" select="."/>
<xsl:if test="string-length($pText)">
<xsl:value-of select="concat($pLastLineNum+1, ' ')"/>
<xsl:value-of select="substring-before($pText, '
')"/>
<xsl:text>
</xsl:text>
<xsl:call-template name="numberLines">
<xsl:with-param name="pLastLineNum"
select="$pLastLineNum+1"/>
<xsl:with-param name="pText"
select="substring-after($pText, '
')"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
应用于此XML文档时:
<t>The biggest airlines are imposing "peak travel surcharges"
this summer. In other words, they're going to raise fees
without admitting they're raising fees: Hey, it's not a $30
price hike. It's a surcharge! This comes on the heels of
checked-baggage fees, blanket fees, extra fees for window
and aisle seats, and "snack packs" priced at exorbitant
markups. Hotels in Las Vegas and elsewhere, meanwhile, are
imposing "resort fees" for the use of facilities (in other
words, raising room rates without admitting they're
raising room rates). The chiseling dishonesty of these
tactics rankles, and every one feels like another nail in
the coffin of travel as something liberating and
pleasurable.
</t>
Run Code Online (Sandbox Code Playgroud)
产生所需的行号:
1 The biggest airlines are imposing "peak travel surcharges"
2 this summer. In other words, they're going to raise fees
3 without admitting they're raising fees: Hey, it's not a $30
4 price hike. It's a surcharge! This comes on the heels of
5 checked-baggage fees, blanket fees, extra fees for window
6 and aisle seats, and "snack packs" priced at exorbitant
7 markups. Hotels in Las Vegas and elsewhere, meanwhile, are
8 imposing "resort fees" for the use of facilities (in other
9 words, raising room rates without admitting they're
10 raising room rates). The chiseling dishonesty of these
11 tactics rankles, and every one feels like another nail in
12 the coffin of travel as something liberating and
13 pleasurable.
Run Code Online (Sandbox Code Playgroud)
XML文件中的一行与元素实际上并不相同.在您的第一个示例中,您并不真正计算行数 - 而是元素数量.
XML文件可能如下所示:
<cheeseCollection>
<cheese country="Cyprus">Gbejna</cheese><cheese>Liptauer</cheese><cheese>Anari</cheese>
</cheeseCollection>
Run Code Online (Sandbox Code Playgroud)
或者完全相同的XML文件可能如下所示:
<cheeseCollection>
<cheese
country="Cyprus">Gbejna</cheese>
<cheese>Liptauer</cheese>
<cheese>Anari</cheese>
</cheeseCollection>
Run Code Online (Sandbox Code Playgroud)
哪个XSLT会完全相同 - 它不会真的打扰换行符.
因此,很难以您希望的方式使用XSLT显示行号 - 它并不是真正意义上的那种解析.
如果我错了,有人会纠正我,但我会说你需要Javascript或其他一些脚本语言来做你想做的事情.
| 归档时间: |
|
| 查看次数: |
14236 次 |
| 最近记录: |