[英]XSLT: Sum of all previous attributes
I am having some troubling getting summing up the arraySize attribute numbers pertaining to each element. 我在汇总与每个元素有关的arraySize属性编号方面有些麻烦。
XML CODE: 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 code: This is how I've tried to solve the problem. XSLT代码:这就是我试图解决问题的方式。
<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>
Expected output: 预期产量:
element #1
1
2
3
6
7
8
9
11
12
13
14
15
element #2
1
2
... etc
Actual output: 实际输出:
1 <-- Problem #1
1
2
5
6
1 <--- Problem #2
2
4
5
6
7
15 <-- The correct summation is produced here.
I need to sum all of the preseding arraySize attributes. 我需要总结所有preseding arraySize属性。 It works somewhat, but the two problems are: 1. the first field isn't summed. 它有些奏效,但是有两个问题:1.第一字段未加和。 2. The summation restarts at the third indented struct (if indented is the correct terminology). 2.求和从第三个缩进的结构重新开始(如果缩进是正确的术语)。
Can someone help me? 有人能帮我吗?
For this, I think you should be using the preceding::
axis and not the preceding-sibling::
axis. 为此,我认为您应该使用preceding::
轴,而不是preceding-sibling::
轴。
Like this: 像这样:
<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="'
'" />
<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>
Produces the output: 产生输出:
element #1
1
2
3
6
7
8
9
11
12
13
14
15
element #2
1
2
3
Here's another way you could look at it: 这是您可以查看的另一种方式:
<xsl:template match="/">
<xsl:for-each select="head/element">
<xsl:variable name="elementID" select="generate-id()" />
<xsl:value-of select="concat('element #', position(), ' ' )"/>
<xsl:for-each select=".//field">
<xsl:value-of select="@arraySize + sum(preceding::field[generate-id(ancestor::element)=$elementID]/@arraySize)" />
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.