gpt4 book ai didi

XSLT 行计数器 - 有那么难吗?

转载 作者:行者123 更新时间:2023-12-04 16:41:35 24 4
gpt4 key购买 nike

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

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

输出将是:

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>

在这里,内部 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, '&#xA;')"/>
<xsl:text>&#xA;</xsl:text>

<xsl:call-template name="numberLines">
<xsl:with-param name="pLastLineNum"
select="$pLastLineNum+1"/>
<xsl:with-param name="pText"
select="substring-after($pText, '&#xA;')"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>

应用于此 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>

产生所需的行号 :
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.

关于XSLT 行计数器 - 有那么难吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2916532/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com