0

我有以下 XML

    <InvestmentAccount Id="Element01_Source3_Sequqence002" Type="Standard" InvestmentStrategyId="Employer" ParameterOverrideIds="AllocationRateOverride">
      <Investment FundName="Fund032" FundValue="4754.82" />
      <Investment FundName="Fund034" FundValue="4643.48" />
      <Investment FundName="Fund035" FundValue="2509.46" />
      <Investment FundName="Fund038" FundValue="7104.71" />
      <Investment FundName="Fund042" FundValue="4244.08" />
    </InvestmentAccount>
    <InvestmentAccount Id="Element01_Source4_Sequence003" Type="DWPRebate" InvestmentStrategyId="DSS" ParameterOverrideIds="DWPAllocationRateOverride">
      <Investment FundName="Fund032" FundValue="1881.76" />
      <Investment FundName="Fund034" FundValue="1584.18" />
      <Investment FundName="Fund035" FundValue="872.99" />
      <Investment FundName="Fund038" FundValue="2899.53" />
      <Investment FundName="Fund042" FundValue="1762.62" />
    </InvestmentAccount>
     <InvestmentAccount Id="Element01_Source2_Sequence001" Type="Standard" InvestmentStrategyId="Employee" ParameterOverrideIds="AllocationRateOverride">
      <Investment FundName="Fund032" FundValue="7395.91" />
      <Investment FundName="Fund034" FundValue="7222.72" />
      <Investment FundName="Fund035" FundValue="3903.52" />
      <Investment FundName="Fund038" FundValue="11051.32" />
      <Investment FundName="Fund042" FundValue="6602.54" />
    </InvestmentAccount>
    <InvestmentAccount Id="Element02_Source2_Sequence004" Type="TransferNonPR" InvestmentStrategyId="Employee" ParameterOverrideIds="AllocationRateOverride">
      <Investment FundName="Fund032" FundValue="1439.29" />
      <Investment FundName="Fund034" FundValue="1614.31" />
      <Investment FundName="Fund035" FundValue="863.68" />
      <Investment FundName="Fund038" FundValue="2153.80" />
      <Investment FundName="Fund042" FundValue="1306.45" />
    </InvestmentAccount>
    <InvestmentAccount Id="Element03_Source2_Sequence005" Type="TransferNonPR" InvestmentStrategyId="Employee" ParameterOverrideIds="AllocationRateOverride">
      <Investment FundName="Fund032" FundValue="9617.42" />
      <Investment FundName="Fund034" FundValue="10787.03" />
      <Investment FundName="Fund035" FundValue="5771.18" />
      <Investment FundName="Fund038" FundValue="14391.20" />
      <Investment FundName="Fund042" FundValue="8729.81" />
      <Investment FundName="fictiousextra" FundValue="1414" />
    </InvestmentAccount>

我想做的是 InvestmentStrategyId 和 Type 与上面最后 2 个的情况相同(为清楚起见重新排序)是 FundName 相同的地方,我需要对基金值求和。在这种情况下,每边都有相同的内容,但每边可能有一些额外或更少。

所以结果是我需要访问 FundName 和 FundValue 或者我可以求和或已经求和的值。

帮助!

对,这就是我想要实现的输出。

        <InvestmentAccount Id="Element01_Source3_Sequence002" Type="Standard" InvestmentStrategyId="Employer" ParameterOverrideIds="AllocationRateOverride">
          <Investment FundName="Fund032" FundValue="4754.82" />
          <Investment FundName="Fund034" FundValue="4643.48" />
          <Investment FundName="Fund035" FundValue="2509.46" />
          <Investment FundName="Fund038" FundValue="7104.71" />
          <Investment FundName="Fund042" FundValue="4244.08" />
        </InvestmentAccount>
        <InvestmentAccount Id="Element01_Source4_Sequence003" Type="DWPRebate" InvestmentStrategyId="DSS" ParameterOverrideIds="DWPAllocationRateOverride">
          <Investment FundName="Fund032" FundValue="1881.76" />
          <Investment FundName="Fund034" FundValue="1584.18" />
          <Investment FundName="Fund035" FundValue="872.99" />
          <Investment FundName="Fund038" FundValue="2899.53" />
          <Investment FundName="Fund042" FundValue="1762.62" />
        </InvestmentAccount>
         <InvestmentAccount Id="Element01_Source2_Sequence001" Type="Standard" InvestmentStrategyId="Employee" ParameterOverrideIds="AllocationRateOverride">
          <Investment FundName="Fund032" FundValue="7395.91" />
          <Investment FundName="Fund034" FundValue="7222.72" />
          <Investment FundName="Fund035" FundValue="3903.52" />
          <Investment FundName="Fund038" FundValue="11051.32" />
          <Investment FundName="Fund042" FundValue="6602.54" />
        </InvestmentAccount>
<!-- THIS ONE IS THE SUMMED COMBINTION DUE TO InvestmentStrategyId and Type being multiply occuring -->
<InvestmentAccount ...>
          <Investment FundName="Fund032" FundValue="11056.71" />
          <Investment FundName="Fund034" FundValue="12401.34" />
          <Investment FundName="Fund035" FundValue="6634.86" />
          <Investment FundName="Fund038" FundValue="16545" />
          <Investment FundName="Fund042" FundValue="10036.26" />
          <Investment FundName="fictiousextra" FundValue="1414" />
</InvestmentAccount>

包括存在于 1 而不是其他的任何 FundNames。

我应该添加我正在使用 .net 4.0 运行

4

2 回答 2

1

XSLT 1.0 将使用 Muenchian 分组。

我认为在这种情况下,你分组两次。首先,您按InvestmentAccount元素进行分组,因此您需要一个像这样的键

<xsl:key name="Accounts" match="InvestmentAccount" 
         use="concat(@Type, '|', @InvestmentStrategyId)" />

然后,您还需要按帐户内的投资元素进行分组。

<xsl:key name="Investments" match="Investment" 
         use="concat(../@Type, '|', ../@InvestmentStrategyId, '|', @FundName)" />

请注意连接中管道的使用。这可以是任何字符,但它必须是不包含任何属性的字符。

要按InvestmentAccount元素分组,您可以只匹配每个组中的第一个元素,如下所示:

<xsl:apply-templates 
  select="InvestmentAccount[
    generate-id() = 
    generate-id(key('Accounts', concat(@Type, '|', @InvestmentStrategyId))[1])]" />

进入组后,您可以获得所有投资元素,如下所示:

 <xsl:apply-templates 
   select="//InvestmentAccount
     [@Type=current()/@Type]
     [@InvestmentStrategyId = current()/@InvestmentStrategyId]/Investment
        [generate-id() = 
         generate-id(key('Investments', 
           concat(../@Type, '|', ../@InvestmentStrategyId, '|', @FundName))[1])]" />

这是完整的 XSLT(请注意,我假设了一个名为Investments的根元素

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="xml" indent="yes"/>

   <xsl:key name="Accounts" match="InvestmentAccount" use="concat(@Type, '|', @InvestmentStrategyId)" />
   <xsl:key name="Investments" match="Investment" use="concat(../@Type, '|', ../@InvestmentStrategyId, '|', @FundName)" />

   <xsl:template match="/Investments">
      <xsl:apply-templates select="InvestmentAccount[generate-id() = generate-id(key('Accounts', concat(@Type, '|', @InvestmentStrategyId))[1])]" />
   </xsl:template>

   <xsl:template match="InvestmentAccount">
      <xsl:copy>
         <xsl:copy-of select="@*" />
         <xsl:apply-templates select="//InvestmentAccount[@Type=current()/@Type][@InvestmentStrategyId = current()/@InvestmentStrategyId]/Investment[generate-id() = generate-id(key('Investments', concat(../@Type, '|', ../@InvestmentStrategyId, '|', @FundName))[1])]" />
      </xsl:copy>   
   </xsl:template>

   <xsl:template match="Investment">
      <xsl:copy>
         <xsl:copy-of select="@FundName" />
         <xsl:attribute name="FundValue"><xsl:value-of select="format-number(sum(key('Investments', concat(../@Type, '|', ../@InvestmentStrategyId, '|', @FundName))/@FundValue), '0.00')" /></xsl:attribute>
      </xsl:copy>   
   </xsl:template>

   <xsl:template match="@*|node()">
      <xsl:copy>
         <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
   </xsl:template>
</xsl:stylesheet>

当应用于您的示例 XML(具有 Investments 的根元素)时,将输出以下内容:

<InvestmentAccount Id="Element01_Source3_Sequqence002" Type="Standard" InvestmentStrategyId="Employer" ParameterOverrideIds="AllocationRateOverride">
   <Investment FundName="Fund032" FundValue="4754.82" />
   <Investment FundName="Fund034" FundValue="4643.48" />
   <Investment FundName="Fund035" FundValue="2509.46" />
   <Investment FundName="Fund038" FundValue="7104.71" />
   <Investment FundName="Fund042" FundValue="4244.08" />
</InvestmentAccount>
   <InvestmentAccount Id="Element01_Source4_Sequence003" Type="DWPRebate" InvestmentStrategyId="DSS" ParameterOverrideIds="DWPAllocationRateOverride">
   <Investment FundName="Fund032" FundValue="1881.76" />
   <Investment FundName="Fund034" FundValue="1584.18" />
   <Investment FundName="Fund035" FundValue="872.99" />
   <Investment FundName="Fund038" FundValue="2899.53" />
   <Investment FundName="Fund042" FundValue="1762.62" />
</InvestmentAccount>
   <InvestmentAccount Id="Element01_Source2_Sequence001" Type="Standard" InvestmentStrategyId="Employee" ParameterOverrideIds="AllocationRateOverride">
   <Investment FundName="Fund032" FundValue="7395.91" />
   <Investment FundName="Fund034" FundValue="7222.72" />
   <Investment FundName="Fund035" FundValue="3903.52" />
   <Investment FundName="Fund038" FundValue="11051.32" />
   <Investment FundName="Fund042" FundValue="6602.54" />
</InvestmentAccount>
<InvestmentAccount Id="Element02_Source2_Sequence004" Type="TransferNonPR" InvestmentStrategyId="Employee" ParameterOverrideIds="AllocationRateOverride">
   <Investment FundName="Fund032" FundValue="11056.71" />
   <Investment FundName="Fund034" FundValue="12401.34" />
   <Investment FundName="Fund035" FundValue="6634.86" />
   <Investment FundName="Fund038" FundValue="16545.00" />
   <Investment FundName="Fund042" FundValue="10036.26" />
   <Investment FundName="fictiousextra" FundValue="1414.00" />
</InvestmentAccount>

我不确定您希望分组InvestmentAccount元素的属性如何,但希望您可以自己调整。

于 2011-11-23T09:20:05.900 回答
1

XSLT 2.0 解决方案:

<?xml version="1.0" encoding="UTF-8"?>
<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:template match="/">
        <xsl:apply-templates/>
    </xsl:template>
    <xsl:template match="*">
        <xsl:copy>
            <xsl:copy-of select="@*"/>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="InvestmentAccount[1]">
        <xsl:for-each-group select="self::*|following-sibling::InvestmentAccount" group-by="concat(@InvestmentStrategyId,@Type)" >
            <InvestmentAccount Id="{@Id}" Type="{@Type}" InvestmentStrategyId="{@InvestmentStrategyId}" ParameterOverrideIds="{@ParameterOverrideIds}">
                <xsl:for-each-group select="current-group()/Investment" group-by="@FundName">
                    <Investment FundName="{current-grouping-key()}" FundValue="{sum(for $x in current-group()/@FundValue return xs:double(data($x)))}" />
                </xsl:for-each-group>
            </InvestmentAccount>
        </xsl:for-each-group>
    </xsl:template>
    <xsl:template match="InvestmentAccount"/>
</xsl:stylesheet>

我将您的 XML 插入到名为的根<test></test>中,XSLT 的结果是:

<?xml version="1.0" encoding="UTF-8"?>
<test>
    <InvestmentAccount Id="Element01_Source3_Sequence002" Type="Standard"
        InvestmentStrategyId="Employer" ParameterOverrideIds="AllocationRateOverride">
        <Investment FundName="Fund032" FundValue="4754.82"/>
        <Investment FundName="Fund034" FundValue="4643.48"/>
        <Investment FundName="Fund035" FundValue="2509.46"/>
        <Investment FundName="Fund038" FundValue="7104.71"/>
        <Investment FundName="Fund042" FundValue="4244.08"/>
    </InvestmentAccount>
    <InvestmentAccount Id="Element01_Source4_Sequence003" Type="DWPRebate"
        InvestmentStrategyId="DSS" ParameterOverrideIds="DWPAllocationRateOverride">
        <Investment FundName="Fund032" FundValue="1881.76"/>
        <Investment FundName="Fund034" FundValue="1584.18"/>
        <Investment FundName="Fund035" FundValue="872.99"/>
        <Investment FundName="Fund038" FundValue="2899.53"/>
        <Investment FundName="Fund042" FundValue="1762.62"/>
    </InvestmentAccount>
    <InvestmentAccount Id="Element01_Source2_Sequence001" Type="Standard"
        InvestmentStrategyId="Employee" ParameterOverrideIds="AllocationRateOverride">
        <Investment FundName="Fund032" FundValue="7395.91"/>
        <Investment FundName="Fund034" FundValue="7222.72"/>
        <Investment FundName="Fund035" FundValue="3903.52"/>
        <Investment FundName="Fund038" FundValue="11051.32"/>
        <Investment FundName="Fund042" FundValue="6602.54"/>
    </InvestmentAccount>
    <InvestmentAccount Id="Element02_Source2_Sequence004" Type="TransferNonPR"
        InvestmentStrategyId="Employee" ParameterOverrideIds="AllocationRateOverride">
        <Investment FundName="Fund032" FundValue="11056.71"/>
        <Investment FundName="Fund034" FundValue="12401.34"/>
        <Investment FundName="Fund035" FundValue="6634.860000000001"/>
        <Investment FundName="Fund038" FundValue="16545"/>
        <Investment FundName="Fund042" FundValue="10036.26"/>
        <Investment FundName="fictiousextra" FundValue="1414"/>
    </InvestmentAccount>




</test>

注意:InvestmentAccount 属性值是 InvestmentAccount 组中的第一个元素 InvestmentAccount,它的 Type 和 InvestmentStrategyId 具有相同的值。这可以很容易地改变。

于 2011-11-22T22:51:55.627 回答