none
BizTalk XSLT - Item # repeat only once for every 'n' SubItemPart RRS feed

  • Question

  • Hi - In my current XSLT for repetitive part number, in the output it PartNumber should be once and SubPart # should be repeating under the same part number, but iam not getting that.

    Please see below Input, my XSLT and Output

    INPUT :

    <ns0:DetailLoop xmlns:ns0="http://P1.DetailLoop">

      <LineItem>

        <PartNumber>ABX-9302</PartNumber>

        <SubPartNumber>aaaa</SubPartNumber>

      </LineItem>

       <LineItem>

        <PartNumber>ABA-1307</PartNumber>

        <SubPartNumber>1307-1981</SubPartNumber>

      </LineItem>

      <LineItem>

        <PartNumber>ABA-1307</PartNumber>

        <SubPartNumber>1307-1982</SubPartNumber>

      </LineItem>

      <LineItem>

        <PartNumber>ABA-1307</PartNumber>

        <SubPartNumber>1307-1983</SubPartNumber>

      </LineItem>

    </ns0:DetailLoop>

    Output :

    <ns0:TargetData xmlns:ns0="http://P1.Schema2">

      <ItemInfo>

        <ItemNumber>ABX-9302</ItemNumber>

        <SubItemInfo>

          <SubItemPart>aaaa</SubItemPart>

        </SubItemInfo>

      </ItemInfo>

      <ItemInfo>

        <ItemNumber>ABA-1307</ItemNumber>

        <SubItemInfo>

          <SubItemPart>1307-1981</SubItemPart>

        </SubItemInfo>

        <SubItemInfo>

          <SubItemPart>1307-1982</SubItemPart>

        </SubItemInfo>

        <SubItemInfo>

          <SubItemPart>1307-1983</SubItemPart>

        </SubItemInfo>

      </ItemInfo>

      <ItemInfo>

        <ItemNumber>ABA-1307</ItemNumber>

        <SubItemInfo>

          <SubItemPart>1307-1981</SubItemPart>

        </SubItemInfo>

        <SubItemInfo>

          <SubItemPart>1307-1982</SubItemPart>

        </SubItemInfo>

        <SubItemInfo>

          <SubItemPart>1307-1983</SubItemPart>

        </SubItemInfo>

      </ItemInfo>

      <ItemInfo>

        <ItemNumber>ABA-1307</ItemNumber>

        <SubItemInfo>

          <SubItemPart>1307-1981</SubItemPart>

        </SubItemInfo>

        <SubItemInfo>

          <SubItemPart>1307-1982</SubItemPart>

        </SubItemInfo>

        <SubItemInfo>

          <SubItemPart>1307-1983</SubItemPart>

        </SubItemInfo>

      </ItemInfo>

    </ns0:TargetData>

    XSLT

    <xsl:for-each select="//s0:DetailLoop/LineItem">

           <xsl:variable name = "CurrentPartID">

                   <xsl:value-of select="PartNumber/text()"/>

           </xsl:variable>

       

            <xsl:if test="//s0:DetailLoop/LineItem[PartNumber!=$CurrentPartID]">

            <!-- Write out an ItemInfo Loop node and an ItemNumber node -->

            <xsl:element name = "ItemInfo">

         

                                    <xsl:element name = "ItemNumber">

                                       <xsl:value-of select="$CurrentPartID"/>

                                    </xsl:element>

                                    <!-- For the current ParentPartID, select Item nodes where the PartNumber is equal to the current part, and the SubPartNumber is not empty -->

                                    <xsl:for-each select = "//s0:DetailLoop/LineItem[PartNumber=$CurrentPartID]">    

                                          <xsl:variable name="ChildPart">

                                                    <xsl:value-of select="SubPartNumber/text()"/>

                                          </xsl:variable>

                                          <!--Write out a SubItemInfo loop and a SubItemPart for the current ChildPart  -->  

                                          <xsl:element name = "SubItemInfo">

                                                    <xsl:element name = "SubItemPart">

                                                       <xsl:value-of select="$ChildPart"/>

                                                    </xsl:element>

                                         </xsl:element>

                                    </xsl:for-each>

           </xsl:element>

          

           </xsl:if>

     </xsl:for-each>


    MBH

    Thursday, April 16, 2015 4:12 PM

Answers

  • You want to group your data based on ItemNumber, you can do this xslt with the xsl:key like this:

             

      <xsl:key name="Parts" match ="LineItem" use="PartNumber"/>

         <xsl:template match="/">
              <xsl:for-each select="//LineItem[generate-id(.)=generate-id(key('Parts', PartNumber)[1])]">
               <xsl:element name = "ItemInfo">
                  <xsl:element name = "ItemNumber">
                 <xsl:value-of select="PartNumber/text()"/>
                   </xsl:element>
                   <xsl:element name = "SubItemInfo">
              <xsl:for-each select="key('Parts', PartNumber)">
                <xsl:element name = "SubItemInfo">
                          <xsl:value-of select ="SubPartNumber/text()"/>
                </xsl:element>
            </xsl:for-each>
            </xsl:element>
          </xsl:element>
        </xsl:for-each>

      </xsl:template>

    • Marked as answer by Angie Xu Thursday, April 23, 2015 9:43 AM
    Friday, April 17, 2015 12:27 PM

All replies

  • I should be getting output just like below : the rest of it should not be repeating.

    What am I doing wrong that , ItemInfo node(in output) is repeating ?How can I correct it ?

    <ns0:TargetData xmlns:ns0="http://P1.Schema2">

      <ItemInfo>

        <ItemNumber>ABX-9302</ItemNumber>

        <SubItemInfo>

          <SubItemPart>aaaa</SubItemPart>

        </SubItemInfo>

      </ItemInfo>

      <ItemInfo>

        <ItemNumber>ABA-1307</ItemNumber>

        <SubItemInfo>

          <SubItemPart>1307-1981</SubItemPart>

        </SubItemInfo>

        <SubItemInfo>

          <SubItemPart>1307-1982</SubItemPart>

        </SubItemInfo>

        <SubItemInfo>

          <SubItemPart>1307-1983</SubItemPart>

        </SubItemInfo>

      </ItemInfo>


    MBH

    Thursday, April 16, 2015 7:21 PM
  • this is my Outbound Schema :

    <?xml
    version="1.0" encoding="utf-16"
    ?>

    - <xs:schema
    xmlns
    ="http://P1.Schema2" xmlns:b="http://schemas.microsoft.com/BizTalk/2003" targetNamespace="http://P1.Schema2"
    xmlns:xs
    ="http://www.w3.org/2001/XMLSchema">
    - <xs:element
    name
    ="TargetData">
    - <xs:complexType>
    - <xs:sequence>
    - <xs:element
    maxOccurs
    ="unbounded" name="ItemInfo">
    - <xs:complexType>
    - <xs:sequence>
      <xs:element name="ItemNumber" type="xs:string"
    />
    - <xs:element
    maxOccurs
    ="unbounded" name="SubItemInfo">
    - <xs:complexType>
    - <xs:sequence>
      <xs:element name="SubItemPart" type="xs:string"
    />
      </xs:sequence>
      </xs:complexType>
      </xs:element>
      </xs:sequence>
      </xs:complexType>
      </xs:element>
      </xs:sequence>
      </xs:complexType>
      </xs:element>
      </xs:schema>


    MBH

    Thursday, April 16, 2015 9:52 PM
  • You want to group your data based on ItemNumber, you can do this xslt with the xsl:key like this:

             

      <xsl:key name="Parts" match ="LineItem" use="PartNumber"/>

         <xsl:template match="/">
              <xsl:for-each select="//LineItem[generate-id(.)=generate-id(key('Parts', PartNumber)[1])]">
               <xsl:element name = "ItemInfo">
                  <xsl:element name = "ItemNumber">
                 <xsl:value-of select="PartNumber/text()"/>
                   </xsl:element>
                   <xsl:element name = "SubItemInfo">
              <xsl:for-each select="key('Parts', PartNumber)">
                <xsl:element name = "SubItemInfo">
                          <xsl:value-of select ="SubPartNumber/text()"/>
                </xsl:element>
            </xsl:for-each>
            </xsl:element>
          </xsl:element>
        </xsl:for-each>

      </xsl:template>

    • Marked as answer by Angie Xu Thursday, April 23, 2015 9:43 AM
    Friday, April 17, 2015 12:27 PM