mapping / xslt challenge - 分组,密钥

时间:2012-05-17 17:32:38

标签: xml xslt map biztalk

我们有一些棘手的映射要求。我正在使用BizTalk映射器在BizTalk应用程序中将传入的xml从一种形式转换为另一种形式。解决方案可以使用XSLT或内置的BizTalk functoid完成。

源架构看起来像这样:

<?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns:b="http://schemas.microsoft.com/BizTalk/2003" xmlns="http://BizTalkTestProject.SourceSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://BizTalkTestProject.SourceSchema">
    <xs:element name="Coverages">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Coverage" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="Category" type="xs:string"/>
                            <xs:element name="BillingChargeType" type="xs:string"/>
                            <xs:element name="ASLCode" type="xs:string"/>
                            <xs:element name="EffectiveDate" type="xs:string"/>
                            <xs:element name="DeltaAmount" type="xs:string"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

目标架构如下所示:

<?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns:b="http://schemas.microsoft.com/BizTalk/2003" xmlns="http://BizTalkTestProject.DestinationSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://BizTalkTestProject.DestinationSchema">
    <xs:element name="Categories" type="CategoriesType"/>
    <xs:complexType name="CategoriesType">
        <xs:sequence>
            <xs:element name="Premium" type="CommonElementsType" maxOccurs="unbounded"/>
            <xs:element name="Tax" type="CommonElementsType" maxOccurs="unbounded"/>
            <xs:element name="Fee" type="CommonElementsType" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="CommonElementsType">
        <xs:sequence>
            <xs:element name="CategoryDetail" type="CategoryDetailType" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="CategoryDetailType">
        <xs:sequence>
            <xs:element name="Type" type="xs:string"/>
            <xs:element name="AnnualStatementLine" type="xs:string"/>
            <xs:element name="Amount" type="xs:string"/>
            <xs:element name="ChangeEffectiveDate" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

传入的xml数据示例:

    <ns0:Coverages xmlns:ns0="http://BizTalkTestProject.SourceSchema">
    <Coverage>
        <Category>premium</Category>
        <BillingChargeType>premium BillingChargeType 2</BillingChargeType>
        <ASLCode>premium ASLCode 2</ASLCode>
        <EffectiveDate>2002-02-02</EffectiveDate>
        <DeltaAmount>22.00</DeltaAmount>
    </Coverage>
    <Coverage>
        <Category>premium</Category>
        <BillingChargeType>premium BillingChargeType 1</BillingChargeType>
        <ASLCode>premium ASLCode 1</ASLCode>
        <EffectiveDate>2001-01-01</EffectiveDate>
        <DeltaAmount>11.00</DeltaAmount>
    </Coverage>
    <Coverage>
        <Category>premium</Category>
        <BillingChargeType>premium BillingChargeType 1</BillingChargeType>
        <ASLCode>premium ASLCode 2</ASLCode>
        <EffectiveDate>2001-01-01</EffectiveDate>
        <DeltaAmount>121.00</DeltaAmount>
    </Coverage>
    <Coverage>
        <Category>premium</Category>
        <BillingChargeType>premium BillingChargeType 1</BillingChargeType>
        <ASLCode>premium ASLCode 1</ASLCode>
        <EffectiveDate>2002-02-02</EffectiveDate>
        <DeltaAmount>112.00</DeltaAmount>
    </Coverage>
    <Coverage>
        <Category>premium</Category>
        <BillingChargeType>premium BillingChargeType 3</BillingChargeType>
        <ASLCode>premium ASLCode 3</ASLCode>
        <EffectiveDate>2003-03-03</EffectiveDate>
        <DeltaAmount>33.00</DeltaAmount>
    </Coverage>
    <Coverage>
        <Category>premium</Category>
        <BillingChargeType>premium BillingChargeType 1</BillingChargeType>
        <ASLCode>premium ASLCode 1</ASLCode>
        <EffectiveDate>2001-01-01</EffectiveDate>
        <DeltaAmount>5.00</DeltaAmount>
    </Coverage>
    <Coverage>
        <Category>tax</Category>
        <BillingChargeType>tax BillingChargeType 4</BillingChargeType>
        <ASLCode>tax ASLCode 4</ASLCode>
        <EffectiveDate>2004-04-04</EffectiveDate>
        <DeltaAmount>44.00</DeltaAmount>
    </Coverage>
    <Coverage>
        <Category>tax</Category>
        <BillingChargeType>tax BillingChargeType 5</BillingChargeType>
        <ASLCode>tax ASLCode 5</ASLCode>
        <EffectiveDate>2005-05-05</EffectiveDate>
        <DeltaAmount>55.00</DeltaAmount>
    </Coverage>
    <Coverage>
        <Category>fee</Category>
        <BillingChargeType>fee BillingChargeType 6</BillingChargeType>
        <ASLCode>fee ASLCode 6</ASLCode>
        <EffectiveDate>2006-06-06</EffectiveDate>
        <DeltaAmount>66.00</DeltaAmount>
    </Coverage>
</ns0:Coverages>

预期输出xml应如下所示:

    <ns0:Categories xmlns:ns0="http://BizTalkTestProject.DestinationSchema">
    <Premium>
        <CategoryDetail>
            <Type>premium BillingChargeType 2</Type>
            <AnnualStatementLine>premium ASLCode 2</AnnualStatementLine>
            <Amount>22.00</Amount>
            <ChangeEffectiveDate>2002-02-02</ChangeEffectiveDate>
        </CategoryDetail>
        <CategoryDetail>
            <Type>premium BillingChargeType 1</Type>
            <AnnualStatementLine>premium ASLCode 1</AnnualStatementLine>
            <Amount>16.00</Amount>
            <ChangeEffectiveDate>2001-01-01</ChangeEffectiveDate>
        </CategoryDetail>
        <CategoryDetail>
            <Type>premium BillingChargeType 1</Type>
            <AnnualStatementLine>premium ASLCode 2</AnnualStatementLine>
            <Amount>121.11</Amount>
            <ChangeEffectiveDate>2001-01-01</ChangeEffectiveDate>
        </CategoryDetail>
        <CategoryDetail>
            <Type>premium BillingChargeType 1</Type>
            <AnnualStatementLine>premium ASLCode 1</AnnualStatementLine>
            <Amount>112.22</Amount>
            <ChangeEffectiveDate>2002-02-02</ChangeEffectiveDate>
        </CategoryDetail>
        <CategoryDetail>
            <Type>premium BillingChargeType 3</Type>
            <AnnualStatementLine>premium ASLCode 3</AnnualStatementLine>
            <Amount>33.00</Amount>
            <ChangeEffectiveDate>2003-03-03</ChangeEffectiveDate>
        </CategoryDetail>
    </Premium>
    <Tax>
        <CategoryDetail>
            <Type>tax BillingChargeType 4</Type>
            <AnnualStatementLine>tax ASLCode 4</AnnualStatementLine>
            <Amount>44.00</Amount>
            <ChangeEffectiveDate>2004-04-04</ChangeEffectiveDate>
        </CategoryDetail>
        <CategoryDetail>
            <Type>tax BillingChargeType 5</Type>
            <AnnualStatementLine>tax ASLCode 5</AnnualStatementLine>
            <Amount>55.00</Amount>
            <ChangeEffectiveDate>2005-05-05</ChangeEffectiveDate>
        </CategoryDetail>
    </Tax>
    <Fee>
        <CategoryDetail>
            <Type>fee BillingChargeType 6</Type>
            <AnnualStatementLine>fee ASLCode 6</AnnualStatementLine>
            <Amount>66.00</Amount>
            <ChangeEffectiveDate>2006-06-06</ChangeEffectiveDate>
        </CategoryDetail>
    </Fee>
</ns0:Categories>

映射要求是: 1.如果源xml中的Category元素具有值“Premium”,那么它应该在Categories / Premium下映射到目标。 2.如果源xml中的Category元素具有值“Tax”,那么它应该在Categories / Tax下映射到目标。 3.如果来自源xml的Category元素具有值“Fee”,那么它应该在Categories / Fee下映射到目的地。 4.输出应该只有Category,BillingChargeType,ALSCode和EffectiveDate的不同记录集。 5.输出应按类别,BillingChargeType,ALSCode和EffectiveDate

在目标节点“Amount”中具有源节点“DeltaAmount”的聚合值

在示例xml中,我已经包含了“Premium”节点的聚合示例,例如

<BillingChargeType>premium BillingChargeType 1</BillingChargeType>
<ASLCode>premium ASLCode 1</ASLCode>
<EffectiveDate>2001-01-01</EffectiveDate>

有两个Coverage记录集,其中包含3个以上的节点。这两者的区别在于

<DeltaAmount>11.00</DeltaAmount> and <DeltaAmount>5.00</DeltaAmount>

在预期输出中,您将看到只有一个与DeltaAmount node = 16映射。

我尝试使用key,distinct和generate-id与apply-templates和for-each循环在xslt中进行映射,但是没有得到我们想要的结果。所以我来这里寻求你的帮助。如果您需要更多信息,请与我们联系。

感谢您的关注,感谢您的帮助! 木孔德

2 个答案:

答案 0 :(得分:1)

如果有人感兴趣,下面是答案的副本,我是从其他人那里得到的。

<?xml version="1.0" encoding="utf-8"?>
 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
 xmlns:ns0="http://BizTalkTestProject.SourceSchema">
   <xsl:output method="xml" indent="yes"/>
   <xsl:key name="group" match="/ns0:Coverages/Coverage" use="concat(BillingChargeType,Category,ASLCode,EffectiveDate)"/>  
   <xsl:template match="/ns0:Coverages">
     <ns0:Categories xmlns:ns0="http://BizTalkTestProject.DestinationSchema">      
       <xsl:for-each select="Coverage[count(. | key('group', concat(BillingChargeType, Category,ASLCode,EffectiveDate))[1]) = 1]">
         <xsl:choose>
           <xsl:when test="./Category ='premium'">
               <ns0:Premium>
                 <ns0:CategoryDetail>
                   <Type>
                     <xsl:value-of select="./BillingChargeType"/>
                   </Type>
                   <AnnualStatementLine>
                     <xsl:value-of select="./ASLCode"/>
                   </AnnualStatementLine>
                   <Amount>
                     <xsl:value-of select="format-number(sum(key('group', concat(BillingChargeType, Category,ASLCode,EffectiveDate))/DeltaAmount),'#,###.00')"/>
                   </Amount>
                   <ChangeEffectiveDate>
                     <xsl:value-of select="./EffectiveDate"/>
                   </ChangeEffectiveDate>

                </ns0:CategoryDetail>
               </ns0:Premium>            
           </xsl:when>
           <xsl:when test="./Category ='tax'">            
               <Tax>
                 <CategoryDetail>

                  <Type>
                     <xsl:value-of select="./BillingChargeType"/>
                   </Type>
                   <AnnualStatementLine>
                     <xsl:value-of select="./ASLCode"/>
                   </AnnualStatementLine>
                   <Amount>
                     <xsl:value-of select="format-number(sum(key('group', concat(BillingChargeType, Category,ASLCode,EffectiveDate))/DeltaAmount),'#,###.00')"/>
                   </Amount>
                   <ChangeEffectiveDate>
                     <xsl:value-of select="./EffectiveDate"/>
                   </ChangeEffectiveDate>

                </CategoryDetail>
               </Tax>

          </xsl:when>
           <xsl:when test="./Category ='fee'">            
               <fee>
                 <CategoryDetail>

                  <Type>
                     <xsl:value-of select="./BillingChargeType"/>
                   </Type>
                   <AnnualStatementLine>
                     <xsl:value-of select="./ASLCode"/>
                   </AnnualStatementLine>
                   <Amount>
                     <xsl:value-of select="format-number(sum(key('group', concat(BillingChargeType, Category,ASLCode,EffectiveDate))/DeltaAmount),'#,###.00')"/>
                   </Amount>
                   <ChangeEffectiveDate>
                     <xsl:value-of select="./EffectiveDate"/>
                   </ChangeEffectiveDate>
                 </CategoryDetail>
               </fee>            
           </xsl:when>
         </xsl:choose>
       </xsl:for-each>
     </ns0:Categories>
   </xsl:template>
 </xsl:stylesheet>

答案 1 :(得分:0)

您可以在地图中使用全局变量,在方法上方的脚本functoid中声明列表或数组。您可以使用它来跟踪已经映射的类别。

该方法将返回一个布尔值,您可以将其连接到输出节点premium和tax。它接受类别,检查集合,如果找到则返回false,否则添加它并返回true。

System.Collections.Generic.List<string> categories;

public string AddToArray(string cat)
{
    if (categories == null)
    {
        categories = new System.Collections.Generic.List<string>();
    }

    if(!categories.Contains(cat))
    {
        categories.Add(cat);
        return true;
    }

    return false;
 }