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
420 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
  • 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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 …
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

707 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

10 Experts available now in Live!

Get 1:1 Help Now