XSLT 获取最后一个元素 [英] XSLT getting last element
问题描述
我试图在我的 xml 中找到最后一个元素,它看起来像:
I am trying to find the last element in my xml, which looks like:
<list>
<element name="A" />
<element name="B" >
<element name="C" />
<element name="D" >
<element name="D" />
<element name="E" />
<element name="F" />
<element name="G" />
</element>
<element name="H" />
<element name="I" />
</list>
我需要获得某种反向菜单,其中当前元素和父元素突出显示为活动";和兄弟姐妹为不活动".相反,结果我只有当我认为D"是一个乱七八糟的树时元素被点击.
I need to get some kind of reverse menu, where current element and parents are highlighted as "active" and sibling as "inactive". Instead in result I have a messy tree only when I suppose "D" element clicked.
双 D 元素是我的问题.当我使用select="//element[@name='D'][last()]"
或 select="//element[@name='D' and last()]"
(顺便说一句,哪个是正确的?)第一次第一次出现 D 元素被选中(调试器显示).这是xsl
Double D elements are my problem. When I use
select="//element[@name='D'][last()]"
or select="//element[@name='D' and last()]"
(btw which one is correct?) first time first occurrence of D element is selected (debugger shows that). Here is xsl
<xsl:template match="list">
<xsl:apply-templates select="//navelement[@name = 'D'][last()]" mode="active"/>
</xsl:template>
<xsl:template match="element">
<ul class="menu">
<xsl:apply-templates select="preceding-sibling::node()" mode="inactive"/>
<li><a>....</a></li>
<xsl:apply-templates select="following-sibling::node()" mode="inactive"/>
</ul>
<xsl:apply-templates select="parent::element" mode="active"/>
</xsl:template>
<xsl:template match="element" mode="inactive">
<li><a>....</a></li>
</xsl:template>
推荐答案
您需要将 last()
索引放在节点列表结果上,而不是作为选择标准的一部分.试试:
You need to put the last()
indexing on the nodelist result, rather than as part of the selection criteria. Try:
(//element[@name='D'])[last()]
这篇关于XSLT 获取最后一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!