如何计算xsl中相同的紧邻兄弟的数量 [英] How to count the number of same immediate preceding siblings in xsl

查看:23
本文介绍了如何计算xsl中相同的紧邻兄弟的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 xslt 版本 2,我正在尝试将 xml 转换为 fo 输出,但我遇到了一个特定的问题.

I'm using xslt version 2, I'm trying to transform an xml to a fo output, and I'm stuck on a specific problematic.

这是我的输入:

    <a1/>
    <a1/>
    <b/>
    <c/>
    <d/>
    <a2/>
    <b/>
    <c/>
    <a1/>
    <a1/>
    <a1/>
    <a1/>
    <b/>
    <c/>
    <d/>

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

This data, functionally speaking, contains a list of 'sets' defined by a1|a2,b?,c?,d?.

我的问题是我不知道如何计算特定集合"的 a1 标签数量.

My problem is that I don't see how I can count the number of a1 tags for a specific 'set'.

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

Indeed, I have written my xsl and I get an output like that:

<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>

我在 a1+|a2 标签上做了一个应用模板,如果 a1 标签有一个等于 a1 的后续兄弟,我什么都不做.我认为必须有一种方法来计算具有前面兄弟的标签(但是如何确保只计算相应的标签?)

I have done an apply-template on a1+|a2 tags, and I do nothing if a1 tag has a following sibling that equals to a1. I think there must be a way to count the tags with preceding sibling (but then how to insure to count only the corresponding one?)

任何提示将不胜感激!

在上面的输入示例中,第一个计数应该是 2:

On the above example of input, the first count should be 2:

    <a1/>
    <a1/>
    <b/>
    <c/>
    <d/>

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

then it should be 4, and not 6:

    <a1/>
    <a1/>
    <a1/>
    <a1/>
    <b/>
    <c/>
    <d/>

推荐答案

你的问题不是很清楚.
相应的"应该是什么?在当前之前计算所有 a1 将是:

Your question is not really clear.
What should "the corresponding one" be? Counting all a1 before the current one would be:

 count(preceding-sibling::a1) 

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

if needed you may add predicates like:

 count(preceding-sibling::a1[/corresponding one/]) 

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

To count only the presiding sibling a1 which are in a sequence of a1 nodes try this: Find the first node which is not an a1.

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

得出的结果是,计算当前 a1 之前的所有节点数减去第一个之前的节点数,而不是 a1 + 这个节点本身.

The wonted result than, is count all nodes before the current a1 minus the count of nodes before the first not a1 + this node it self.

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

或不带变量:

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

这篇关于如何计算xsl中相同的紧邻兄弟的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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