开发者

Mapping collection as a string to a target node

开发者 https://www.devze.com 2023-02-21 10:04 出处:网络
I am using biztalk 2009 and need help with mapping. I ha开发者_运维问答ve input like: <root>

I am using biztalk 2009 and need help with mapping. I ha开发者_运维问答ve input like:

<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>

I want to map the collection of products to a target element as a string of xml that looks like this: <products><product type="1" code="ab" /><product type="2" code="cd" /></products>

I have found a solution using custom xslt but I dont want to use it as we have found it to be very fickle. Is there any functoids that could do this for me with some custom scripting? I am also a c sharp dev thanks!


This is completely doable out of the box with a simple map.

Here is the soure XML file:

<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>

Here is the source 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="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>

Here is the target 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>

And here is the map:

Mapping collection as a string to a target node

And here is the output:

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

Armed witht his result, you can follow one of two suggestions outlined by Mark Brimble in his blog.

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


I am sorry to say this but when mapping gets too involved and there is no obvious way to do this in the mapper I just fall back on a .net helper method inside assign message which will build the output message.

The helper method can take a biztalk message as an argument of type XLANGMessage and return a type of XMLDocument which will be converted to your target message type, providing the xml inside renders the type correctly.

For example:

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;
}

It would be trivial to do this inside .net so just take it into .net and do it. Sorry to all the mapping gurus out there!

0

精彩评论

暂无评论...
验证码 换一张
取 消