Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The XSLT 1.0 would make use of Muenchian Grouping.</p> <p>I think in this case, you are grouping twice. First you are grouping by <strong>InvestmentAccount</strong> elements, so you would need a key like so</p> <pre><code>&lt;xsl:key name="Accounts" match="InvestmentAccount" use="concat(@Type, '|', @InvestmentStrategyId)" /&gt; </code></pre> <p>And then, you also need to group by <strong>Investment</strong> elements within the account.</p> <pre><code>&lt;xsl:key name="Investments" match="Investment" use="concat(../@Type, '|', ../@InvestmentStrategyId, '|', @FundName)" /&gt; </code></pre> <p>Do note the use of the pipe in the concatenetion. This could be any character, but it must be one that does not feature in any of the attributes.</p> <p>To group by <strong>InvestmentAccount</strong> elements, you can then just match the first element in each group like the following:</p> <pre><code>&lt;xsl:apply-templates select="InvestmentAccount[ generate-id() = generate-id(key('Accounts', concat(@Type, '|', @InvestmentStrategyId))[1])]" /&gt; </code></pre> <p>And once in the group, you can get all the <strong>Investment</strong> elements like so:</p> <pre><code> &lt;xsl:apply-templates select="//InvestmentAccount [@Type=current()/@Type] [@InvestmentStrategyId = current()/@InvestmentStrategyId]/Investment [generate-id() = generate-id(key('Investments', concat(../@Type, '|', ../@InvestmentStrategyId, '|', @FundName))[1])]" /&gt; </code></pre> <p>Here is the full XSLT (Do note I have assumed a root element called <strong>Investments</strong></p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output method="xml" indent="yes"/&gt; &lt;xsl:key name="Accounts" match="InvestmentAccount" use="concat(@Type, '|', @InvestmentStrategyId)" /&gt; &lt;xsl:key name="Investments" match="Investment" use="concat(../@Type, '|', ../@InvestmentStrategyId, '|', @FundName)" /&gt; &lt;xsl:template match="/Investments"&gt; &lt;xsl:apply-templates select="InvestmentAccount[generate-id() = generate-id(key('Accounts', concat(@Type, '|', @InvestmentStrategyId))[1])]" /&gt; &lt;/xsl:template&gt; &lt;xsl:template match="InvestmentAccount"&gt; &lt;xsl:copy&gt; &lt;xsl:copy-of select="@*" /&gt; &lt;xsl:apply-templates select="//InvestmentAccount[@Type=current()/@Type][@InvestmentStrategyId = current()/@InvestmentStrategyId]/Investment[generate-id() = generate-id(key('Investments', concat(../@Type, '|', ../@InvestmentStrategyId, '|', @FundName))[1])]" /&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;xsl:template match="Investment"&gt; &lt;xsl:copy&gt; &lt;xsl:copy-of select="@FundName" /&gt; &lt;xsl:attribute name="FundValue"&gt;&lt;xsl:value-of select="format-number(sum(key('Investments', concat(../@Type, '|', ../@InvestmentStrategyId, '|', @FundName))/@FundValue), '0.00')" /&gt;&lt;/xsl:attribute&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;xsl:template match="@*|node()"&gt; &lt;xsl:copy&gt; &lt;xsl:apply-templates select="@*|node()"/&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>When applied to your sample XML (with a root element of Investments), the following is output:</p> <pre><code>&lt;InvestmentAccount Id="Element01_Source3_Sequqence002" Type="Standard" InvestmentStrategyId="Employer" ParameterOverrideIds="AllocationRateOverride"&gt; &lt;Investment FundName="Fund032" FundValue="4754.82" /&gt; &lt;Investment FundName="Fund034" FundValue="4643.48" /&gt; &lt;Investment FundName="Fund035" FundValue="2509.46" /&gt; &lt;Investment FundName="Fund038" FundValue="7104.71" /&gt; &lt;Investment FundName="Fund042" FundValue="4244.08" /&gt; &lt;/InvestmentAccount&gt; &lt;InvestmentAccount Id="Element01_Source4_Sequence003" Type="DWPRebate" InvestmentStrategyId="DSS" ParameterOverrideIds="DWPAllocationRateOverride"&gt; &lt;Investment FundName="Fund032" FundValue="1881.76" /&gt; &lt;Investment FundName="Fund034" FundValue="1584.18" /&gt; &lt;Investment FundName="Fund035" FundValue="872.99" /&gt; &lt;Investment FundName="Fund038" FundValue="2899.53" /&gt; &lt;Investment FundName="Fund042" FundValue="1762.62" /&gt; &lt;/InvestmentAccount&gt; &lt;InvestmentAccount Id="Element01_Source2_Sequence001" Type="Standard" InvestmentStrategyId="Employee" ParameterOverrideIds="AllocationRateOverride"&gt; &lt;Investment FundName="Fund032" FundValue="7395.91" /&gt; &lt;Investment FundName="Fund034" FundValue="7222.72" /&gt; &lt;Investment FundName="Fund035" FundValue="3903.52" /&gt; &lt;Investment FundName="Fund038" FundValue="11051.32" /&gt; &lt;Investment FundName="Fund042" FundValue="6602.54" /&gt; &lt;/InvestmentAccount&gt; &lt;InvestmentAccount Id="Element02_Source2_Sequence004" Type="TransferNonPR" InvestmentStrategyId="Employee" ParameterOverrideIds="AllocationRateOverride"&gt; &lt;Investment FundName="Fund032" FundValue="11056.71" /&gt; &lt;Investment FundName="Fund034" FundValue="12401.34" /&gt; &lt;Investment FundName="Fund035" FundValue="6634.86" /&gt; &lt;Investment FundName="Fund038" FundValue="16545.00" /&gt; &lt;Investment FundName="Fund042" FundValue="10036.26" /&gt; &lt;Investment FundName="fictiousextra" FundValue="1414.00" /&gt; &lt;/InvestmentAccount&gt; </code></pre> <p>I wasn't sure how you wanted the attributes for the grouped <strong>InvestmentAccount</strong> element, but hopefully you can adjust that yourself.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload