Ситуация здесь - это Input, имеющий поля «Type», «StateCode» и «amount» в каждой детали, выходной образец имеет Detail и StateDetail.Все детали из входных данных должны отображаться в детали (один-один), но StateDetail должен группироваться по состоянию и типу (1 или 2) и суммировать всю сумму для каждого состояния (пример выходных данных объяснит это)
Входной XML:
<ns0:Root xmlns:ns0="http://TestConvoys.Input2">
<Record>
<Detail>
<Type>1</Type>
<Name>Danny</Name>
<Addr>Overland</Addr>
<State>KS</State>
<Amount>100</Amount>
</Detail>
<Detail>
<Type>2</Type>
<Name>Larry</Name>
<Addr>Overland</Addr>
<State>KS</State>
<Amount>100</Amount>
</Detail>
<Detail>
<Type>1</Type>
<Name>Sam</Name>
<Addr>Miami</Addr>
<State>FL</State>
<Amount>100</Amount>
</Detail>
<Detail>
<Type>2</Type>
<Name>Ricky</Name>
<Addr>Kansas</Addr>
<State>FL</State>
<Amount>200</Amount>
</Detail>
<Detail>
<Type>1</Type>
<Name>kenny</Name>
<Addr>Newjersey</Addr>
<State>CA</State>
<Amount>50</Amount>
</Detail>
<Detail>
<Type>2</Type>
<Name>John</Name>
<Addr>Overland</Addr>
<State>CA</State>
<Amount>100</Amount>
</Detail>
<Detail>
<Type>1</Type>
<Name>Bailey</Name>
<Addr>Overland</Addr>
<State>TX</State>
<Amount>100</Amount>
</Detail>
<Detail>
<Type>2</Type>
<Name>Sam</Name>
<Addr>Miami</Addr>
<State>TX</State>
<Amount>100</Amount>
</Detail>
<Detail>
<Type>1</Type>
<Name>Ricky</Name>
<Addr>Kansas</Addr>
<State>KS</State>
<Amount>200</Amount>
</Detail>
<Detail>
<Type>2</Type>
<Name>kenny</Name>
<Addr>Newjersey</Addr>
<State>KS</State>
<Amount>50</Amount>
</Detail>
</Record>
</ns0:Root>
Ожидаемый вывод:
<ns0:Root xmlns:ns0="http://TestConvoys.Output">
<Record>
<Detail>
<Type>1</Type>
<Name>Danny</Name>
<Address>Overland</Address>
<StateCode>KS</StateCode>
<Amount>100</Amount>
</Detail>
<Detail>
<Type>2</Type>
<Name>Larry</Name>
<Address>Overland</Address>
<StateCode>KS</StateCode>
<Amount>100</Amount>
</Detail>
<Detail>
<Type>1</Type>
<Name>Sam</Name>
<Address>Miami</Address>
<StateCode>FL</StateCode>
<Amount>100</Amount>
</Detail>
<Detail>
<Type>2</Type>
<Name>Ricky</Name>
<Address>Kansas</Address>
<StateCode>FL</StateCode>
<Amount>200</Amount>
</Detail>
<Detail>
<Type>1</Type>
<Name>kenny</Name>
<Address>Newjersey</Address>
<StateCode>CA</StateCode>
<Amount>50</Amount>
</Detail>
<Detail>
<Type>2</Type>
<Name>John</Name>
<Address>Overland</Address>
<StateCode>CA</StateCode>
<Amount>100</Amount>
</Detail>
<Detail>
<Type>1</Type>
<Name>Bailey</Name>
<Address>Overland</Address>
<StateCode>TX</StateCode>
<Amount>100</Amount>
</Detail>
<Detail>
<Type>2</Type>
<Name>Sam</Name>
<Address>Miami</Address>
<StateCode>TX</StateCode>
<Amount>100</Amount>
</Detail>
<Detail>
<Type>1</Type>
<Name>Ricky</Name>
<Address>Kansas</Address>
<StateCode>KS</StateCode>
<Amount>200</Amount>
</Detail>
<Detail>
<Type>2</Type>
<Name>kenny</Name>
<Address>Newjersey</Address>
<StateCode>KS</StateCode>
<Amount>50</Amount>
</Detail>
<StateDetail>
<Detail>
<StateCode>CA</StateCode>
<TotalAmountType1>50</TotalAmountType1>
<TotalAmountType2>100</TotalAmountType2>
</Detail>
<Detail>
<StateCode>FL</StateCode>
<TotalAmountType1>100</TotalAmountType1>
<TotalAmountType2>200</TotalAmountType2>
</Detail>
<Detail>
<StateCode>KS</StateCode>
<TotalAmountType1>300</TotalAmountType1>
<TotalAmountType2>150</TotalAmountType2>
</Detail>
<Detail>
<StateCode>TX</StateCode>
<TotalAmountType1>100</TotalAmountType1>
<TotalAmountType2>100</TotalAmountType2>
</Detail>
</StateDetail>
</Record>
</ns0:Root>
XSLT-код:
<?xml version="1.0" encoding="utf-16"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:var="http://schemas.microsoft.com/BizTalk/2003/var" exclude-result-prefixes="msxsl var s0" version="1.0" xmlns:s0="http://TestConvoys.Input2" xmlns:ns0="http://TestConvoys.Output">
<xsl:output omit-xml-declaration="yes" method="xml" version="1.0" />
<xsl:key name="group1" match ="Detail" use ="State"/>
<xsl:template match="/">
<xsl:apply-templates select="/s0:Root" />
</xsl:template>
<xsl:template match="/s0:Root">
<ns0:Root>
<Record>
<xsl:for-each select="Record/Detail">
<Detail>
<Type>
<xsl:value-of select="Type/text()" />
</Type>
<Name>
<xsl:value-of select="Name/text()" />
</Name>
<Address>
<xsl:value-of select="Addr/text()" />
</Address>
<StateCode>
<xsl:value-of select="State/text()" />
</StateCode>
<Amount>
<xsl:value-of select="Amount/text()" />
</Amount>
</Detail>
</xsl:for-each>
<StateDetail>
<xsl:for-each select="Record/Detail[generate-id(.)=generate-id(key('group1',State))]">
<xsl:sort select="State" order="ascending"/>
<Detail>
<StateCode>
<xsl:value-of select="State/text()" />
</StateCode>
<TotalAmountType1>
<xsl:value-of select="sum(key('group1', State[../Type=1])/Amount)"/>
</TotalAmountType1>
<TotalAmountType2>
<xsl:value-of select="sum(key('group1', State[../Type=2])/Amount)"/>
</TotalAmountType2>
</Detail>
</xsl:for-each>
</StateDetail>
</Record>
</ns0:Root>
</xsl:template>
</xsl:stylesheet>
Фактический выход XSLT:
<ns0:Root xmlns:ns0="http://TestConvoys.Output">
<Record>
<Detail>
<Type>1</Type>
<Name>Danny</Name>
<Address>Overland</Address>
<StateCode>KS</StateCode>
<Amount>100</Amount>
</Detail>
<Detail>
<Type>2</Type>
<Name>Larry</Name>
<Address>Overland</Address>
<StateCode>KS</StateCode>
<Amount>100</Amount>
</Detail>
<Detail>
<Type>1</Type>
<Name>Sam</Name>
<Address>Miami</Address>
<StateCode>FL</StateCode>
<Amount>100</Amount>
</Detail>
<Detail>
<Type>2</Type>
<Name>Ricky</Name>
<Address>Kansas</Address>
<StateCode>FL</StateCode>
<Amount>200</Amount>
</Detail>
<Detail>
<Type>1</Type>
<Name>kenny</Name>
<Address>Newjersey</Address>
<StateCode>CA</StateCode>
<Amount>50</Amount>
</Detail>
<Detail>
<Type>2</Type>
<Name>John</Name>
<Address>Overland</Address>
<StateCode>CA</StateCode>
<Amount>100</Amount>
</Detail>
<Detail>
<Type>1</Type>
<Name>Bailey</Name>
<Address>Overland</Address>
<StateCode>TX</StateCode>
<Amount>100</Amount>
</Detail>
<Detail>
<Type>2</Type>
<Name>Sam</Name>
<Address>Miami</Address>
<StateCode>TX</StateCode>
<Amount>100</Amount>
</Detail>
<Detail>
<Type>1</Type>
<Name>Ricky</Name>
<Address>Kansas</Address>
<StateCode>KS</StateCode>
<Amount>200</Amount>
</Detail>
<Detail>
<Type>2</Type>
<Name>kenny</Name>
<Address>Newjersey</Address>
<StateCode>KS</StateCode>
<Amount>50</Amount>
</Detail>
<StateDetail>
<Detail>
<StateCode>CA</StateCode>
<TotalAmountType1>150</TotalAmountType1>
<TotalAmountType2>0</TotalAmountType2>
</Detail>
<Detail>
<StateCode>FL</StateCode>
<TotalAmountType1>300</TotalAmountType1>
<TotalAmountType2>0</TotalAmountType2>
</Detail>
<Detail>
<StateCode>KS</StateCode>
<TotalAmountType1>450</TotalAmountType1>
<TotalAmountType2>0</TotalAmountType2>
</Detail>
<Detail>
<StateCode>TX</StateCode>
<TotalAmountType1>200</TotalAmountType1>
<TotalAmountType2>0</TotalAmountType2>
</Detail>
</StateDetail>
</Record>
</ns0:Root>
XSLT суммирует всю сумму, но не в упомянутом выше случае, не знаю, чего мне не хватает
Благодарю, если кто-нибудь может помочь мне в этом