XSLT:所有先前属性的总和 [英] XSLT: Sum of all previous attributes

查看:23
本文介绍了XSLT:所有先前属性的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在总结与每个元素相关的 arraySize 属性编号时遇到了一些麻烦.

I am having some troubling getting summing up the arraySize attribute numbers pertaining to each element.

XML 代码:

<head>
    <element>
        <message name="something">
            <field arraySize="1"/>
            <struct name="asdf">
                <struct name="qwera">
                    <field arraySize="1"/>
                    <field arraySize="1"/>                
                </struct>
                <struct name="xcv">
                    <field arraySize="3"/>
                    <field arraySize="1"/>
                </struct>
                <struct name="nnge">
                    <struct name="sdfssk">
                        <field arraySize="1"/>
                        <field arraySize="1"/>                
                    </struct>
                    <struct name="fhjmn">
                        <field arraySize="2"/>
                        <field arraySize="1"/>
                    </struct>
                    <struct name="wetryk">
                        <field arraySize="1"/>
                        <field arraySize="1"/>
                    </struct>
                </struct>
            </struct>
            <field arraySize="1"/>
        </message>
    </element>
    <element>
       ... similar struct "tree"
    </element>
    <element>
       ... similar struct "tree"
    </element>
</head>

XSLT 代码:这是我尝试解决问题的方式.

XSLT code: This is how I've tried to solve the problem.

<xsl:template match="p:struct">
    <xsl:apply-templates>
        <xsl:with-param name="previous" select="sum(preceding-sibling::*//@arraySize)"/>
    </xsl:apply-templates>
</xsl:template>

<xsl:template match="p:field">
    <xsl:param name="previous" select="0"/>
    <xsl:value-of select="$previous + sum(preceding-sibling::*//@arraySize)"/>
</xsl:template>

预期输出:

element #1
1   
2
3
6
7
8   
9
11
12 
13 
14
15

element #2
1
2
... etc

实际输出:

1   <-- Problem #1
1
2
5
6
1   <--- Problem #2
2 
4
5
6
7
15  <-- The correct summation is produced here.

我需要对所有前面的 arraySize 属性求和.它有点工作,但两个问题是: 1. 第一个字段没有求和.2. 求和在第三个缩进结构处重新开始(如果缩进是正确的术语).

I need to sum all of the preseding arraySize attributes. It works somewhat, but the two problems are: 1. the first field isn't summed. 2. The summation restarts at the third indented struct (if indented is the correct terminology).

有人可以帮我吗?

推荐答案

为此,我认为您应该使用 preceding:: 轴而不是 preceding-sibling:: 轴.

For this, I think you should be using the preceding:: axis and not the preceding-sibling:: axis.

像这样:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:p="aaa">
  <xsl:output method="text" indent="yes"/>

  <xsl:variable name="nl" select="'&#xA;'" />

  <xsl:template match="text()" />

  <xsl:template match="/*">
    <xsl:apply-templates select="p:element" />
  </xsl:template>

  <xsl:template match="p:element">
    <xsl:value-of select="concat('element #', position(), $nl)"/>
    <xsl:apply-templates />
    <xsl:value-of select="$nl"/>
  </xsl:template>

  <xsl:template match="p:field">
    <xsl:variable name="elId" select="generate-id(ancestor::p:element)" />
    <xsl:variable name="preds"
                  select="preceding::*/@arraySize[generate-id(ancestor::p:element) = 
                                                  $elId]" />
    <xsl:value-of select="concat(@arraySize + sum($preds), $nl)"/>
  </xsl:template>
</xsl:stylesheet>

产生输出:

element #1
1
2
3
6
7
8
9
11
12
13
14
15

element #2
1
2
3

这篇关于XSLT:所有先前属性的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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