xsl-list
[Top] [All Lists]

Re: [xsl] Techniques for Sorting and Reducing Maps in XSLT 3/XPath 3?

2018-07-19 07:16:05
Here is my current solution, which is definitely an improvement over my earlier 
solution. I hadn’t realized (or it didn’t occur to me) that I could use 
for-each-group on maps. Using for-each-group and grouping by a specific field 
is certainly easy and straightforward:

 

For this data, the paths provide the course details, in particular, course ID 
and locale. I also have to eliminate config.xml files that are not the ones I’m 
looking for, which I do just by counting the number of tokens in the path.

 

    <xsl:variable name="candidate-configs" as="map(*)*"
      select="collection($courses-dir || 
'?recurse=yes;metadata=yes;match=config.xml')"
    />
        
    <!-- Select configurations that are in the appropriate locale and that
         are course configurations and not course content configurations 
(config.xml
         is used both for course description and within the course content 
directory).
         
         Result is a sequence of maps, one for each selected candidate course.
      -->
    <xsl:variable name="configs-to-use" as="map(*)*">
      <xsl:for-each select="$candidate-configs">        
        <xsl:variable name="cand" as="map(*)" select="."/>
        <xsl:variable name="path-base" as="xs:string" select="$cand?name"/>     
   
        <xsl:variable name="path" as="xs:string" 
select="substring-after($path-base, $courses-dir || '/')"/>
        <xsl:variable name="tokens" as="xs:string*" select="tokenize($path, 
'/')"/>
        <xsl:variable name="course-group" as="xs:string" select="$tokens[1]"/>
        <xsl:variable name="course-id" as="xs:string" select="$tokens[2]"/>
        <xsl:variable name="locale" as="xs:string" select="$tokens[3]"/>
        <xsl:variable name="version" as="xs:string" select="$tokens[4]"/>
        <xsl:variable name="new-map" as="map(*)"
          select="
          map{
          'course-group' : $course-group,
          'course-id' : $course-id,
          'locale' : $locale,
          'version' : $version,
          'course-key' : ($course-group, $course-id) => string-join('^'),
          'course-key-locale' : ($course-group, $course-id, $locale) => 
string-join('^')
          }"
        />
        <xsl:if test="(empty($locales) or (exists($locales) and $locale = 
$locales)) and count($tokens) eq 6">
          <xsl:sequence select="map:merge((., $new-map))"/>
        </xsl:if>
      </xsl:for-each>
    </xsl:variable>

    <!-- Now group by course ID and locale in order to select the highest 
version of each course. -->
    <xsl:variable name="course-configs-by-course-key-locale" as="map(xs:string, 
map(*))">
      <xsl:map>
        <xsl:for-each-group select="$configs-to-use" 
group-by=".?course-key-locale">
          <xsl:variable name="courses-for-locale" as="map(*)*" 
select="current-group()"/>
          <xsl:variable name="highest-version" as="xs:string" 
            select="$courses-for-locale ! map:get(., 'version') ! xs:double(.) 
=> max() => format-number('#.0')"
          />
          <xsl:variable name="course-map" as="map(*)*" 
            select="
              $courses-for-locale ! 
              (if (map:get(., 'version') eq $highest-version) 
               then . 
               else ()
              )"

          />
          <xsl:sequence select="map:entry(current-grouping-key(), 
$course-map)"/>
        </xsl:for-each-group>
      </xsl:map>
    </xsl:variable>

 

Cheers,

 

E.

--

Eliot Kimber

http://contrext.com

 

 

 

From: "Mukul Gandhi gandhi(_dot_)mukul(_at_)gmail(_dot_)com" 
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com>
Reply-To: <xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com>
Date: Saturday, July 7, 2018 at 2:01 AM
To: <xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com>
Subject: Re: [xsl] Techniques for Sorting and Reducing Maps in XSLT 3/XPath 3?

 

On Fri, Jul 6, 2018 at 9:58 PM, Liam R. E. Quin liam(_at_)w3(_dot_)org 
<xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> wrote:

On your xsl:iterate question, i'd generally try for-each and/or for-
each-group before xsl:iterate. The reason is largely that i think it
encourage thinking in terms of a functional mapping rather than an
imperative loop, and that can help clarity of thought.

 

 I'm still not up to speed with using XSLT 3.0 (but I'm trying to). But I agree 
with your point Liam.

 



 

-- 

Regards,

Mukul Gandhi

XSL-List info and archive 

EasyUnsubscribe (by email) 
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--
<Prev in Thread] Current Thread [Next in Thread>