除了重写大量 XSLT 代码(我不会这样做)之外,当上下文被任意设置为其他内容时,有没有办法在其父元素中找到元素的位置?这是一个例子:
<!-- Here are my records-->
<xsl:for-each select="/path/to/record">
<xsl:variable name="record" select="."/>
<!-- At this point, I could use position() -->
<!-- Set the context to the current record -->
<xsl:for-each select="$record">
<!-- At this point, position() is meaningless because it's always 1 -->
<xsl:call-template name="SomeTemplate"/>
</xsl:for-each>
</xsl:for-each>
<!-- This template expects the current context being set to a record -->
<xsl:template name="SomeTemplate">
<!-- it does stuff with the record's fields -->
<xsl:value-of select="SomeRecordField"/>
<!-- How to access the record's position in /path/to or in any other path? -->
</xsl:template>
注意:这是一个简化的示例。我有几个限制因素使我无法实施明显的解决方案,例如将新参数传递给SomeTemplate
等。我真的只能修改SomeTemplate
.
注意:我将 Xalan 2.7.1 与EXSLT一起使用。所以这些技巧是可用的
有任何想法吗?