XSLT 1.0中具有多个节点的嵌套分组

时间:2019-05-27 11:26:16

标签: xslt xslt-1.0

我正在使用XSLT 1.0进行分组,但是输出不符合预期。

我需要对MovingLine中各个Movement的数量求和,但是总和却发生在所有Movement中。 这里的键应该对每个运动都适用,但对所有人都不适用

源XML:

`<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<MovementFileFlowRequest>
    <Movement>
        <MovementLine>
            <ArticleNumber>355</ArticleNumber>
            <Quantity>10</Quantity>
            <LogisticVariety>2</LogisticVariety>
            <Status>12</Status>
            <DateFormatted>20190516</DateFormatted>
            <Content/>
            <LineReference/>
        </MovementLine>
        <MovementLine>
            <ArticleNumber>129</ArticleNumber>
            <Quantity>10</Quantity>
            <LogisticVariety>2</LogisticVariety>
            <Status>12</Status>
            <DateFormatted>20190516</DateFormatted>
            <Content/>
            <LineReference/>
        </MovementLine>
        <MovementLine>
            <ArticleNumber>355</ArticleNumber>
            <Quantity>50</Quantity>
            <LogisticVariety>2</LogisticVariety>
            <Status>12</Status>
            <DateFormatted>20190516</DateFormatted>
            <Content/>
            <LineReference/>
        </MovementLine>
    </Movement>
    <Movement>
        <MovementLine>
            <ArticleNumber>359</ArticleNumber>
            <Quantity>10</Quantity>
            <LogisticVariety>2</LogisticVariety>
            <Status>12</Status>
            <DateFormatted>20190516</DateFormatted>
            <Content/>
            <LineReference/>
        </MovementLine>
    </Movement>
    <Movement>
        <MovementLine>
            <ArticleNumber>359</ArticleNumber>
            <Quantity>10</Quantity>
            <LogisticVariety>2</LogisticVariety>
            <Status>12</Status>
            <DateFormatted>20190516</DateFormatted>
            <Content/>
            <LineReference/>
        </MovementLine>
    </Movement>
</MovementFileFlowRequest>`

XSLT:

`<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
    <xsl:output method="xml" indent="yes"/>
    <xsl:key name="Movements" match="MovementLine" use="concat(ArticleNumber, '|', LogisticVariety, '|' , Status)"/>

    <xsl:template match="/">
        <xsl:for-each select="MovementFileFlowRequest">


        <MovementFileFlowRequest >
            <xsl:for-each select="Movement">
                <!-- Apply "group" template to the first Record Line in group -->
                <Movement>
                    <xsl:for-each select="MovementLine[generate-id() = generate-id(key('Movements', concat(ArticleNumber, '|', LogisticVariety, '|' , Status))[1])]">
                        <xsl:variable name="Quantity" select="sum(key('Movements', concat(ArticleNumber, '|', LogisticVariety, '|' , Status))/Quantity)"/>
                        <MovementLine>
                            <xsl:copy-of select="./*[not(name()='Quantity')]"/>
                            <Quantity>
                                <xsl:value-of select="$Quantity" />
                            </Quantity>    
                        </MovementLine>
                    </xsl:for-each>
                </Movement>
            </xsl:for-each>
        </MovementFileFlowRequest>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>`

当前输出:

`<?xml version="1.0" encoding="UTF-8"?>
<MovementFileFlowRequest>
<Movement>
      <MovementLine>
         <ArticleNumber>355</ArticleNumber>
         <LogisticVariety>2</LogisticVariety>
         <Status>12</Status>
         <DateFormatted>20190516</DateFormatted>
         <Content/>
         <LineReference/>
         <Quantity>60</Quantity>
      </MovementLine>
      <MovementLine>
         <ArticleNumber>129</ArticleNumber>
         <LogisticVariety>2</LogisticVariety>
         <Status>12</Status>
         <DateFormatted>20190516</DateFormatted>
         <Content/>
         <LineReference/>
         <Quantity>10</Quantity>
      </MovementLine>
   </Movement>
   <Movement>
      <MovementLine>
         <ArticleNumber>359</ArticleNumber>
         <LogisticVariety>2</LogisticVariety>
         <Status>12</Status>
         <DateFormatted>20190516</DateFormatted>
         <Content/>
         <LineReference/>
         <Quantity>20</Quantity>
      </MovementLine>
   </Movement>
   <Movement/>
</MovementFileFlowRequest>
`

所需的输出:

`<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<MovementFileFlowRequest>
    <Movement>
        <MovementLine>
            <ArticleNumber>355</ArticleNumber>
            <Quantity>60</Quantity>
            <LogisticVariety>2</LogisticVariety>
            <Status>12</Status>
            <DateFormatted>20190516</DateFormatted>
            <Content/>
            <LineReference/>
        </MovementLine>
        <MovementLine>
            <ArticleNumber>129</ArticleNumber>
            <Quantity>10</Quantity>
            <LogisticVariety>2</LogisticVariety>
            <Status>12</Status>
            <DateFormatted>20190516</DateFormatted>
            <Content/>
            <LineReference/>
        </MovementLine>
    </Movement>
    <Movement>
        <MovementLine>
            <ArticleNumber>359</ArticleNumber>
            <Quantity>10</Quantity>
            <LogisticVariety>2</LogisticVariety>
            <Status>12</Status>
            <DateFormatted>20190516</DateFormatted>
            <Content/>
            <LineReference/>
        </MovementLine>
    </Movement>
    <Movement>
        <MovementLine>
            <ArticleNumber>359</ArticleNumber>
            <Quantity>10</Quantity>
            <LogisticVariety>2</LogisticVariety>
            <Status>12</Status>
            <DateFormatted>20190516</DateFormatted>
            <Content/>
            <LineReference/>
        </MovementLine>
    </Movement>
</MovementFileFlowRequest>`

1 个答案:

答案 0 :(得分:0)

更改键event或选择表达式以包括父元素的生成ID,例如use,请确保您在使用use="concat(generated-id(..), '|', ArticleNumber, '|', LogisticVariety, '|' , Status)"函数的XSLT代码中适应所有表达式,并以相同的方式适应表达式。