XSLT: Sum of all previous attributes

2019-09-12 03:32发布

问题:

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

XML CODE:

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

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

Can someone help me?

回答1:

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

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="'&#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>

Produces the output:

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

element #2
1
2
3


回答2:

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(), '&#10;' )"/>
    <xsl:for-each select=".//field">
        <xsl:value-of select="@arraySize + sum(preceding::field[generate-id(ancestor::element)=$elementID]/@arraySize)" />
        <xsl:text>&#10;</xsl:text>
    </xsl:for-each>
</xsl:for-each>
</xsl:template>


标签: xml xslt sum