Access Attribute from Current Group Part 2

60 views Asked by At

A follow up to this question Access attribute from within current-grouping-key() xslt

I have my thousands of movies and many titles are the same -- sample code here

    <mediaList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:noNamespaceSchemaLocation="mediaList.xsd">
   <movie id="1125898" dateCreated="2014-04-12" lastModified="2014-05-23">
      <title>127 Hours</title>
   </movie>
   <movie id="1155300" dateCreated="2014-04-12" lastModified="2014-05-23">
      <title>Lord Jim</title>
   </movie>
   <movie id="866019" dateCreated="2014-09-18">
      <title>Othello</title>
</movie>
   <movie id="811875" dateCreated="2014-04-12" lastModified="2014-05-23">
      <title>Escape from New York</title>
   </movie>
   <movie id="1340523" dateCreated="2014-04-12" lastModified="2014-05-23">
      <title>Escape from L.A.</title>
   </movie>
   <movie id="1108660" dateCreated="2014-04-13" lastModified="2014-05-23">
      <title>Black Cat Run</title>
   </movie>
   <movie id="910246" dateCreated="2014-09-17">
      <title differentiator="1990">Othello</title>
</movie>
   <movie id="1324917" dateCreated="2014-04-13" lastModified="2014-05-28">
      <title>Police Story 2</title>
   </movie>
   <movie id="949534" dateCreated="2014-04-13" lastModified="2014-05-28">
      <title>Rambo: First Blood Part II</title>
   </movie>
  <movie id="910900" dateCreated="2014-09-14">
      <title differentiator="1965">Othello</title>
</movie>
</mediaList>

I make an effort when I enter a movie with a repeated title to add the @differentiator to the so as to keep them unique when sort -- but sometimes I may miss one, so I'm trying to write a template to find all titles which are the same, and then print ANY which do not ALREADY have a @differentiator.

I CAN do it with

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0">
    <xsl:output method="html" indent="yes"/>


    <xsl:template match="/">
        <html>
            <body>
                <ul>
 <xsl:apply-templates select="mediaList/movie[title[not(@differentiator)] = ./following-sibling::movie/title[not(@differentiator)]] | mediaList/movie[title[not(@differentiator)] = ./preceding-sibling::movie/title[not(@differentiator)]]"/>
</ul>
</body>
</html>
</xsl:template>


    <xsl:template match="movie">
        <li>
            <xsl:value-of select="concat(title[1],year,' appears multiple times')"/>
        </li>
    </xsl:template>

but with thousands of movies...it's VERY inefficient!!

I know that similar to the linked question, there's SOME sneak of using concat(.,'_',@differentiator)

but the output would be ALL titles which appear more than once, and ALSO do not have @differentiator

please advise!

1

There are 1 answers

1
Martin Honnen On BEST ANSWER

XSLT 2.0 has a grouping instruction so use that with e.g.

<xsl:template match="/">
        <html>
            <body>
                <ul>
                  <xsl:for-each-group select="mediaList/movie" group-by="title">
                     <xsl:apply-templates select="current-group()[not(title/@differentiator)]"/>
</ul>
</body>
</html>
</xsl:template>