【发布时间】:2011-07-20 09:42:25
【问题描述】:
除了重写大量 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 的内部结构。
注意:我正在使用带有EXSLT 的 Xalan 2.7.1。所以这些技巧是可用的
有什么想法吗?
【问题讨论】: