2011-11-22 232 views
0

我有下面的XMLXSLT分組與子元素的添加/組合

<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和類型相同,是與上面的最後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

+0

你能提供一些輸出嗎?不知道你想做什麼。 – FailedDev

回答

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組中具有相同Type和Investm值的第一個元素InvestmentAccount之一entStrategyId。這可以很容易地改變。

+0

這看起來很刺眼,但不幸的是我正在運行使用.net所以只有1.0 :( –

+1

嘗試使用撒克遜。我認爲有一個.NET的實現。請參閱http://saxon.sourceforge.net/#F9.3HE(它的開源它是一個xslt 2.0處理器) –

1

XSLT 1.0將使用Muenchian分組。

我認爲在這種情況下,你是分組兩次。首先你被InvestmentAccount元素分組,所以你需要像這樣

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

一個鍵,然後,你還需要GROUP BY 投資賬戶內元素。

<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(請注意,我假定稱爲根元素投資

<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(與投資的根元素),下面是輸出:

<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屬性元素,但希望你可以自己調整。