Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 432
  • Last Modified:

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

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
piratepatrol
Asked:
piratepatrol
  • 2
1 Solution
 
Geert BormansCommented:
<?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
 
piratepatrolAuthor Commented:
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
 
Geert BormansCommented:
Hi Jazon,

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

Geert
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now