在Xslt中计算值

时间:2016-05-31 08:44:02

标签: xml xslt

我有一个像这样的xml文件:

<RD>
<RE>
    <RecordID>D</RecordID>
    <instanceID>00323</instanceID>
    <Employee_Count>31</Employee_Count>
    <Workers>
        <Time_Type Descriptor="Full time">
            <ID type="Position_Time_Type_ID">Full_time</ID>
        </Time_Type>
    </Workers>
    <Workers>
        <Time_Type Descriptor="Full time">
           <ID type="Position_Time_Type_ID">Full_time</ID>
        </Time_Type>
    </Workers>
    <Workers>
        <Time_Type Descriptor="Part time">
            <ID type="Position_Time_Type_ID">Part_time</ID>
        </Time_Type>
    </Workers>
</RE>
<RE>
    <RecordID>D</RecordID>
    <instanceID>09903</instanceID>
    <Employee_Count>41</Employee_Count>
    <Workers>
        <Time_Type Descriptor="Full time">
            <ID type="Position_Time_Type_ID">Full_time</ID>
        </Time_Type>
    </Workers>
    <Workers>
        <Time_Type Descriptor="Full time">
            <ID type="Position_Time_Type_ID">Full_time</ID>
        </Time_Type>
    </Workers>
</RE> </RD>

我希望产生这样的输出:
D0000000323实例计数为instanceID = 00323

D0000000323次序的次数为instanceID = 00323
D0000009903对于instanceID = 09903的零件时间计数

我尝试了各组和条件的各种组合,但我被卡住了。另外,我是xslt转换的初学者。请建议我如何实现这一目标。

我正在尝试以下代码:

 <xsl:output method="text"/>
  <xsl:variable name="linefeed" select="'&#xA;'"></xsl:variable>
    <xsl:template match="/">
<xsl:value-of select="$linefeed"/>
<xsl:for-each select="/RD/RE">
    <xsl:variable name="CC">
        <xsl:value-of select="/RD/RE/instanceID"/>
    </xsl:variable>
    <xsl:for-each-group select="RE" group-by="instanceID">
        <xsl:sort select= "current-grouping-key()"/>
        <xsl:variable name="CostCenter" select="current-grouping-key()"/>
        <xsl:variable name="CC1">
            <xsl:value-of select="/RD/RE/instanceID"/>
        </xsl:variable>
        <CC1>
            <xsl:value-of select="$CC1"/>
        </CC1>
        <xsl:variable name="FullTimeCount">
            <xsl:value-of select="count(/RD/RE[instanceID='$CC']/Workers/Time_Type[@Descriptor='Full time'])"/>
        </xsl:variable>
        <FullTime>
            <xsl:value-of select="$FullTimeCount"/>
        </FullTime>
    </xsl:for-each-group>
    <xsl:value-of select="$linefeed"/>
</xsl:for-each>

1 个答案:

答案 0 :(得分:0)

如果RE已经识别出instanceID元素,那么我认为您不必对它们进行分组,您只需要按描述符对Workers个元素进行分组:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">

    <xsl:output method="text"/>

    <xsl:template match="/">
        <xsl:for-each select="RD/RE">
            <xsl:sort select="instanceID"/>
            <xsl:for-each-group select="Workers" group-by="Time_Type/@Descriptor">
                <xsl:sort select="current-grouping-key()"/>
                <xsl:value-of select="concat(../RecordID, format-number(../instanceID, '0000000000'), ' ', current-grouping-key(), ' ', count(current-group()), '&#10;')"/>
            </xsl:for-each-group>       
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

输出就是这样。

D0000000323 Full time 2
D0000000323 Part time 1
D0000009903 Full time 2