这里的情况是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将所有金额相加,但不包括我提到的情况,不知道我所缺少的内容
感谢有人可以帮助我
答案 0 :(得分:0)
代替:
<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>
这样做:
<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>