看看下面的代码:
<xsl:template match="tocline[@toclevel='2']">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:for-each select="descendant::toctitle">
<xsl:if test="position() = last()">
<xsl:attribute name="last">
<xsl:value-of select="'true'"/>
</xsl:attribute>
</xsl:if>
</xsl:for-each>
<xsl:apply-templates />
</xsl:copy>
</xsl:template>
此模板将属性应用于 tocline 元素。我希望它将属性应用于节点集中的最后一个 toctitle,它可以位于不同的级别。
使用此示例:
<tocline id="d1e11" toclevel="1">
<toctitle>Section 1. Legislative Powers</toctitle>
<tocline id="d1e40" toclevel="2">
<toctitle>Separation of Powers and Checks and Balances</toctitle>
<tocline id="d1e51" toclevel="3">
<toctitle>The Theory Elaborated and Implemented</toctitle>
</tocline>
<tocline id="d1e189" toclevel="3">
<toctitle>Judicial Enforcement</toctitle>
</tocline>
</tocline>
</tocline>
我要这个:
<tocline id="d1e11" toclevel="1">
<toctitle>Section 1. Legislative Powers</toctitle>
<tocline id="d1e40" toclevel="2">
<toctitle>Separation of Powers and Checks and Balances</toctitle>
<tocline id="d1e51" toclevel="3">
<toctitle>The Theory Elaborated and Implemented</toctitle>
</tocline>
<tocline id="d1e189" toclevel="3">
<toctitle last="true">Judicial Enforcement</toctitle>
</tocline>
</tocline>
</tocline>
但我明白了:
<tocline id="d1e11" toclevel="1">
<toctitle>Section 1. Legislative Powers</toctitle>
<tocline id="d1e40" toclevel="2" last="true">
<toctitle>Separation of Powers and Checks and Balances</toctitle>
<tocline id="d1e51" toclevel="3">
<toctitle>The Theory Elaborated and Implemented</toctitle>
</tocline>
<tocline id="d1e189" toclevel="3">
<toctitle>Judicial Enforcement</toctitle>
</tocline>
</tocline>
</tocline>