将集合作为字符串映射到目标节点

时间:2011-03-31 19:40:36

标签: biztalk biztalk-2009

我正在使用biztalk 2009并需要帮助进行映射。我的输入如下:

<root>
    <shop>
        <product>
            <type>1</type>
            <code>ab</code>
            <desc></desc>
        </product>
        <product>
            <type>2</type>
            <code>cd</code>
            <desc></desc>
        </product>
    </shop>
    <address />
    <names />
</root>

我想将产品集合映射到目标元素,作为xml的字符串,如下所示: <products><product type="1" code="ab" /><product type="2" code="cd" /></products>

我找到了使用自定义xslt的解决方案,但我不想使用它,因为我们发现它非常易变。是否有任何functoid可以通过一些自定义脚本为我做这个?我也是一个敏锐的开发人员谢谢!

2 个答案:

答案 0 :(得分:2)

使用简单的地图,这是完全可行的。

这是源XML文件:

<root>
    <shop>
        <product>
            <type>1</type>
            <code>ab</code>
            <desc></desc>
        </product>
        <product>
            <type>2</type>
            <code>cd</code>
            <desc></desc>
        </product>
    </shop>
    <address />
    <names />
</root>

以下是源架构:

<?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns:b="http://schemas.microsoft.com/BizTalk/2003" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="root">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="shop">
          <xs:complexType>
            <xs:sequence>
              <xs:element minOccurs="1" maxOccurs="unbounded" name="product">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="type" type="xs:string" />
                    <xs:element name="code" type="xs:string" />
                    <xs:element name="desc" type="xs:string" />
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="address">
          <xs:complexType />
        </xs:element>
        <xs:element name="names">
          <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:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="products">
    <xs:complexType>
      <xs:sequence>
        <xs:element minOccurs="1" maxOccurs="unbounded" name="product">
          <xs:complexType mixed="true">
            <xs:attribute name="type" type="xs:string" />
            <xs:attribute name="code" type="xs:string" />
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

这是地图:

BizTalk Map for Source and destination schemas

这是输出:

<products>
    <product type="1" code="ab" /> 
    <product type="2" code="cd" /> 
</products>

根据他的结果,您可以按照Mark Brimble在他博客中提出的两条建议之一。

How to copy the entire node to element of string type in a map

答案 1 :(得分:0)

我很遗憾地说这个,但是当映射过于复杂并且在mapper中没有明显的方法可以做到这一点时,我只需要在分配消息内部的.net帮助器方法中构建输出消息。

辅助方法可以将biztalk消息作为XLANGMessage类型的参数,并返回一种XMLDocument,它将转换为您的目标消息类型,前提是xml内部正确呈现类型。

例如:

public static XmlDocument HelperMethod (XLANGMessage message)
{
    var sourceType = (SourceType)message[0].RetrieveAs(typeof(SourceType));
    var targetType = new TargetType();

    // ... Do target type population and serialization to XmlDocument here

    return targetAsXmlDoc;
}

在.net中执行此操作将是微不足道的,所以只需将其带入.net并执行此操作即可。对不起那里的所有地图专家!