Solved

XSLT - Loop through unknown node names

Posted on 2009-05-11
5
554 Views
Last Modified: 2013-11-18
My XSLT/XPATH is a bit rusty. The sample xml contains several <price> elements and I  need to figure out how sum up these values, but only the CHEAPEST of each service type.
 
So far, i've done this:

<xsl:for-each select="AvailabilitySearch/Services">
      <xsl:value-of select="sum(child::*/child::*/child::Price)"/>
</xsl:for-each>

This gets me the sum of all the price elements. However, I need to extend this to:
1/ Only Sum up the cheapest of each service i.e. the cheapest book, the cheapest cd, the chepest game
2/ Be able to skip a specified service e.g. Don't include the Books in the calculation, only sum up the cheapest cd and game.

To be able to sum up only the cheapest, i'm guessing that I should be doing a sort. As a test, I tried this:
<xsl:for-each select="child::*/child::*">
            <xsl:sort data-type="number" order="ascending" select="Price"/>
      <xsl:value-of select="Price"/>
      <br/>
</xsl:for-each>

However this sorts the WHOLE of the results by price, rather than the services individually.

Any help would be great. If anything isn't clear please let me know and i'll try and explain further.

Thanks.
<AvailabilitySearch>

	<Services>

		<Books>

			<Book id="1">

				<Name>Book 1</Name>

				<Code>1234</Code>

				<Price>10</Price>

			</Book>

			<Book id="1">

				<Name>Book 1</Name>

				<Code>5678</Code>

				<Price>20</Price>

			</Book>

		</Books>

		<CDS>

			<CD>

				<Name>CD 1</Name>

				<Code>9101112</Code>

				<Price>5</Price>

			</CD>

			<CD>

				<Name>CD 2</Name>

				<Code>13141516</Code>

				<Price>7</Price>

			</CD>

		</CDS>

                                           <Games>

			<Game>

				<Name>Game 1</Name>

				<Price>39.99</Price>

			</Game>

			<Game>

				<Name>Game 2</Name>

				<Price>42.99</Price>

			</Game>

			<Game>

				<Name>Game 3</Name>

				<Price>24.99</Price>

			</Game>

		</Games>

	</Services>

</AvailabilitySearch>

Open in new window

0
Comment
Question by:hendrix500
  • 4
5 Comments
 
LVL 60

Assisted Solution

by:Geert Bormans
Geert Bormans earned 200 total points
ID: 24356019
Here is how to loop only the cheapest in their kind
<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:template match="Services">

        <xsl:for-each select="*/*[not(Price > preceding-sibling::*/Price)][not(Price > following-sibling::*/Price)][1]">

            <xsl:copy-of select="."/>

        </xsl:for-each>

    </xsl:template>

</xsl:stylesheet>

Open in new window

0
 
LVL 60

Assisted Solution

by:Geert Bormans
Geert Bormans earned 200 total points
ID: 24356041
Here is your sum
<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:template match="Services">

        <xsl:value-of select="sum(*/*[not(Price > preceding-sibling::*/Price)][not(Price > following-sibling::*/Price)][1]/Price)"/>

     </xsl:template>

</xsl:stylesheet>

Open in new window

0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 200 total points
ID: 24356057
excluding books
<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

    <xsl:template match="Services">

        <xsl:value-of select="sum(*[not(self::Books)]/*[not(Price > preceding-sibling::*/Price)][not(Price > following-sibling::*/Price)][1]/Price)"/>

     </xsl:template>

</xsl:stylesheet>

Open in new window

0
 
LVL 1

Author Closing Comment

by:hendrix500
ID: 31580197
Perfect. Thanks for your quick answer.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 24365549
welcome
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Unattended XML settings 4 103
cookies analysis tools 2 73
Re-position the objects 7 99
Adjust the position 3 50
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now