xsl-list
[Top] [All Lists]

Re: [xsl] Is this a grouping task?

2019-10-22 14:55:59
Instead of trying to do fancy things -- even using XSLT 3.0 accumulators !
here is a short and simple XSLT 1.0 solution -- 36 lines with all
formatting for readability:

<xsl:stylesheet version="1.0" xmlns:xsl="
http://www.w3.org/1999/XSL/Transform";>
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:key name="kInRev"
    match="*[not(self::revst or self::revend)
                    and preceding-sibling::*[self::revst or
self::revend][1][self::revst]
                 ]"
    use="generate-id(preceding-sibling::revst[1])"/>

  <xsl:template match="node()|@*" name="identity">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="topic/*">
    <xsl:variable name="vNodesInRev" select="key('kInRev',
generate-id(preceding-sibling::revst[1]))"/>
    <xsl:variable name="vInRev" select="count($vNodesInRev | .) =
count($vNodesInRev)"/>

    <xsl:apply-templates select="self::*[$vInRev]" mode="inRev"/>
    <xsl:apply-templates select="self::*[not($vInRev)]" mode="notInRev"/>
  </xsl:template>

  <xsl:template match="*" mode="inRev">
    <xsl:copy>
      <xsl:apply-templates select="@*"/>
      <xsl:attribute name="rev">changed</xsl:attribute>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="*" mode="notInRev">
    <xsl:call-template name="identity"/>
  </xsl:template>
</xsl:stylesheet>

😆
Cheers,
Dimitre



On Tue, Oct 22, 2019 at 12:10 PM Martin Honnen 
martin(_dot_)honnen(_at_)gmx(_dot_)de <
xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com> wrote:

On 22.10.2019 20:41, Martin Honnen martin(_dot_)honnen(_at_)gmx(_dot_)de 
wrote:
On 22.10.2019 20:07, Rick Quatro rick(_at_)rickquatro(_dot_)com wrote:

I am using XSLT 2 and think this may require a for-each-group solution.
Any advice would be appreciated. Thank you very much.

As an alternative, if you can move to XSLT 3, then I think it is also
easy with "xsl:iterate":

Thinking about it again, it seems in XSLT 3 simply using an accumulator
makes it even more easy and compact:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
     xmlns:xs="http://www.w3.org/2001/XMLSchema";
     exclude-result-prefixes="#all"
     version="3.0">

     <xsl:mode on-no-match="shallow-copy" streamable="yes"
use-accumulators="rev-change"/>

     <xsl:accumulator name="rev-change" as="xs:boolean"
initial-value="false()" streamable="yes">
         <xsl:accumulator-rule match="topic" select="false()"/>
         <xsl:accumulator-rule match="topic/revst" select="true()"/>
         <xsl:accumulator-rule match="topic/revend" select="false()"/>
     </xsl:accumulator>

     <xsl:template match="topic/*[accumulator-before('rev-change')]">
         <xsl:copy>
             <xsl:attribute name="rev">changed</xsl:attribute>
             <xsl:apply-templates select="@*"/>
             <xsl:apply-templates/>
         </xsl:copy>
     </xsl:template>

     <xsl:template match="revst | revend" priority="2"/>

</xsl:stylesheet>

Saxon EE however doesn't like it for streaming and crashes, works fine
however with HE or with EE and streaming turned off.




-- 
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
To avoid situations in which you might make mistakes may be the
biggest mistake of all
------------------------------------
Quality means doing it right when no one is looking.
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play
-------------------------------------
To achieve the impossible dream, try going to sleep.
-------------------------------------
Facts do not cease to exist because they are ignored.
-------------------------------------
Typing monkeys will write all Shakespeare's works in 200yrs.Will they write
all patents, too? :)
-------------------------------------
Sanity is madness put to good use.
-------------------------------------
I finally figured out the only reason to be alive is to enjoy it.
--~----------------------------------------------------------------
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>