Solved

How do I use XLST to count items in one XML document to be used in new XML document?

Posted on 2007-12-04
3
425 Views
Last Modified: 2013-11-18
Hi all,
  I have one XML file that contains a list of items and I need to create a second XML file that only shows an item name and the number of times it occured in the original file. My original XML file looks like this:
<ProductResponse>
      <ns1:Products>
            <ns1:Product>
                  <ns1:ID>001</ns1:ID>
                  <ns1:Name>Dodge Ball</ns1:Name>
                  <ns1:Price>14.87</ns1:Price>
                  <ns1:Rating>PG-14</ns1:Rating>
                  <ns1:Category>Comedy</ns1:Category>
                  <ns1:Company>Blockbuster</ns1:Company>
            </ns1:Product>
      </ns1:Products>
      <ns1:Products>
            <ns1:Product>
                  <ns1:ID>002</ns1:ID>
                  <ns1:Name>Dodge Ball</ns1:Name>
                  <ns1:Price>10.99</ns1:Price>
                  <ns1:Rating>R</ns1:Rating>
                  <ns1:Category>Comedy</ns1:Category>
                  <ns1:Company>Netflix</ns1:Company>
            </ns1:Product>
      </ns1:Products>
      <ns1:Products>
            <ns1:Product>
                  <ns1:ID>003</ns1:ID>
                  <ns1:Name>Apocalypse Now</ns1:Name>
                  <ns1:Price>15.99</ns1:Price>
                  <ns1:Rating>R</ns1:Rating>
                  <ns1:Category>Drama</ns1:Category>
                  <ns1:Company>Netflix</ns1:Company>
            </ns1:Product>
      </ns1:Products>
</ns1:ProductResponse>

Based on this, I would like to create a new XML file that has the following format:
<MovieCount>
      <CompanyName>Blockbuster</CompanyName>
      <NumberOfMovies>1</NumberOfMovies>
</MovieCount>
<MovieCount>
      <CompanyName>Netflix</CompanyName>
      <NumberOfMovies>2</NumberOfMovies>
</MovieCount>

I would appreciate any help on comming up with an appropriate XSL to transform the first file into the second.
Thanks
0
Comment
Question by:piratepatrol
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 20405338
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    xmlns:ns1="internal"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
     exclude-result-prefixes="ns1">
    <xsl:key name="prod-by-comp" match="ns1:Product" use="ns1:Company"/>
    <xsl:output indent="yes"/>
    <xsl:template match="ns1:ProductResponse">
        <MovieCounts>
            <xsl:for-each select="ns1:Products/ns1:Product[generate-id() = generate-id(key('prod-by-comp', ns1:Company)[1])]">
                <MovieCount>
                    <CompanyName><xsl:value-of select="ns1:Company"/></CompanyName>
                    <NumberOfMovies><xsl:value-of select="count(key('prod-by-comp', ns1:Company))"/></NumberOfMovies>
                </MovieCount>
            </xsl:for-each>
        </MovieCounts>
    </xsl:template>

</xsl:stylesheet>

This uses muenchian grouping
which is explained very well here
http://www.jenitennison.com/xslt/grouping/muenchian.html

note that you XML source is not wellformed,
since it doesn't declare the ns1 namespace
In my XSLT I set the ns1 namespace to "internal", but you need to replace that with the correct namespace URI

cheers

geert
0
 
LVL 3

Author Closing Comment

by:piratepatrol
ID: 31412629
Geert,

I apologize sincerely for forgetting to close this post and rewarding you the points you surely deserve.  Thank you so much for your assistance, my friend.

Merry Christmas and Happy New Year!


Jazon
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 20537172
Hi Jazon,

you are welcome. Merry Christmass and Happy Newyear for you as well

Geert
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
.net and XML report from SCCM 5 80
Run a Radio Station (Streaming media) with web interface 10 85
Unexpected End of File 11 86
object method as an event handler 8 74
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

732 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