How to apply a function to a sequence of nodes in XSLT
sequence, xslt, xslt-2.0
Solution
`<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 "/>
in the template of `article` should do.
Problem
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. This is the input document ``` <article id="4"> <author ref="#Guy1"/> <author ref="#Guy2"/> </article> ``` This is how the calling site: ``` <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> ``` And this is the code of the 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> ``` What should I use inside `func:author-names`? I tried using `xsl:for-each` but the result is a single node, not a sequence.