如何将函数应用于 XSLT 中的节点序列 [英] How to apply a function to a sequence of nodes in XSLT

查看:46
本文介绍了如何将函数应用于 XSLT 中的节点序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个 XSLT 函数,将节点序列转换为字符串序列.我需要做的是对序列中的所有节点应用一个函数,并返回一个与原始序列一样长的序列.

I need to write an XSLT function that transforms a sequence of nodes into a sequence of strings. What I need to do is to apply a function to all the nodes in the sequence and return a sequence as long as the original one.

这是输入文件

<article id="4">
    <author ref="#Guy1"/>
    <author ref="#Guy2"/>
</article>

调用站点是这样的:

<xsl:template match="article">
    <xsl:text>Author for </xsl:text>
    <xsl:value-of select="@id"/>

    <xsl:variable name="names" select="func:author-names(.)"/>

    <xsl:value-of select="string-join($names, ' and ')"/>
    <xsl:value-of select="count($names)"/>
</xsl:function>

这是函数的代码:

<xsl:function name="func:authors-names">
    <xsl:param name="article"/>

    <!-- HELP: this is where I call `func:format-name` on
         each `$article/author` element -->
</xsl:function>

我应该在 func:author-names 中使用什么?我尝试使用 xsl:for-each 但结果是单个节点,而不是序列.

What should I use inside func:author-names? I tried using xsl:for-each but the result is a single node, not a sequence.

推荐答案

is一种方式,另一种是 <xsl:sequence select="for $a in $article/author return func:format-name($a)"/>.

<xsl:sequence select="$article/author/func:format-name(.)"/> is one way, the other is <xsl:sequence select="for $a in $article/author return func:format-name($a)"/>.

我不确定你当然需要这个功能,做

I am not sure you would need the function of course, doing

<xsl:value-of select="author/func:format-name(.)" separator=" and "/>

文章的模板中应该可以.

这篇关于如何将函数应用于 XSLT 中的节点序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆