Question

I am trying to use Muenchian grouping to select a node set containing the first occurrences of each movie in the following XML document but I am not able to return anything...

<poll>
<ballot id="b1">
   <movie>NATIONAL LAMPOON'S ANIMAL HOUSE (1978)</movie>
   <movie>SILVER STREAK (1976)</movie>
   <movie>SOME LIKE IT HOT (1959)</movie>
</ballot>
<ballot id="b2">
   <movie>MODERN TIMES (1936)</movie>
   <movie>NATIONAL LAMPOON'S ANIMAL HOUSE (1978)</movie>
   <movie>SHAMPOO (1975)</movie>
</ballot>
</poll>

And the following is my xsl...

<xsl:template match="/">    
    <xsl:for-each select="//ballot[generate-id()=generate-id(key('movies',.)[1])]">
        <xsl:value-of select="movie" />
    </xsl:for-each>
</xsl:template>
Was it helpful?

Solution

You are very close.

First, you need to update your key:

<xsl:key name="movies" match="movie" use="." />

By putting movie in the use= you were saying "use movie that is under movie" which isn't correct.

Next, your for-each is setting the context to ballot which means, based on your input, that it would run only twice. I believe you actually wanted the context to be movie so it would run over each movie that matched your key. That would look like the below:

<xsl:for-each select="//ballot/movie[generate-id()=generate-id(key('movies',.)[1])]">
  <tr><xsl:value-of select="." /></tr>
</xsl:for-each>

As a side note, you will also want to fix the way the table is made since you are outputting only <tr> for each movie with no <td> or anything similar. But I'm sure you would get to that once this is fixed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top