代码之家  ›  专栏  ›  技术社区  ›  DevonDahon

xslt:position()和last()未返回预期结果

  •  1
  • DevonDahon  · 技术社区  · 7 年前

    我想知道每个人的职位 svg:use[@href='#electron'] 当它匹配时。我希望能在同一场比赛的其他因素中占据一席之地,但我却得到了一些我不理解的数字。我该怎么用 position() 和 last() 得到预期的数字?

    <!-- Add node "animateTransform" -->
    <xsl:template match="svg:use[@href='#electron']">
        <!-- Copy the element -->
        <xsl:copy>
            <!-- And everything inside it -->
            <xsl:apply-templates select="@* | node()"/>
            <xsl:if test="position() = last()">
                <xsl:attribute name="class">val</xsl:attribute>
            </xsl:if>
            <animateTransform attributeName="transform" type="rotate" from="360 0 0" to="0 0 0" dur="20s" repeatCount="indefinite" />
            <xsl:value-of select="position()"/>/<xsl:value-of select="last()"/>
        </xsl:copy>
    </xsl:template>
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Lucero    7 年前

    这个 position() 和 last() 函数返回相对于当前节点集的位置。

    但是,节点集不是由 match 属性;这只是一个过滤器,用于确定应应用哪个模板。

    问题在于选择要应用模板的节点的部分,例如 apply-templates 结果是 svg:use 比赛。

    尝试在该位置使用:

    <xsl:apply-templates select="svg:use[@href='#electron']"/>
    

    然后你就会得到预期的位置。

    或者,您可以更改最后一项的支票,例如:

    <xsl:if test="not(following-sibling::svg:use[@href='#electron'])">
        <xsl:attribute name="class">val</xsl:attribute>
    </xsl:if>