Grouping by element

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sandeep Bonde
    New Member
    • Jul 2010
    • 2

    #1

    Grouping by element

    Is there any way in XSLT to achive below XML in required output

    Want to transform element on same level with different element name.

    XML:-

    Code:
    <Root>
    <Parent>
    <Make>Make</Make>
    <Para>Line 1 $100.00</Para>
    <Para>Line 1 $200.00</Para>
    <Para>Line 1 $300.00</Para>
    <Make>Make</Make>
    <Para>Line 1 $500.00</Para>
    <Para>Line 1 $600.00</Para>
    </Parent>
    </Root>
    Required Output:-
    Code:
    <Root>
    <Parent>
    <Make>Make</Make>
    <Para_1>Line 1 $100.00</Para_1>
    <Para_2>Line 1 $200.00</Para_2>
    <Para_1>Line 1 $300.00</Para_1>
    <Make>Make</Make>
    <Para_1>Line 1 $500.00</Para_1>
    <Para_2>Line 1 $600.00</Para_2>
    </Parent>
    </Root>
    Last edited by Dormilich; Jul 28 '10, 06:19 AM. Reason: Please use [code] tags when posting code
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    if you have the logic, when to attach _1 and _2, this should be possible. I’d use <xsl:element> for that.

    Comment

    • Sandeep Bonde
      New Member
      • Jul 2010
      • 2

      #3
      Logic is i want make group of <para> and increment them with variable in my sample i group them by 2 in group it appends _1 to first and _2 to second para.

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        What's the logic for the 3rd group?
        Do you just alternate 1 and 2?
        Code:
        <xsl:template match="Para">
          <xsl:variable name="name">
            <xsl:text>Para_</xsl:text>
            <xsl:choose>
              <xsl:when test="position() mod 2 = 1">1</xsl:when>
              <xsl:otherwise>2</xsl:otherwise>
            </xsl:choose>
          </xsl:variable>
          <xsl:element name="{$name}">
            <xsl:value-of select="."/>
          </xsl:element>
        </xsl:template>

        Comment

        Working...