XSLT:将每个匹配项的值从一个传递到下一个
我使用以下命令将所有XSLT:将每个匹配项的值从一个传递到下一个,xslt,xpath,docbook,Xslt,Xpath,Docbook,我使用以下命令将所有与修订属性集匹配可以出现在文档树的许多不同级别,始终包含在s中 <xsl:for-each select="//section[@revision]"> <!-- Do one thing if this is the first section matched in this chapter --> <!-- Do something else if this section is in the sam
与修订属性集匹配<代码>可以出现在文档树的许多不同级别,始终包含在
s中
<xsl:for-each select="//section[@revision]">
<!-- Do one thing if this is the first section
matched in this chapter -->
<!-- Do something else if this section is in the same
chapter as the last section matched -->
</xsl:for-each>
但我认为这完全可以用XPath实现
有什么想法吗?谢谢 position()将返回当前迭代中的位置。第一次迭代IIRC将返回0,因此测试“position()=0”将告诉您是否在第一次迭代中。不确定我是否100%取消了您的需求,但是
<xsl:variable name="sections" select="//section[@revision]" />
<xsl:for-each select="$sections">
<xsl:variable name="ThisPos" select="position()" />
<xsl:variable name="PrevMatchedSection" select="$sections[$ThisPos - 1]" />
<xsl:choose>
<xsl:when test="
not($PrevMatchedSection)
or
generate-id($PrevMatchedSection/ancestor::chapter[1])
!=
generate-id(ancestor::chapter[1])
">
<!-- Do one thing if this is the first section
matched in this chapter -->
</xsl:when>
<xsl:otherwise>
<!-- Do something else if this section is in the same
chapter as the last section matched -->
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
然而,我怀疑整个问题可以通过
/
方法更优雅地解决。但是如果没有看到您的输入和预期输出,这很难说。迭代次数无关紧要;我需要将章节号从一个迭代传递到下一个迭代。谢谢,我正在做这个。但是有一件事:在每个的循环中,在之前为每个
定义的节
变量不可用。不过,只要不使用常量就可以轻松修复。当然,变量在
中是可用的。唯一不正确的是[position()-1]
谓词,我现在已经修复了它。
<xsl:variable name="sections" select="//section[@revision]" />
<xsl:for-each select="$sections">
<xsl:variable name="ThisPos" select="position()" />
<xsl:variable name="PrevMatchedSection" select="$sections[$ThisPos - 1]" />
<xsl:choose>
<xsl:when test="
not($PrevMatchedSection)
or
generate-id($PrevMatchedSection/ancestor::chapter[1])
!=
generate-id(ancestor::chapter[1])
">
<!-- Do one thing if this is the first section
matched in this chapter -->
</xsl:when>
<xsl:otherwise>
<!-- Do something else if this section is in the same
chapter as the last section matched -->
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>