Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml XSLT:所有先前属性的总和_Xml_Xslt_Sum - Fatal编程技术网

Xml XSLT:所有先前属性的总和

Xml XSLT:所有先前属性的总和,xml,xslt,sum,Xml,Xslt,Sum,我在总结与每个元素相关的arraySize属性编号时遇到了一些麻烦 XML代码: <head> <element> <message name="something"> <field arraySize="1"/> <struct name="asdf"> <struct name="qwera">

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

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>
实际产量:

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

1为此,我认为您应该使用
前面的::
轴,而不是
前面的兄弟::

像这样:

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

你可以用另一种方式来看待它:

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




在这种情况下,您能显示实际期望的输出吗?谢谢哦,对不起。忘了吧。我已经编辑了这篇文章。谢谢你的回复,但我刚刚注意到我犯了一个相当严重的错误,遗漏了一些细节。有多个元素节点,它应该只汇总自身内部的属性。很抱歉,希望您仍能提供一些帮助。@user3010546但是现在您的XML源缺少一个根元素,因此它是无效的。非常感谢您修复了它:)我已经关注这个问题好几个小时了,现在我看到了解决方案,它看起来非常简单。再次感谢!
<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
<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>