Xml 如何访问排序xsl:for每个循环中上一个和下一个元素的值
我使用xsl:for-each循环根据元素的@id属性对元素进行排序。我需要获取循环中上一个和下一个元素的@id属性 我一直在尝试前面的sibling::和后面的sibling axis,但都没有用。我也试过了Xml 如何访问排序xsl:for每个循环中上一个和下一个元素的值,xml,sorting,xslt,Xml,Sorting,Xslt,我使用xsl:for-each循环根据元素的@id属性对元素进行排序。我需要获取循环中上一个和下一个元素的@id属性 我一直在尝试前面的sibling::和后面的sibling axis,但都没有用。我也试过了 <xsl:variable name="current_pos" select="position()"/> <xsl:value-of select="(//chapter)[($current_pos - 1)]/id> 不能在xsl:for-each循环中
<xsl:variable name="current_pos" select="position()"/>
<xsl:value-of select="(//chapter)[($current_pos - 1)]/id>
不能在xsl:for-each循环中获取前面和/或后面的元素,因为严格来说xsl:for-each不是一个循环,而是一个映射构造 您可以将排序结果保存在变量中,如下所示
<xsl:variable name="chapters" as="element()*">
<xsl:perform-sort select="chapter">
<xsl:sort select="@id"/>
</xsl:perform-sort>
</xsl:variable>
试试这个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>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
首先进行排序,然后从排序序列中选择项,如您所示,version=3.0,如果支持高阶函数,则可以使用sortchapter、函数$c{$c/@id}来获得排序序列,甚至可以使用sortchapter/@id/数据来获得排序字符串序列。输出所有的值可以在XQuery中用一个翻滚窗口子句和一个存储您感兴趣的值的开始和结束子句来完成。有关更多信息,请参见。
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:variable name="chapters" as="element()*">
<xsl:perform-sort select="chapter">
<xsl:sort select="@id"/>
</xsl:perform-sort>
</xsl:variable>
<xsl:value-of select="$chapters[position() = $current_pos - 1]/@id" />
<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>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>