xsl-list
[Top] [All Lists]

RE: [xsl] Sort by one attribute & use Muenchian technique to group by another attribute?

2009-06-10 14:17:35
John,

The node-set() function is supported under IE (MSXML) as well, but it's 
registered under a different namespace - "urn:schemas-microsoft-com:xslt". You 
could use function-available() to check if your stylesheet is being run in 
MSXML or non-MSXML and use the appropriate namespace, but that's a little 
message. However, if you're using the stylesheet primarily on a browser, and if 
you have the option to serve your XSLT file through a web-based preprocessor, 
I've found it's much easier to just grab the stylesheet's text content in an 
intermediate step, sniff the HTTP_USER_AGENT CGI variable to determine the 
browser, and regex-replace the EXSL namespace into the MSXML one if it's being 
requested by IE, or vice-versa if it's not. YMMV here.

~ Scott

-----Original Message-----
From: Newman, John W [mailto:newmanjw(_at_)upmc(_dot_)edu] 
Sent: Wednesday, June 10, 2009 12:32 PM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: RE: [xsl] Sort by one attribute & use Muenchian technique to group by 
another attribute? 

Thank you James, that works great.  I see what you did there, very nice.  I 
will adapt this to our actual xsl now and see how it goes.

Martin - Thanks for your approach as well, I am having a bit of trouble getting 
the exsl ns to work through IE, but I'll keep at it.

Andrew - Welcome to my spam folder, KMA.



-----Original Message-----
From: James A. Robinson [mailto:jim(_dot_)robinson(_at_)stanford(_dot_)edu] 
Sent: Wednesday, June 10, 2009 1:26 PM
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: Re: [xsl] Sort by one attribute & use Muenchian technique to group by 
another attribute? 

 
Anyway, I could really use some help or guidance here.  I have two sets of 
nodes that are intermixed - I need to output them ordered by a sort order 
attribute, and group them by a key attribute such that the key only shows up 
when it changes from the previously sorted item.  Here's a simplified example:

My first though is that, if you have some control/knowledge of
what values are or are not legal in your grouping key (@key),
you could build your own grouping function:


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="1.0">

  <xsl:output indent="no" method="text"/>

  <xsl:template match="/">

    <xsl:variable name="list">
      <xsl:for-each select="root/*">
        <xsl:sort select="@order" data-type="number" />
        <xsl:value-of select="concat(@order, ',', @key, '&#10;')" />
      </xsl:for-each>
    </xsl:variable>

    <xsl:call-template name="group">
      <xsl:with-param name="list" select="$list" />
      <xsl:with-param name="previous" select="''" />
    </xsl:call-template>

  </xsl:template>

  <xsl:key name="key-pair" match="*" use="concat(@order, ',', @key)"/>

  <xsl:template name="group">
    <xsl:param name="list" />
    <xsl:param name="previous" />

    <xsl:choose>
      <xsl:when test="$list = ''" />
      <xsl:otherwise>
        <xsl:variable name="key-pair" select="substring-before($list, '&#10;')" 
/>
        <xsl:variable name="current"  select="substring-after($key-pair, ',')" 
/>
        <xsl:variable name="remaining" select="substring-after($list, '&#10;')" 
/>

        <xsl:choose>
          <xsl:when test="$previous = ''">
            <xsl:value-of select="concat($current,':&#10;')"/>
          </xsl:when>
          <xsl:when test="$previous != $current">
            <xsl:value-of select="concat($current,':&#10;')"/>
          </xsl:when>
        </xsl:choose>

        <xsl:value-of select="concat(key('key-pair', $key-pair)/@text, 
'&#10;')"/>

        <xsl:call-template name="group">
          <xsl:with-param name="list" select="$remaining" />
          <xsl:with-param name="previous" select="$current" />
        </xsl:call-template>

      </xsl:otherwise>
    </xsl:choose>
   </xsl:template>

</xsl:stylesheet>


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
James A. Robinson                       
jim(_dot_)robinson(_at_)stanford(_dot_)edu
Stanford University HighWire Press      http://highwire.stanford.edu/
+1 650 7237294 (Work)                   +1 650 7259335 (Fax)

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>