0

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

源架构看起来像这样:

<?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. 输出应仅具有按类别、BillingChargeType、ALSCode 和 EffectiveDate 不同的记录集。5. 输出应具有目标节点“Amount”中源节点“DeltaAmount”按类别、BillingChargeType、ALSCode 和 EffectiveDate 的聚合值

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

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

以上 3 个节点有两个 Coverage 记录集。这两者的区别是

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

在预期的输出中,您将看到只有一个被映射为 DeltaAmount 节点 = 16。

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

感谢您查看此内容,感谢您的所有帮助!穆昆德

4

2 回答 2

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>
于 2012-05-22T19:30:29.633 回答
0

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

该方法将返回一个布尔值,您可以将其连接到输出节点溢价和税金。它接受类别,检查集合,如果找到返回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;
 }
于 2012-05-17T19:11:29.207 回答