前兄弟姐妹和后兄弟姐妹不使用其他元素

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

首先我想为这个问题的长度道歉,但希望提供这些信息会有所帮助。

我正尝试在多个 xml 文档上使用单个 xsl,它适用于我的原始文档(房间中四面墙的文本,每面墙都表示为一个 TEI surfaceGrp 元素)。但是,现在我需要修改xsl来处理一个额外的TEI surfaceGrp元素,代表一个书写页面的两侧,然后是页面的集合。

我原来的xml四面墙代码是这样的:

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

我将其转换为以下 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>

通过这个 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>

当我 运行 在新文档上使用额外的 surfaceGrp 元素时,

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

我只得到一页的信息。这是有道理的,因为页面与原始代码中的墙一样适合相同的 surfaceGrp 插槽。但是当我尝试修改代码以向上移动到页面集合级别时

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

我得到了同样的结果。我不知道为什么。我显然不像我想的那样理解 preceding-sibling 和 following-sibling,如果有更简洁的方法来完成我想要的,我会很乐意使用它。

我不明白你想实现什么,但你的一些代码看起来很奇怪,例如你想用

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

?一般来说,这样的构造可以写成 <xsl:variable name="total_blocks" select="../last()"/> 但表达式 ../last() 将简单地 return 1 只要有父节点 ...

另一个奇怪的是 count(../following-sibling::node()/position()),我认为它的作用不超过 count(../following-sibling::node())