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

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
Extract XML Data from using TSQL 5 53
Windows Tool to Build Android and iOS App 3 74
Configure a Bean in an XML file 4 31
Help needed with Powershell  XML to MySQL 5 37
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…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

786 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