Looping problem with mapper

Home Page Forums BizTalk 2004 – BizTalk 2010 Looping problem with mapper

Viewing 2 reply threads
  • Author
    Posts
    • #19762

      Hello,
       
      I need to make simple transformation to my message and I’m trying to make it with mapper, but with little success.
      The scenario is:
      xml I got as input is like:
      <Root>
            <Offer>
                 <Key>aaa</Key>
                 <field1>a1</field1>
                 <field2>b1</field2>
            </Offer>
            <Offer>
                 <Key>aaa</Key>
                 <field1>a2</field1>
                 <field2>b2</field2>
            </Offer>
            <Offer>
                 <Key>bbb</Key>
                 <field1>a3</field1>
                 <field2>b3</field2>
            </Offer>
      </Root>

      And the format I need to transform it is:
      <Root>
          <Offer>
             <Key>aaa<Key>
             <Line>
               <Field1>a1</Field1>
               <Field2>b1</Field2>
             </Line>
             <Line>
               <Field1>a2</Field2>
               <Field2>b2</Field2>
             </Line>
          </Offer>
          <Offer>
             <Key>bbb<Key>
             <Line>
               <Field1>a2</Field1>
               <Field2>b3</Field2>
             </Line>
          </Offer>   
      </Root>

      So I need to make new Offer element for each distinct values in Key field and put those Field1 and Field2 under it.
      Can I make this somehow with mapper or do I need to use custom xslt or something else?
      I’m new with biztalk, so I dont know if this is simple task and I just dont know how to use that mapper right,
      but I have tried to get this done some time now, so any help/suggestion is appreciated 🙂 

       

      Woltor

    • #19765

       Hi Woltor,

      My mapper understanding level is 300 level at best, but I think there is a good chance that you can solve your problem using the out-of-the-box functoids.  I would look into using the Table Looping and Table Extractor functoids.  If that doesn’t work then you can definitely do it with custom XSLT.  Hope this helps!

    • #19766

      Hi –

      You actually CAN do this with the mapper. I don;t have time to spell it out for you but the basic methodology is to use looping so that each line has ALL item iterations and you use the logicals to turn off the ones that are inappropriate for that iteration; i.e. generate a false unless the line items are equal to “this” item.  The “this item” is going to be determined by an outer for each (looping).

       

       

       

       

      • #19774

         Here is a custom Xslt that will perform the map required

         <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 version=1.0>
        <
        xsl:output omit-xml-declaration=yes method=xml version=1.0 />
        <
        xsl:key name=OfferKey match=Offer use =Key/>
        <
        xsl:template match=/>
        <
        xsl:apply-templates select=/Root />
        </
        xsl:template>
        <
        xsl:template match=/Root>
        <
        xsl:element name=Root>
        <
        xsl:for-each select=Offer>
        <
        xsl:variable name=group select =key(‘OfferKey’, Key) />
        <
        xsl:if test=generate-id($group[1]) = generate-id()>
        <
        xsl:element name=Offer>
        <xsl:element name=Key>
        <
        xsl:value-of select =Key/>
        </
        xsl:element>
        <
        xsl:for-each select=$group>
        <
        xsl:element name=Line>
        <
        xsl:element name=Field1>
        <
        xsl:value-of select=field1/>
        </
        xsl:element>
        <
        xsl:element name=Field2>
        <
        xsl:value-of select=field2/>
        </
        xsl:element>
        </
        xsl:element>
        </
        xsl:for-each>
        </
        xsl:element>
        </
        xsl:if>
        </
        xsl:for-each>
        </
        xsl:element>
        </
        xsl:template>
        </
        xsl:stylesheet>

         

         

        • #19778

          Once he implements custom XSLT, he loses the ability to maintain the map in the mapping tool, correct?

           

          • #19780

             Yes, that is correct. The custom Xslt file overrides all content within the mapping grid. 

            Disadvantages: No GUI mapper and you have to learn Xslt

            Advantages: Line by line debugging with breakpoints using Xslt, Handle more complicated mapping scenarios, More efficient maps, You can use templates, better structured output,

            You can use Xslt maps for message validation-even in the pipeline by having a choice of output i.e.  the original message or an error message

            I still use the mapper but only for simple maps.

            • #19792

              But doesn’t not using a mapping tool defeat the purpose of having a mapping tool?

              • #19797

                Having one tool to do a job is fine, but if that tool can’t do what you want it to do then you need another.

                I find with really complicated maps the Mapper is really hard to comprehend. Given a page of obtuse functoids and lines disappearing off the page, continual scrolling and opening functoids takes forever to untangle a map you have not seen. Some times you just cannot do what you want with the mapper.  

                I use the mapper for simple maps, straight field to field mapping, with a few simple functoids. When a map gets complicated I use Xslt – I find it much faster, more predictable, faster to debug, with more efficient Xslt.

                 

                 

                • #19808

                  1. But the tool can do what you want. So why use XSLT instead of the tool?

                  2. WIth more experience with the mapper, you get better at recognizing groups of functoids and what they do.

                  3. In writing pure XSLT, you have to copy your xpaths from another tool or type them, which is more prone to error.

                  4. On complicated schemas like Edifact and X12, you have segments and elements repeatedly reused throughout the document. Without the visual representation from the mapper, it becomes harder to that you have the right node.

                  So there are some good reasons to use the mapping tool.

                  • #19814

                    On the plus side choosing XSLT instead of the mapper, I should acknowledge that there are several things that the Biztalk Mapper does not do easily. So from a capability standpoint, you can do certain things far more easily with the straight XSLT, as we see in this thread’s example.  In my solution to preserving the graphical mapping, I would have had to cache keys and use external scripting – esentially using a lot of tricks. Greg’s XSLT solution is a far more elegant in directly solving the problem without tricks to get around limitations.

                    The latest issue of BizTalk HotRot has a detailed article on the Smart use of straight XSLT.  There are a number of considerations to factor into the decision, such as performance, large file support, etc.  I highly recommend to anyone interested in this thread to read the article.

                    Article by Scott Colestock on P28 of the current issue:  http://cid-b6c859f7a5f75e63.skydrive.live.com/self.aspx/Public/Q2FY08_biztalk.pdf

                    Biztalk hotrod main site:  http://biztalkhotrod.com/default.aspx

        • #19801

          Hello

           

          Thanks for the xsl, I got it working now 🙂

          -Woltor

Viewing 2 reply threads
  • The forum ‘BizTalk 2004 – BizTalk 2010’ is closed to new topics and replies.