如何计算xsl中相同的前一个兄弟姐妹的数量

sko*_*oll 4 xslt xslt-2.0

我正在使用xslt版本2,我正在尝试将xml转换为fo输出,而我却陷入了一个特定的问题.

这是我的输入:

    <a1/>
    <a1/>
    <b/>
    <c/>
    <d/>
    <a2/>
    <b/>
    <c/>
    <a1/>
    <a1/>
    <a1/>
    <a1/>
    <b/>
    <c/>
    <d/>
Run Code Online (Sandbox Code Playgroud)

从功能上讲,这些数据包含由a1 | a2,b?,c?,d?定义的'集'列表.

我的问题是,我没有看到如何计算特定"集合"的a1标签的数量.

确实,我已经写了我的xsl,我得到了这样的输出:

<fo:table>
    <fo:row>
        <fo:cell>b: </fo:cell>
        <fo:cell>b value</fo:cell>
    </fo:row>
    <fo:row>
        <fo:cell>a1: </fo:cell>
        <fo:cell>number of a1 ???</fo:cell> <-- what I am trying to retrieve
    </fo:row>
    <fo:row>
        ...
    </fo:row>
    ...
</fo:table>
Run Code Online (Sandbox Code Playgroud)

我在a1 + | a2标签上做了一个apply-template,如果a1标签有一个等于a1的跟随兄弟,我什么都不做.我认为必须有一种方法来计算前面兄弟的标签(但那么如何确保只计算相应的?)

任何提示将不胜感激!

编辑:在上面的输入示例中,第一个计数应为2:

    <a1/>
    <a1/>
    <b/>
    <c/>
    <d/>
Run Code Online (Sandbox Code Playgroud)

那么应该是4,而不是6:

    <a1/>
    <a1/>
    <a1/>
    <a1/>
    <b/>
    <c/>
    <d/>
Run Code Online (Sandbox Code Playgroud)

hr_*_*117 9

你的问题不是很清楚.
应该怎样"对应一个 "是什么?a1在当前的计数之前统计所有:

 count(preceding-sibling::a1) 
Run Code Online (Sandbox Code Playgroud)

如果需要,您可以添加如下谓词:

 count(preceding-sibling::a1[/corresponding one/]) 
Run Code Online (Sandbox Code Playgroud)

要仅计算a1节点序列中的主要兄弟a1,请尝试:找到不是a1的第一个节点.

<xsl:variable name="firstnota1" select="preceding-sibling::*[not (self::a1)][1]" />
Run Code Online (Sandbox Code Playgroud)

胜过的结果是,计算当前a1之前的所有节点减去第一个不是a1 +节点之前的节点数.

<xsl:value-of select="count(preceding-sibling::*) 
       -  count($firstnota1/preceding-sibling::* | $firstnota1)"/>
Run Code Online (Sandbox Code Playgroud)

或者没有变量:

<xsl:value-of 
      select="count(preceding-sibling::*)
             -  count( preceding-sibling::*[not (self::a1)][1]
                      /preceding-sibling::*
                      | preceding-sibling::*[not (self::a1)][1] )"/>
Run Code Online (Sandbox Code Playgroud)