 |
| XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

April 20th, 2009, 06:44 AM
|
|
Authorized User
|
|
Join Date: Apr 2009
Posts: 25
Thanks: 5
Thanked 0 Times in 0 Posts
|
|
xsl:Looping more than one time through same node
Hi,
I have a problem in looping in xslt.
It is simple while performing in java but here, in xslt, I have a root node and inside that 4 nodes :
<Rates>
<Rate Guests="2" Units="1" Code="CP" Room="DLX">
<Rate Guests="2" Units="1" Code="RAC" Room="DLX">
<Rate Guests="3" Units="1" Code="CP" Room="DLX">
<Rate Guests="3" Units="1" Code="RAC" Room="DLX">
</Rates>
Now, I need to traverse in all these nodes and check @Code and @Room values which must be same.
Then, I need to add @Guests and @Units values.
I will exclude those nodes with @Code="RAC"
So, my result set will only be a single node like this:
<Rate Guests="5" Units="2" Code="CP" Room="DLX">
All others need to be deleted.
Hope you understand the problem.
Please give your suggestions urgently.
Thanks,
Mary
|
|

April 20th, 2009, 07:11 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
Here is an XSLT 2.0 solution:
Code:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:template match="Rates">
<xsl:copy>
<xsl:for-each-group select="Rate[not(@Code = 'RAC')]" group-by="concat(@Code, '|', @Room)">
<Rate Guests="{sum(current-group()/@Guests)}"
Units="{sum(current-group()/@Units)}"
Code="{@Code}" Room="{@Room}"/>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
It processes only those 'Rate' elements where the attribute 'Code' is not 'RAC' and then groups on the concatenation of Code and Room attributes to sum up the Guests and Units attributes.
Is that what you want to achieve?
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
|
|
The Following User Says Thank You to Martin Honnen For This Useful Post:
|
|
|

April 20th, 2009, 07:51 AM
|
|
Authorized User
|
|
Join Date: Apr 2009
Posts: 25
Thanks: 5
Thanked 0 Times in 0 Posts
|
|
Hi Martin,
Yes. This is what I wanted and it is working too 
Thank u so much for such a quick solution.
I have tried it.
Thanks a lot !!
Best Regards,
Mary
|
|

April 21st, 2009, 01:30 AM
|
|
Authorized User
|
|
Join Date: Apr 2009
Posts: 25
Thanks: 5
Thanked 0 Times in 0 Posts
|
|
Can u further enhance the solution
Can the code be further enhanced based on some other nodes?
In every Rate node, there is further a node as:
<Rates>
<Rate Guests="2" Units="1" Code="CP" Room="DLX">
<aa>
<Night Amount="2000" Code="CP" Room="DLX">
<Night Amount="2000" Code="CP" Room="DLX">
</aa>
</Rate>
<Rate Guests="3" Units="1" Code="CP" Room="DLX">
<aa>
<Night Amount="3000" Code="CP" Room="DLX">
<Night Amount="1000" Code="CP" Room="DLX">
</aa>
</Rate>
</Rates>
Now, in addition to the previous solution, what i need is 2 <Night> nodes with @Amount of both <Rate> nodes added to their respective counterpart.
Eg.
<Rate Guests="5" Units="2" Code="CP" Room="DLX">
<aa>
<Night Amount="5000" Code="CP" Room="DLX">
<Night Amount="3000" Code="CP" Room="DLX">
</aa>
</Rate>
Please help but in accordance to the previous solution !!
Best Regards,
Mary
|
|

April 21st, 2009, 02:41 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
I didn't reply to your earlier post because unlike Martin, I failed to detect the pattern that showed how your output related to your input. Now it seems that he only spotted part of the pattern, and you are supplying more data in the hope that more of the pattern will be revealed.
Specification-by-example is often a useful technique, but it usually relies on having more than one example. I think at this stage it would be useful for you to stand back and try to define the general rules for how the output should relate to the input, rather than trying to develop them on a case-by-case basis.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
|
|

April 21st, 2009, 02:52 AM
|
|
Authorized User
|
|
Join Date: Apr 2009
Posts: 25
Thanks: 5
Thanked 0 Times in 0 Posts
|
|
Hi Michael !
What Martin suggested is in perfect accordance to what I wanted and actually, I was wrong in providing half of the details on first time.
I have already implemented what Martin suggested and it's working properly!!
Now what I want is, I need to aggregate Night nodes also, in such a way that,
@Amount of 1st <Night> node in 1st <Rate> node adds with @Amount of 1st <Night> node in 2nd <Rate> node.
Similarly, @Amount of 2nd <Night> node in 1st <Rate> node adds with @Amount of 2nd <Night> node in 2nd <Rate> node.
Sorry if you find my way of asking somewhat wrong.
I will improve  .
Thanks,
Mary
|
|

April 21st, 2009, 05:06 AM
|
|
Authorized User
|
|
Join Date: Apr 2009
Posts: 25
Thanks: 5
Thanked 0 Times in 0 Posts
|
|
please answer. solution urgently required.
Thanks for your support.
Regards,
Mary
|
|

April 21st, 2009, 05:52 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
Here is an adapted stylesheet:
Code:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="Rates">
<xsl:copy>
<xsl:for-each-group select="Rate[not(@Code = 'RAC')]" group-by="concat(@Code, '|', @Room)">
<Rate Guests="{sum(current-group()/@Guests)}"
Units="{sum(current-group()/@Units)}"
Code="{@Code}" Room="{@Room}">
<aa>
<xsl:for-each select="aa/Night">
<xsl:variable name="pos" select="position()"/>
<Night Amount="{sum(current-group()/aa/Night[position() eq $pos]/@Amount)}"
Code="{@Code}" Room="{@Room}"/>
</xsl:for-each>
</aa>
</Rate>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
|
|
The Following User Says Thank You to Martin Honnen For This Useful Post:
|
|
|

April 21st, 2009, 07:40 AM
|
|
Authorized User
|
|
Join Date: Apr 2009
Posts: 25
Thanks: 5
Thanked 0 Times in 0 Posts
|
|
Thank u so much Martin.....
I have implemented this too and its working :)
Loads of thanks for your timely help !!
Regards,
Mary
|
|

April 21st, 2009, 06:11 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2007
Posts: 477
Thanks: 10
Thanked 19 Times in 18 Posts
|
|
Nice work Martin!
I'm still trying to wrap my head around XSLT style programming. I wonder if I'll ever be able to put things together that quickly. :)
__________________
-------------------------
Whatever you can do or dream you can, begin it. Boldness has genius, power and magic in it. Begin it now.
-Johann von Goethe
When Two Hearts Race... Both Win.
-Dove Chocolate Wrapper
Chroniclemaster1, Founder of www.EarthChronicle.com
A Growing History of our Planet, by our Planet, for our Planet.
|
|
 |