Search code examples
xmlxslttei

Preceding-sibling and following-sibling not working with additional element


I first want to apologize for the length of this question, but hopefully providing this information will help.

I am attempting to use a single xsl on multiple xml documents, and it works on my original document (the text of four walls in a room, with each wall represented as a TEI surfaceGrp element). However, now I need to modify the xsl to deal with an extra TEI surfaceGrp element, representing the two sides of a written page, and then the collection of pages.

My original xml code for the four walls is something like this:

<TEI>
<sourceDoc>
    <surfaceGrp xml:id="wall" n="South Wall">
        <surface xml:id="EETS.T.29">
            <label>Verse 29</label>
            <graphic url="sw_test_1.jpg"/>
            <zone>
                <line/>
            </zone>
        </surface>
        <surface xml:id="EETS.T.30">
            <label>Verse 30</label>
            <graphic url="sw_test_2.jpg"/>
            <zone>
                <line/>
            </zone>
        </surface>
        <surface xml:id="EETS.T.27">
            <label>Verse 27</label>
            <graphic url="sw_test_3.jpg"/>
            <zone>
                <line/>
            </zone>
        </surface>
        <surface xml:id="EETS.T.56">
            <label>Verse 56</label>
            <graphic url="sw_test_4.jpg"/>
            <zone>
                <line/>
            </zone>
        </surface>
        <surface xml:id="EETS.T.57">
            <label>Verse 57</label>
            <graphic url="sw_test_5.jpg"/>
            <zone>
                <line/>
            </zone>
        </surface>
        <surface xml:id="EETS.T.58">
            <label>Verse 58</label>
            <graphic url="sw_test_6.jpg"/>
            <zone>
                <line/>
            </zone>
        </surface>
        <surface xml:id="EETS.T.59">
            <label>Verse 59</label>
            <graphic url="sw_test_7.jpg"/>
            <zone>
                <line/>
            </zone>
        </surface>
    </surfaceGrp>
    <surfaceGrp xml:id="wall" n="West Wall">
        <surface xml:id="EETS.T.60">
            <label>Verse 60</label>
            <graphic url="ww_test_1.jpg"/>
            <zone>
                <line/>
            </zone>
        </surface>
        <surface xml:id="EETS.T.63">
            <label>Verse 63</label>
            <graphic url="ww_test_2.jpg"/>
            <zone>
                <line/>
            </zone>
        </surface>
    </surfaceGrp>
</sourceDoc>

that I convert to the following html

<h1 class="menuHeader">
<span class="menuitem" id="image_thumbnail">
    <a class="nav_link" href="sw_test_1-thumbnail.html">
        <img class="thumbnail" src="../../Testament/Clopton/Thumbnails/sw_test_1-thumbnail.jpg"
            alt="Verse 29"/>
    </a>
</span>
<span class="menuitem" id="image_thumbnail">
    <a class="nav_link" href="sw_test_2-thumbnail.html">
        <img class="thumbnail" src="../../Testament/Clopton/Thumbnails/sw_test_2-thumbnail.jpg"
            alt="Verse 30"/>
    </a>
</span>
<span class="menuitem" id="image_thumbnail">
    <a class="nav_link" href="sw_test_3-thumbnail.html">
        <img class="thumbnail" src="../../Testament/Clopton/Thumbnails/sw_test_3-thumbnail.jpg"
            alt="Verse 27"/>
    </a>
</span>
<span class="menuitem" id="image_thumbnail">
    <a class="nav_link" href="sw_test_4-thumbnail.html">
        <img class="thumbnail" src="../../Testament/Clopton/Thumbnails/sw_test_4-thumbnail.jpg"
            alt="Verse 56"/>
    </a>
</span>
<span class="menuitem" id="image_thumbnail">
    <a class="nav_link" href="sw_test_5-thumbnail.html">
        <img class="thumbnail" src="../../Testament/Clopton/Thumbnails/sw_test_5-thumbnail.jpg"
            alt="Verse 57"/>
    </a>
</span>
<span class="menuitem" id="image_thumbnail">
    <a class="nav_link" href="sw_test_6-thumbnail.html">
        <img class="thumbnail" src="../../Testament/Clopton/Thumbnails/sw_test_6-thumbnail.jpg"
            alt="Verse 58"/>
    </a>
</span>
<span class="menuitem" id="image_thumbnail">
    <a class="nav_link" href="sw_test_7-thumbnail.html">
        <img class="thumbnail" src="../../Testament/Clopton/Thumbnails/sw_test_7-thumbnail.jpg"
            alt="Verse 59"/>
    </a>
</span>
<span class="menuitem" id="previousItem">
    <a class="nav_link" href="sw_test_6.html"> Previous</a>
</span>
<span class="menuitem" id="nextItem">
    <a class="nav_link" href="ww_test_1.html">Next</a>
</span>

via this xsl:

      <xsl:template name="menuHeader">
    <xsl:variable name="filename_length" select="string-length(tei:graphic/@url)"/>
    <h1 class="menuHeader">
        <span class="menuitem" id="groupLabel">
            <xsl:choose>
                <xsl:when test="../../../../tei:teiHeader/fileDesc/SourceDesc='Clopton_Chantry_Chapel'">
                    <xsl:value-of select="../@n"/>
                    <xsl:text> </xsl:text>
                    <span id="modelViewer">
                        <a href="../../Model/three/examples/chantry_chapel.html">

                            <xsl:text>(View Model)</xsl:text>
                        </a>
                    </span>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="concat(../@xml:id,' ',@n)"/>
                </xsl:otherwise>
            </xsl:choose>
        </span> 

        <xsl:apply-templates select=".././/tei:graphic" mode="list"/>
        <xsl:variable name="last_block" as="xs:integer">

            <xsl:value-of select="count(../preceding-sibling::tei:surfaceGrp)"/>
        </xsl:variable>
        <xsl:variable name="next_block" as="xs:integer">
            <xsl:value-of select="count(../following-sibling::tei:surfaceGrp)"/>
        </xsl:variable>
        <xsl:variable name="total_blocks">
            <xsl:value-of select="../last()"/>
        </xsl:variable>
        <xsl:variable name="group_position" as="xs:integer">
            <xsl:value-of
                select="../../count(tei:surfaceGrp) - count(../following-sibling::node()/position())"
            />
        </xsl:variable>
        <xsl:variable name="last_item_id">
            <xsl:value-of select="../../tei:surfaceGrp[last()]/tei:surface[last()]/@xml:id"/>
        </xsl:variable>
        <xsl:choose>
            <xsl:when test="count(preceding-sibling::tei:surface)+1 &gt; 1">
                <span class="menuitem" id="previousItem">
                    <a class="nav_link">
                        <xsl:attribute name="href">
                            <xsl:value-of select="concat(substring(preceding-sibling::*[1]/tei:graphic/@url,1,($filename_length - 4)),'.html')"/>
                        </xsl:attribute>
                        Previous</a>
                </span>
            </xsl:when>
            <xsl:when test="count(../preceding-sibling::tei:surfaceGrp)+1 &gt; 1">
                <span class="menuitem" id="previousItem">
                    <a class="nav_link"><xsl:attribute name="href">
                        <xsl:value-of select="concat(substring(../../tei:surfaceGrp[$last_block]/tei:surface[last()]/tei:graphic/@url,1,($filename_length - 4)),'.html')"/>
                    </xsl:attribute>Previous</a>
                </span>
            </xsl:when>
            <xsl:otherwise>
                <span class="menuitem" id="previousItem">
                    <a class="nav_link">Previous</a>
                </span>
            </xsl:otherwise>
        </xsl:choose>
        <xsl:choose>
            <xsl:when test="$last_item_id = @xml:id">
                <span class="menuitem" id="nextItem">
                    <a class="nav_link">Next</a>
                </span>
            </xsl:when>
            <xsl:when test="position() = last()">
                <span class="menuitem" id="nextItem">
                    <a class="nav_link"><xsl:attribute name="href"><xsl:value-of
                        select="concat(substring(../../tei:surfaceGrp[$group_position+1]/tei:surface[1]/tei:graphic/@url,1,($filename_length - 4)),'.html')"
                    /></xsl:attribute>Next</a>
                </span>
            </xsl:when>
            <xsl:when test="not(position() = last())">
                <span class="menuitem" id="nextItem">
                    <a class="nav_link"><xsl:attribute name="href"><xsl:value-of
                        select="concat(substring(following-sibling::*[1]/tei:graphic/@url,1,($filename_length - 4)),'.html')"
                    /></xsl:attribute>Next</a>
                </span>
            </xsl:when>
            <xsl:otherwise/>
        </xsl:choose>
    </h1>
</xsl:template>
<xsl:template match="tei:graphic" mode="list">
    <span class="menuitem" id="image_thumbnail">
        <a class="nav_link">
            <xsl:attribute name="href">
                <xsl:value-of select="concat(substring(@url,1,string-length(@url)-4),'.html')"/>
            </xsl:attribute>
            <img class="thumbnail">
                <xsl:attribute name="src">
                    <xsl:value-of
                        select="concat('../../',$title_folder,'/',$witness,'/',$thumbnail_folder,'/',substring(@url,1,string-length(@url)-4),'-thumbnail.jpg')"
                    />
                </xsl:attribute>
                <xsl:attribute name="alt">
                    <xsl:value-of select="../tei:label"/>
                </xsl:attribute>
            </img>
        </a>
    </span>
</xsl:template>

When I run this on the new document with the extra surfaceGrp element,

<TEI>
<sourceDoc>
    <surfaceGrp xml:id="g.3" n="gathering">
        <surfaceGrp xml:id="f.19" n="folio">
            <surface n="verso">
                <graphic url="Jesus_Q_G_8_f19v.jpg"/>
                <zone n="EETS.QD.I">
                    <line/>
                </zone>
                <zone n="EETS.QD.1">
                    <line/>
                </zone>
            </surface>
        </surfaceGrp>
        <surfaceGrp xml:id="f.20" n="folio">
            <surface n="recto">
                <graphic url="Jesus_Q_G_8_f20r.jpg"/>
                <zone n="EETS.QD.1">
                    <line/>
                </zone>
                <zone n="EETS.QD.2">
                    <line/>
                </zone>
                <zone n="EETS.QD.3">
                    <line/>
                </zone>
                <zone n="EETS.QD.4">
                    <line/>
                </zone>
            </surface>
            <surface n="verso">
                <graphic url="Jesus_Q_G_8_f20v.jpg"/>
                <zone n="EETS.QD.4">
                    <line>
                        <orig>To see my son <hi rend="underline">thus</hi> nailed to <hi
                                rend="underline">a tre</hi> .</orig>
                    </line>
                </zone>
                <zone n="EETS.QD.5">
                    <line/>
                </zone>
                <zone n="EETS.QD.6">
                    <line/>
                </zone>
                <zone n="EETS.QD.7">
                    <line/>
                </zone>
            </surface>
        </surfaceGrp>
    </surfaceGrp>
</sourceDoc>

I only get one page's worth of information. Which makes sense, because a page is fitting the same surfaceGrp slot that a wall does in the original code. But when I try to modify the code to move up to the collection of pages level

 <xsl:template name="menuHeader">
    <xsl:variable name="filename_length" select="string-length(tei:graphic/@url)"/>
    <h1 class="menuHeader">
        <span class="menuitem" id="groupLabel">
            <xsl:choose>
                <xsl:when test="../../../../tei:teiHeader/fileDesc/SourceDesc='Clopton_Chantry_Chapel'">
                    <xsl:value-of select="../@n"/>
                    <xsl:text> </xsl:text>
                    <span id="modelViewer">
                        <a href="../../Model/three/examples/chantry_chapel.html">

                            <xsl:text>(View Model)</xsl:text>
                        </a>
                    </span>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="concat(../@xml:id,' ',@n)"/>
                </xsl:otherwise>
            </xsl:choose>
        </span> 

        <xsl:apply-templates select=".././/tei:graphic" mode="list"/>
        <xsl:variable name="last_block" as="xs:integer">
            <xsl:choose>
                <xsl:when test="../../../../tei:teiHeader/fileDesc/SourceDesc='Clopton_Chantry_Chapel'">
                    <xsl:value-of select="count(../preceding-sibling::tei:surfaceGrp)"/>   
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="count(../../preceding-sibling::tei:surfaceGrp)"/> 
                </xsl:otherwise>
            </xsl:choose>

        </xsl:variable>
        <xsl:variable name="next_block" as="xs:integer">
            <xsl:choose>
                <xsl:when test="../../../../tei:teiHeader/fileDesc/SourceDesc='Clopton_Chantry_Chapel'">
                    <xsl:value-of select="count(../following-sibling::tei:surfaceGrp)"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="count(../../following-sibling::tei:surfaceGrp)"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:variable name="total_blocks">
            <xsl:value-of select="../last()"/>
        </xsl:variable>
        <xsl:variable name="group_position" as="xs:integer">
            <xsl:choose>
                <xsl:when test="../../../../tei:teiHeader/fileDesc/SourceDesc='Clopton_Chantry_Chapel'">
                    <xsl:value-of
                        select="../../count(tei:surfaceGrp) - count(../following-sibling::node()/position())"
                    />                        
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of
                        select="../../../count(tei:surfaceGrp) - count(../following-sibling::node()/position())"
                    />
                </xsl:otherwise>
            </xsl:choose>

        </xsl:variable>
        <xsl:variable name="last_item_id">
            <xsl:choose>
                <xsl:when test="../../../../tei:teiHeader/fileDesc/SourceDesc='Clopton_Chantry_Chapel'">
                    <xsl:value-of select="../../tei:surfaceGrp[last()]/tei:surface[last()]/@xml:id"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="../../../tei:surfaceGrp[last()]/tei:surfaceGrp[last()]/tei:surface[last()]/@xml:id"/>
                </xsl:otherwise>
            </xsl:choose>

        </xsl:variable>
        <xsl:choose>
            <xsl:when test="count(preceding-sibling::tei:surface)+1 &gt; 1">
                <span class="menuitem" id="previousItem">
                    <a class="nav_link">
                   <xsl:attribute name="href">
                       <xsl:value-of select="concat(substring(preceding-sibling::*[1]/tei:graphic/@url,1,($filename_length - 4)),'.html')"/>

                   </xsl:attribute>
                    Previous</a>
                </span>
            </xsl:when>
            <xsl:when test="count(../preceding-sibling::tei:surfaceGrp)+1 &gt; 1">
                <span class="menuitem" id="previousItem">
                    <a class="nav_link"><xsl:attribute name="href">
                        <xsl:value-of select="concat(substring(../../tei:surfaceGrp[$last_block]/tei:surface[last()]/tei:graphic/@url,1,($filename_length - 4)),'.html')"/>
                    </xsl:attribute>Previous</a>
                </span>
            </xsl:when>
            <xsl:otherwise>
                <span class="menuitem" id="previousItem">
                    <a class="nav_link">Previous</a>
                </span>
            </xsl:otherwise>
        </xsl:choose>

        <xsl:choose>
            <xsl:when test="$last_item_id = @xml:id">
                <span class="menuitem" id="nextItem">
                    <a class="nav_link">Next</a>
                </span>
            </xsl:when>
            <xsl:when test="position() = last()">
                <span class="menuitem" id="nextItem">
                    <a class="nav_link"><xsl:attribute name="href"><xsl:value-of
                        select="concat(substring(../../tei:surfaceGrp[$group_position+1]/tei:surface[1]/tei:graphic/@url,1,($filename_length - 4)),'.html')"
                            /></xsl:attribute>Next</a>
                </span>
            </xsl:when>
            <xsl:when test="not(position() = last())">
                <span class="menuitem" id="nextItem">
                    <a class="nav_link"><xsl:attribute name="href"><xsl:value-of
                        select="concat(substring(following-sibling::*[1]/tei:graphic/@url,1,($filename_length - 4)),'.html')"
                            /></xsl:attribute>Next</a>
                </span>
            </xsl:when>
            <xsl:otherwise/>
        </xsl:choose>
    </h1>
</xsl:template>

I get the same result. And I have no idea why. I obviously don't understand preceding-sibling and following-sibling as well as I thought I did, and if there's a cleaner way to accomplish what I want I'd be happy to use it.


Solution

  • I don't understand what you want to achieve but some of your code looks rather odd, for instance what do you want to achieve with

        <xsl:variable name="total_blocks">
            <xsl:value-of select="../last()"/>
        </xsl:variable>
    

    ? In general such constructs can be written as <xsl:variable name="total_blocks" select="../last()"/> but the expression ../last() will simply return 1 as long as there is a parent node ...

    Another oddity is count(../following-sibling::node()/position()) which I don't think does more than count(../following-sibling::node()).