Re: [xsl] Grouping text nodes

Subject: Re: [xsl] Grouping text nodes
From: David Carlisle <davidc@xxxxxxxxx>
Date: Thu, 4 Aug 2005 01:00:24 +0100
something like this,if I understood you correctly


<!DOCTYPE div [
<!ENTITY nbsp " ">
<!ENTITY aelig "ae">
]>
<div>

<div class="chapter">
<span class="chapter-num">1</span>
        <div class="poetrystartchapter">
                    <span class="vn" id="x1_1">1</span>&nbsp;Beatus vir qui
                    non abiit in consilio impiorum,<br/> et in via peccatorum
                    non stetit,<br/> et in cathedra pestilenti&aelig;
non sedit&nbsp;;<br/>
                    <span class="vn" id="x1_2">2</span>&nbsp;sed in lege
                    Domini voluntas ejus,<br/> et in lege ejus meditabitur die
                    ac nocte.<br/>
                    <span class="vn" id="x1_3">3</span>&nbsp;Et erit tamquam
                    lignum quod plantatum est secus decursus aquarum,<br/> quod
                    fructum suum dabit in tempore suo&nbsp;:<br/> et folium
                    ejus non defluet&nbsp;;<br/> et omnia
                    qu&aelig;cumque faciet prosperabuntur.<br/>
...</div>...</div>

 <div class="chapter"><span class="chapter-num">118</span>
                <span class="vn" id="x118_1">1</span>&nbsp;Alleluja.
                    <div class="poetry"><span
class="speaker">Aleph.</span> Beati
                    immaculati in via,<br/> qui ambulant in lege Domini.<br/>
                    <span class="vn" id="x118_2">2</span>&nbsp;Beati qui
                    scrutantur testimonia ejus&nbsp;;<br/> in toto corde
                    exquirunt eum.<br/>
</div>
</div>
</div>

<!--
What I want to get is something like:
- - - - -
<div type="chapter" n="1">
             <milestone type="poetrystartchapter"/>
             <lg xml:id="x1_1" n="1">
                    <l xml:id="x1_1-1">Beatus vir qui
                    non abiit in consilio impiorum,</l>
                   <l xml:id="x1_1-2">et in via peccatorum non stetit,</l>
                    <l xml:id="x1_1-3">et in cathedra
pestilenti&aelig; non sedit </l>
              </lg>
              <lg xml:id="x1_2" n="2">
                    <l xml:id="x1_2-1"> sed in lege Domini voluntas ejus,</l>
                    <l xml:id="x1_2-2">et in lege ejus meditabitur die
ac nocte.</l>
               </lg>
                <lg xml:id="x1_3">
                     <l xml:id="x1_3-1"> Et erit tamquam
                    lignum quod plantatum est secus decursus aquarum,</l>
                    <l xml:id="x1_3-2"> quod fructum suum dabit in
tempore suo :</l>
                    <l xml:id="x1_3-3"> et folium ejus non defluet;</l>
                    <l xml:id="x1_3-4"> et omnia qu&aelig;cumque
faciet prosperabuntur.</l>
                     </lg>
<milestone type="EndOfpoetrystartchapter"/>
...</div>
- - -
My problem is when I'm looking backwards to create the @xml
Which is supposed to  come out something likelike:
- - -
 <div type="chapter" n="118">
                <lg xml:id="x118_1" n="1">
                     <l xml:id="x118_1-1">Alleluja.
                      <milestone type="poetry"/>
                    <seg type="speaker">Aleph.</seg> Beati immaculati
in via,</l>
                     <l xml:id="x118_1-2"> qui ambulant in lege Domini.</l>
                 </lg>
                  <lg>
                     <l xml:id="x118_2-1"> Beati qui scrutantur
testimonia ejus; </l>
                     <l xml:id="x118_2-2"> in toto corde  exquirunt eum.</l>
                  </lg>
                   <milestone type="Endofpoetry"/>
... </div>
- - -
At the moment when matching  text() to create the lines, I then look
back (preceding:: or prece

-->







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

<xsl:template match="/">
<xsl:variable name="x">
<xsl:apply-templates/>
</xsl:variable>
<xsl:apply-templates mode="lg" select="$x"/>
</xsl:template>

<xsl:template match="*" mode="#default lg">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:apply-templates mode="#current"/>
</xsl:copy>
</xsl:template>

<xsl:template match="div[@class='chapter']">
<div type="chapter" n="{span[@class='chapter-num']}">
<xsl:apply-templates select="node()[not(self::span[@class='chapter-num'])]"/>
</div>
</xsl:template>

<xsl:template match="div[@class=('poetrystartchapter','poetry')]">
<milestone type="{@class}"/>
<xsl:apply-templates/>
<milestone type="EndOf{@class}"/>
</xsl:template>

<xsl:template match="span[@class='speaker']">
<seg type="speaker">
<xsl:apply-templates/>
</seg>
</xsl:template>

<xsl:template match="*[span[@class='vn']]" mode="lg">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:for-each-group select="node()" group-adjacent="empty(self::span[@class='vn'])">
<xsl:choose>
<xsl:when test="not(current-grouping-key())"/>
<xsl:when test="preceding-sibling::span[1][@class='vn']">
<xsl:text>&#10;</xsl:text>
<xsl:variable name="i" select="preceding-sibling::span[1]/@id"/>
<lg xml:id="{$i}" n="{substring-after($i,'_')}">
<xsl:for-each-group select="current-group()" group-adjacent="empty(self::br)">
<xsl:choose>
<xsl:when test="current-group()/self::milestone and not(normalize-space(string-join(current-group(),'')))">
<xsl:copy-of select="current-group()[not(self::br)]"/>
</xsl:when>
<xsl:when test="current-grouping-key() and(current-group()/self::* or normalize-space(string-join(current-group(),'')))">
<xsl:text>&#10;</xsl:text>
<l xml:id="{$i}-{(position()+1)div 2}">
<xsl:copy-of select="current-group()[not(self::br)]"/>
</l>
</xsl:when>
</xsl:choose>
</xsl:for-each-group>
<xsl:text>&#10;</xsl:text>
</lg>
<xsl:text>&#10;</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="current-group()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>


$ saxon8 id.xml id.xsl
<?xml version="1.0" encoding="UTF-8"?><div>

<div type="chapter" n="1">

        <milestone type="poetrystartchapter"/>

<lg xml:id="x1_1" n="1">
<l xml:id="x1_1-1"> Beatus vir qui
                    non abiit in consilio impiorum,</l>
<l xml:id="x1_1-2"> et in via peccatorum
                    non stetit,</l>
<l xml:id="x1_1-3"> et in cathedra pestilentiae
non sedit ;</l>
</lg>

<lg xml:id="x1_2" n="2">
<l xml:id="x1_2-1"> sed in lege
                    Domini voluntas ejus,</l>
<l xml:id="x1_2-2"> et in lege ejus meditabitur die
                    ac nocte.</l>
</lg>

<lg xml:id="x1_3" n="3">
<l xml:id="x1_3-1"> Et erit tamquam
                    lignum quod plantatum est secus decursus
aquarum,</l>
<l xml:id="x1_3-2"> quod
                    fructum suum dabit in tempore suo :</l>
<l xml:id="x1_3-3"> et folium
                    ejus non defluet ;</l>
<l xml:id="x1_3-4"> et omnia
                    quaecumque faciet prosperabuntur.</l>
<l xml:id="x1_3-5">
...<milestone type="EndOfpoetrystartchapter"/>...</l>
</lg>
</div>

 <div type="chapter" n="118">

<lg xml:id="x118_1" n="1">
<l xml:id="x118_1-1"> Alleluja.
                    <milestone type="poetry"/><seg
type="speaker">Aleph.</seg> B
eati
                    immaculati in via,</l>
<l xml:id="x118_1-2"> qui ambulant in lege Domini.</l>
</lg>

<lg xml:id="x118_2" n="2">
<l xml:id="x118_2-1"> Beati qui
                    scrutantur testimonia ejus ;</l>
<l xml:id="x118_2-2"> in toto corde
                    exquirunt eum.</l>
<milestone type="EndOfpoetry"/>

</lg>
</div>
</div>


________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________

Current Thread