xsl-list
[Top] [All Lists]

Re: [xsl] Problem with grouping sibling tags

2008-12-23 08:26:15
At 2008-12-23 12:12 +0100, Casper Voortman wrote:
The second link post seems more in the direction of what i'm looking
for, but unfortunately i'm not completely grasping the process. Will
study it harder, but this is 'higher' xslt writing for me.
...
Op 23 dec 2008, om 11:45 heeft Michael Kay het volgende geschreven:
And in particular Ken Holman's post at
http://markmail.org/message/teo6uftbxjvsa56c (dated 22 Oct 1999)
which must
be the earliest exposition of the technique often called "sibling
recursion".

Thanks, Michael, for citing that.

Casper, the sentence for:

  
not(preceding-sibling::*[1][self::Paragraph][(_at_)Pstyle='PlattetekstBullet'])

is "it is not true that the immediately preceding sibling is a paragraph with the particular style".

The sentence for:

  following-sibling::*[1][self::Paragraph][(_at_)Pstyle='PlattetekstBullet']

is "the imeediately following sibling if it is a paragraph with the particular style".

And both are brought together in the running example below that is almost identical to the one posted nine years ago. I wrote the below before looking back and I see that nothing really has changed.

I hope this helps.

Happy holidays to everyone on the list!

. . . . . . . . . Ken


T:\ftemp>type casper.xml
<?xml version="1.0" encoding="UTF-8"?>
<ArticleContent>
    <Frame Label="BroodTekst">
        <Paragraph Pstyle="Tussenkop">
            <Text Cstyle="[No character style]">textcontent</Text>
            <Text Cstyle="[No character style]"/>
        </Paragraph>
        <Paragraph Pstyle="PlattetekstInitiaal">
            <Text Cstyle="[No character style]">textcontent</Text>
        </Paragraph>
        <Paragraph Pstyle="PlattetekstBullet">
            <Text Cstyle="[No character style]">bullet1</Text>
        </Paragraph>
        <Paragraph Pstyle="PlattetekstBullet">
            <Text Cstyle="[No character style]">bullet2</Text>
        </Paragraph>
        <Paragraph Pstyle="Plattetekst">
            <Text Cstyle="[No character style]">textcontent</Text>
        </Paragraph>
        <Paragraph Pstyle="Plattetekst">
            <Text Cstyle="[No character style]">textcontent</Text>
        </Paragraph>
        <Paragraph Pstyle="PlattetekstBullet">
            <Text Cstyle="[No character style]">bullet3</Text>
        </Paragraph>
    </Frame>
</ArticleContent>

T:\ftemp>call xslt casper.xml casper.xsl casper.out

T:\ftemp>type casper.out
<?xml version="1.0" encoding="utf-8"?>

<section role="BroodTekst">
   <title role="tussenkop">textcontent</title>
   <para role="plattetekstInitiaal">
            textcontent
        </para>
   <itemizedlist>
      <listitem>
         <para role="plattetekstBullet">
            bullet1
        </para>
      </listitem>
      <listitem>
         <para role="plattetekstBullet">
            bullet2
        </para>
      </listitem>
   </itemizedlist>
   <para role="plattetekst">
            textcontent
        </para>
   <para role="plattetekst">
            textcontent
        </para>
   <itemizedlist>
      <listitem>
         <para role="plattetekstBullet">
            bullet3
        </para>
      </listitem>
   </itemizedlist>
</section>

T:\ftemp>type casper.xsl
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="1.0">

<xsl:output indent="yes"/>

<xsl:template match="Frame">
  <section role="{(_at_)Label}"><xsl:apply-templates select="*"/></section>
</xsl:template>

<xsl:template match="Paragraph[(_at_)Pstyle='Tussenkop']">
  <title role="tussenkop"><xsl:apply-templates select="Text"/></title>
</xsl:template>

<xsl:template match="Paragraph[(_at_)Pstyle='PlattetekstInitiaal']">
  <para role="plattetekstInitiaal"><xsl:apply-templates/></para>
</xsl:template>

<xsl:template match="Paragraph[(_at_)Pstyle='Plattetekst']">
  <para role="plattetekst"><xsl:apply-templates/></para>
</xsl:template>

<xsl:template match="Paragraph[(_at_)Pstyle='PlattetekstBullet']">
  <!--only do something if this is the first of consecutive siblings-->
  <xsl:if test="not(preceding-sibling::*[1][self::Paragraph]
                                           [(_at_)Pstyle='PlattetekstBullet'])">
    <itemizedlist>
      <xsl:apply-templates mode="next-list-item" select="."/>
    </itemizedlist>
  </xsl:if>
</xsl:template>

<xsl:template mode="next-list-item"
              match="Paragraph[(_at_)Pstyle='PlattetekstBullet']">
  <listitem>
    <para role="plattetekstBullet"><xsl:apply-templates/></para>
  </listitem>
  <!--keep building the list as long as there are consecutive siblings-->
  <xsl:apply-templates mode="next-list-item"
                       select="following-sibling::*[1][self::Paragraph]
                                           [(_at_)Pstyle='PlattetekstBullet']"/>
</xsl:template>

</xsl:stylesheet>

T:\ftemp>rem Done!



--
Upcoming XSLT/XSL-FO, UBL and code list hands-on training classes:
:  Sydney, AU 2009-01/02; Brussels, BE 2009-03; Prague, CZ 2009-03
Training tools: Comprehensive interactive XSLT/XPath 1.0/2.0 video
Video sample lesson:    http://www.youtube.com/watch?v=PrNjJCh7Ppg
Video course overview:  http://www.youtube.com/watch?v=VTiodiij6gE
G. Ken Holman                 mailto:gkholman(_at_)CraneSoftwrights(_dot_)com
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Male Cancer Awareness Nov'07  http://www.CraneSoftwrights.com/s/bc
Legal business disclaimers:  http://www.CraneSoftwrights.com/legal


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