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
423 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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

828 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