XSLT - 编写名称模板并将 xpath 作为参数传递

XSLT - write name template and passing xpath as parameter

我有一个 xml 如下,

<doc>
    <p c="1">para <style c="Bold">content 1</style> </p>
    <p c="1">para <style c="Bold">content 2</style> </p>
    <p c="1">para <style c="Bold">content 2</style> </p>
    <p c="1">para <style c="Bold">content 2</style> </p>
    <p c="1">para <style c="Bold">content 2</style> </p>
    <s></s>
</doc>

my objective 正在编写一个名称模板来计算 <s> 节点的先行兄弟姐妹的数量,如果 <s> 节点则显示该数字。 我已经写了以下 xsl 来做到这一点,

<xsl:template name="myTemp">
        <xsl:param name="var" as="node()"/>
        <xsl:value-of select="count($var/preceding-sibling::p)"/>
    </xsl:template>

    <xsl:template match="s">
        <xsl:call-template name="myTemp">
            <xsl:with-param name="var" select="s"/>
        </xsl:call-template>
    </xsl:template>

但它给了我 'An empty sequence is not allowed as the value of parameter $var' 撒克逊语。

你能提出解决这个问题的建议吗?

请注意:有更简单的方法来计算 <s> 节点的主持兄弟姐妹并打印。但我需要的是编写一个名称模板,然后编写另一个模板并调用该名称模板。

在匹配 s 的模板中,它是上下文节点,因此您需要 <xsl:with-param name="var" select="."/> 而不是 <xsl:with-param name="var" select="s"/>