xsl-list
[Top] [All Lists]

RE: [xsl] Positional grouping with exceptions

2006-12-22 01:11:10
Michael Kay, Andrew Welch, thank you for your help.

However, Micheal Kay's solution isn't working, Saxon reports that you
cannot start with self:: in a group-starting-with attribute. (Axis in
pattern must be child or attribute).

The solution of Andrew Welch seemed to work better, until I discovered
that when I have a node containing a childnode and some text, only the
childnode remains in the resulting document.
I've tried to modify the stylesheet, but no such luck.

--
Fredrik Geers

-----Original Message-----
Date: Wed, 20 Dec 2006 15:11:12 -0000
To: <xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com>
From: "Michael Kay" <mike(_at_)saxonica(_dot_)com>
Subject: RE: [xsl] Positional grouping with exceptions
Message-ID: <01c701c72449$161cb0a0$6401a8c0(_at_)turtle>

Try

<xsl:template match="book">
  <book>
    <xsl:for-each-group select="*"
      group-starting-with="self::a[not(child::text())] | 

self::a[not(preceding-sibling::*[1][self::a])] | 
                           *[not(self::a)]">
      <xsl:choose>
        <xsl:when test="self::a[not(child::text())]">
          <a1><xsl:copy-of select="remove(current-group(),1)"/></a1>
        </xsl:when>
        <xsl:when test="self::a">
          <a1><xsl:copy-of select="current-group()"/></a1>
        </xsl:when>
        <xsl:otherwise> 
          <xsl:copy-of select="current-group()"/>

Not tested.

Michael Kay
http://www.saxonica.com/

-----Original Message-----
Date: Wed, 20 Dec 2006 15:28:15 +0000
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
From: "Andrew Welch" <andrew(_dot_)j(_dot_)welch(_at_)gmail(_dot_)com>
Subject: Re: [xsl] Positional grouping with exceptions
Message-ID:
<74a894af0612200728o791232f5t915f97e8b0faf43(_at_)mail(_dot_)gmail(_dot_)com>

I think you need a modified identity transform for this one.  The
following stylesheet walks the tree along the following-sibling axis,
allowing you to group <a>'s with text as you come across them:

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

<xsl:template match="node()">
      <xsl:copy>
              <xsl:copy-of select="@*"/>
              <xsl:apply-templates select="node()[1]"/>
      </xsl:copy>
      <xsl:apply-templates select="following-sibling::*[1]"/>
</xsl:template>

<xsl:template match="a[text()]">
      <al>
              <xsl:apply-templates select="." mode="fill"/>
      </al>
      <xsl:apply-templates
select="following-sibling::*[not(self::a[text()])][1]"/>
</xsl:template>

<xsl:template match="a[text()]" mode="fill">
      <xsl:copy-of select="."/>
      <xsl:apply-templates
select="following-sibling::*[1][self::a[text()]]" mode="fill"/>
</xsl:template>

</xsl:stylesheet>

The output generated is:

<book>
      <header>title</header>
      <al>
              <a>text</a>
              <a>text</a>
      </al>
      <otherelement>title</otherelement>
      <al>
              <a>text</a>
              <a>text</a>
      </al>
      <a/>
      <al>
              <a>text</a>
      </al>
</book>

Notice the <a/> which wasnt present in your example - easy to suppress
if that really is the case.

By the way, Saxon 8.8 is available now.

cheers
andrew

--~------------------------------------------------------------------
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>