1

I am again providing you the requirement like this. Could you please look into this?

<xml>
<test>
<BookID>
  <BookID1>
    <BookID2>
      0061AB
    </BookID2>
  </BookID1>
</BookID>
<amount>
  16
</amount>
</test>
<test>
<BookID>
  <BookID1>
    <BookID2>
      0062CD
    </BookID2>
  </BookID1>
</BookID>
<amount>
  2
</amount>
</test>
<test>
<BookID>
  <BookID1>
    <BookID2>
      0061AB
    </BookID2>
  </BookID1>
</BookID>
<amount>
  2
</amount>
</test>
</xml>

here According to the equal value of BookID, I want to add the amount value.....like for above example, if value of BookID is 0061AB, then the value of amount should be 18.

Edit (Pasted to Answer)

Output should be like this

<xml>
  <test>
    <BookID>
      <BookID1>
        <BookID2>
          0061AB
        </BookID2>
      <BookID1>
    </BookID>
    <amount>
      18
    </amount>
  </test>
  <test>
    <BookID>
      <BookID1>
        <BookID2>
          0062CD
        </BookID2>
      <BookID1>
    </BookID>
    <amount>
      2
    </amount>
  </test>
</xml>
Dipta
  • 13
  • 6
  • Dipta - please don't try to edit answers with new information - Use the comments to interact with answerers. Also, please try to take the time to provide all relevant information in your original question. – StuartLC Oct 05 '12 at 06:56

1 Answers1

0

I'm assuming you mean the sum of the amounts. You can do with with xpath like the below

  <xsl:template match="/xml">
    <xsl:value-of select="sum(test[normalize-space(BookID)='0061AB']/amount)"/>
  </xsl:template>

Edit

You can parameterize the BookID by using a variable, or by using a call-template as per my answer to your previous question

The following stylesheet

  <xsl:template match="/xml">
    <xsl:variable name="bookID" select="'0061AB'"/>
    <xsl:value-of select="sum(test[normalize-space(BookID)=$bookID]/amount)"/>
  </xsl:template>

Edit #2

It seems that you are now after grouping distinct elements and aggregating the totals.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>

    <!-- the identity template -->
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()" />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="test">
        <xsl:variable name="bookID" select="normalize-space(BookID/text())" />
        <xsl:if test="not(preceding-sibling::test[normalize-space(BookID/text())=$bookID])">
            <test>
                <xsl:apply-templates />
            </test>
        </xsl:if>
        <!--Else `eat` the duplicate test node-->
    </xsl:template>

    <xsl:template match="amount">
        <amount>
            <xsl:variable name="bookID" select="normalize-space(../BookID/text())" />
            <xsl:value-of select="sum(//test[normalize-space(BookID/text())=$bookID]/amount)"/>
        </amount>
    </xsl:template>
</xsl:stylesheet>

Produces the output

<xml>
    <test>
        <BookID>
            0061AB
        </BookID>
        <amount>18</amount>
    </test>
    <test>
        <BookID>
            0062CD
        </BookID>
        <amount>2</amount>
    </test>

</xml>

Please can you take the time to read through the FAQ

Edit 3

This stylesheet will meet your latest requirement.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>

    <!-- the identity template -->
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()" />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="test">
        <xsl:variable name="bookID" select="normalize-space(BookID/BookID1/BookID2/text())" />
        <xsl:if test="not(preceding-sibling::test[normalize-space(BookID/BookID1/BookID2/text())=$bookID])">
            <test>
                <xsl:apply-templates />
            </test>
        </xsl:if>
        <!--Else `eat` the duplicate test node-->
    </xsl:template>

    <xsl:template match="amount">
        <amount>
            <xsl:variable name="bookID" select="normalize-space(../BookID/BookID1/BookID2/text())" />
            <xsl:value-of select="sum(//test[normalize-space(BookID/BookID1/BookID2/text())=$bookID]/amount)"/>
        </amount>
    </xsl:template>
</xsl:stylesheet>
Community
  • 1
  • 1
StuartLC
  • 104,537
  • 17
  • 209
  • 285
  • (Comment from Dipta)Thanks for you r post. Here the BookID value comming dynamically and I need to check for equal BookID value and i need to add the amount of the equal BookID. Would this logic work for this or any change is needed for the above logic? – StuartLC Oct 05 '12 at 06:49
  • Actually i am not much familar with this site.So this kind of mistake happenned.Please ignore it – Dipta Oct 05 '12 at 07:20
  • okay.....I am implementing the logic in my xsl that you have suggesting me....thnx – Dipta Oct 05 '12 at 09:50
  • @Dipta A common way of grouping is to exclude nodes which have the same key as one which has been previously processed, i.e. if you look above, you will see `not(preceding-sibling::{Test for Duplication here})` – StuartLC Oct 05 '12 at 10:37
  • @nonb..yes it is answer of this question. – Dipta Oct 07 '12 at 04:55
  • @nonnb....Could you please look into this? i am providing you another requirement. – Dipta Oct 07 '12 at 04:56
  • @Dipta I've answered 2 questions, and you haven't upvoted or marked either as answered, so as it stands, there is little incentive for me to answer your questions. – StuartLC Oct 07 '12 at 07:28
  • I don't know how to mark it as answer. previously I have also told you that I am not much familar with the site.If you tell me the way how to mark it as answer,then I can do that. I have also mentioned in my comment those as answer. thanks – Dipta Oct 08 '12 at 03:14
  • @Dipta - Updated again. Note that this isn't really the way that StackOverflow works - ideally you should get your question ready + right first time, and then if post new questions when you have a new requirement. You should also give your best effort to attempting this yourself - this is the best way to learn new technologies. – StuartLC Oct 08 '12 at 08:46
  • 1
    Thanks for your information.This really worked.I will keep in mind further.Thanks again – Dipta Oct 08 '12 at 13:00