Transforming XML problem....Please help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • navneet0909
    New Member
    • Aug 2007
    • 7

    Transforming XML problem....Please help

    hello,
    i m new to the enthralling world of xml,xslt so guys pls help.
    What will be the xsl file of the following.I have an xml input

    <root>
    <element name="sam" number="1"/>
    <element name="sam" number="2"/>
    <element name="sam" number="3"/>
    <element name="bob" number="5"/>
    <element name="bob" number="7"/>
    </root>

    and the output which i want is

    <root>
    <element name="sam" number="6"/>
    (1+2+3 =6 for attribute name="sam")------comment
    <element name="" number="1"/>
    <element name="" number="2"/>
    <element name="" number="3"/>

    <element name="bob" number="12"/>
    (5+7 = 12 for attribute name="bob")------comment
    <element name="" number="5"/>
    <element name="" number="7"/>
    </root>

    As u can see, whenever the value of name attribute changes a new row should be added before, with the sum of their number attributes, and until the next name attribute change the name attributes must be blanked out...
    Actually this will be used for grouping in a list.
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    Muenchian grouping:http://www.jenitenniso n.com/xslt/grouping/muenchian.html

    Sorry in a rush, will answer further if you need it in a few hours.

    Comment

    • jkmyoung
      Recognized Expert Top Contributor
      • Mar 2006
      • 2057

      #3
      [CODE=html]<xsl:key name="elementBy Name" match="element" use="@name"/>
      ...

      <xsl:for-each select="element[count(.|key('el eByName', @name)[1]) = 1]">
      <element name="@name" number="{sum(ke y('eleByName', @name)/@number)}"/>
      </xsl:for-each>[/CODE]

      Comment

      • navneet0909
        New Member
        • Aug 2007
        • 7

        #4
        Thanks a million JKMYOUNG...

        But still....
        Almost done.....but... ..


        <xsl:styleshe et version="1.0" xmlns:xsl="http ://www.w3.org/1999/XSL/Transform">
        <xsl:output method="html"/>

        <xsl:key name="eleByName " match="element" use="@name"/>
        <xsl:template match="/root">

        <xsl:for-each select="element[count(.|key('el eByName', @name)[1]) = 1]">
        <element name="@name" number="{sum(ke y('eleByName', @name)/@number)}"/>
        <xsl:value-of select = "@name"/>
        <xsl:value-of select = "sum(key('eleBy Name', @name)/@number)"/>
        <br></br>

        <xsl:for-each select="//element"> (this line needs the correct code)
        <xsl:value-of select = "@number"/>
        <br></br>
        </xsl:for-each>
        </xsl:for-each>

        </xsl:template>
        </xsl:stylesheet>





        This is giving the output

        sam6
        1
        2
        3
        5
        7
        bob12
        1
        2
        3
        5
        7

        What i really want

        sam6
        1
        2
        3
        bob12
        5
        7

        Comment

        • jkmyoung
          Recognized Expert Top Contributor
          • Mar 2006
          • 2057

          #5
          replace //element with
          key('eleByName' , @name)

          Comment

          • navneet0909
            New Member
            • Aug 2007
            • 7

            #6
            <xsl:variable name="name" select="@name"/>
            <xsl:for-each select="//element[@name = $name]">




            this works perfectly fine....Thank you all for the help....

            Comment

            • jkmyoung
              Recognized Expert Top Contributor
              • Mar 2006
              • 2057

              #7
              The method you propose is easier to understand.

              But, why waste the index after you've built it? Using it the 2nd time would help performance as well.

              Comment

              Working...