Xml 在xslt中传递值并返回值

Xml 在xslt中传递值并返回值,xml,xslt,Xml,Xslt,我想打印属性及其值。因此,我创建了一个XSLT函数。 试图编码 <xsl:template name="preserve"> <xsl:call-template name="single"/> <w:r> <xsl:value-of select="t:get-attr-from-ele('ewd')"/> <w:instrText> <xsl:attrib

我想打印属性及其值。因此,我创建了一个XSLT函数。 试图编码

<xsl:template name="preserve">
    <xsl:call-template name="single"/>
    <w:r>
        <xsl:value-of select="t:get-attr-from-ele('ewd')"/>
        <w:instrText>
            <xsl:attribute name="xml:space">
                <xsl:value-of select="'preserve'"/>
            </xsl:attribute>
            <xsl:value-of select="w:ins[1]//w:instrText"/>
        </w:instrText>
    </w:r>
</xsl:template>



<xsl:function name="t:get-attr-from-ele">
    <xsl:param name="attr"/>

    <xsl:attribute name="{$attr}">
        <xsl:value-of select="generate-id()"/>
    </xsl:attribute>
</xsl:function>

当我尝试这个时,我得到了以下>错误

没有上下文项


我如何解决这个问题?我使用的是XSLT 2

嗯,函数内部没有上下文项,因此使用
generate-id()
不起作用,因为它需要上下文项。另外,如果您想让该函数为结果创建属性节点,那么以后使用
xsl:value of
调用该函数是没有意义的,因为这样会创建文本节点。因此,基本上,如果要在函数内部使用
generate id
,则需要将节点作为参数显式传入(或使用一些全局参数或变量):


然后您可以使用

<xsl:function name="t:get-attr-from-ele">
    <xsl:param name="context-node" as="node()"/>
    <xsl:param name="attr" as="xs:string"/>

    <xsl:attribute name="{$attr}">
        <xsl:value-of select="generate-id($context-node)"/>
    </xsl:attribute>
</xsl:function>