如何从排序的XSL中的上一个和下一个元素访问值:for-EAST循环



我正在使用xsl:for-east循环根据其 @id-attribute对元素进行排序。我需要在循环中获取上一个和下一个元素的 @ID-ATTRIBUTE。

我一直在尝试使用前面的sibling ::和后面的兄弟轴,但无济于事。我也尝试了

<xsl:variable name="current_pos" select="position()"/>
<xsl:value-of select="(//chapter)[($current_pos - 1)]/id>

,但这返回未分类数据的属性值。

示例XML数据:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <chapter id="t19"/>
    <chapter id="a23"/>
    <chapter id="c-0"/>
    <chapter id="d42"/>
    <chapter id="c-1"/>
</root>

样本XSLT样式表:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="3.0">
    <xsl:template match="/root">
        <xsl:for-each select="chapter">
            <xsl:sort select="@id"/>
            <xsl:variable name="current_id" select="@id"/>
            Chapter id: <xsl:value-of select="$current_id"/>
            Sorted position: <xsl:value-of select="position()"/>
            Sorted predecessor chapter id: ? <!-- no idea but most important -->
            Sorted follower chapter id: ? <!-- no idea but most important -->
            <xsl:text>&#xa;</xsl:text>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

我需要的结果:

Chapter id: a23
Sorted position: 1
Sorted predecessor chapter id: none 
Sorted follower chapter id: c-0
Chapter id: c-0
Sorted position: 2
Sorted predecessor chapter id: a23 
Sorted follower chapter id: c-1     
Chapter id: c-1
Sorted position: 3
Sorted predecessor chapter id: c-0 
Sorted follower chapter id: d42
Chapter id: d42
Sorted position: 4
Sorted predecessor chapter id: c-1 
Sorted follower chapter id: t19
Chapter id: t19
Sorted position: 5
Sorted predecessor chapter id: d42 
Sorted follower chapter id: none

您无法在xsl:for-each循环中获得以前和/或以下元素,因为严格地说xsl:for-each不是循环,而是映射构造。

您可以做什么作为将排序结果保存在变量中的结果,例如So ....

<xsl:variable name="chapters" as="element()*">
     <xsl:perform-sort select="chapter">
        <xsl:sort select="@id"/>
    </xsl:perform-sort>
</xsl:variable>

然后,您可以在此上使用xsl:for-each,并在chapters变量本身中访问上一个和下一个值。

<xsl:value-of select="$chapters[position() = $current_pos - 1]/@id" />

尝试此XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="3.0">
    <xsl:template match="/root">
        <xsl:variable name="chapters" as="element()*">
            <xsl:perform-sort select="chapter">
                <xsl:sort select="@id"/>
            </xsl:perform-sort>
        </xsl:variable>
        <xsl:for-each select="$chapters">
            <xsl:variable name="current_id" select="@id"/>
            <xsl:variable name="current_pos" select="position()"/>
            Chapter id: <xsl:value-of select="$current_id"/>
            Sorted position: <xsl:value-of select="position()"/>
            Sorted predecessor chapter id: <xsl:value-of select="$chapters[position() = $current_pos - 1]/@id" />
            Sorted follower chapter id: ? <xsl:value-of select="$chapters[position() = $current_pos + 1]/@id" />
            <xsl:text>&#xa;</xsl:text>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

最新更新